Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet add package cli support for cpm projects #4700

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ static MSBuildAPIUtilityTests()
{
MSBuildLocator.RegisterDefaults();
}

[Fact]
[PlatformFact(Platform.Windows)]
public void GetDirectoryBuildPropsRootElementWhenItExists_Success()
{
// Arrange
var testDirectory = TestDirectory.Create();

var projectCollection = new ProjectCollection(
Expand Down Expand Up @@ -59,18 +60,19 @@ public void GetDirectoryBuildPropsRootElementWhenItExists_Success()
</PropertyGroup>
</Project>";
File.WriteAllText(Path.Combine(testDirectory, "projectA.csproj"), projectContent);

var project = Project.FromFile(Path.Combine(testDirectory, "projectA.csproj"), projectOptions);

// Act
var result = new MSBuildAPIUtility(logger: new TestLogger()).GetDirectoryBuildPropsRootElement(project);

// Assert
Assert.Equal(Path.Combine(testDirectory, "Directory.Packages.props"), result.FullPath);
}

[Fact]
[PlatformFact(Platform.Windows)]
public void AddPackageReferenceIntoProjectFileWhenItemGroupDoesNotExist_Success()
{
// Set up
// Arrange
var testDirectory = TestDirectory.Create();
var projectCollection = new ProjectCollection(
globalProperties: null,
Expand All @@ -89,7 +91,7 @@ public void AddPackageReferenceIntoProjectFileWhenItemGroupDoesNotExist_Success(
ProjectCollection = projectCollection
};

// Set up project file
// Arrange project file
string projectContent =
@$"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
Expand All @@ -116,14 +118,15 @@ public void AddPackageReferenceIntoProjectFileWhenItemGroupDoesNotExist_Success(
project.Save();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had an offline conversation with @pragnya17 about removing project.Save() method call but instead use MSBuild Project object model to validate the result.

For context, project.Save() is invoked here to save the modification done by AddPackageReferenceIntoItemGroupCPM to the file system and then verify the behavior. Given that these are unit tests, I am wondering if we use the MSBuild API to validate the result instead of reading csproj or packages.props file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. However, there were some issues with debugging the unit test after making this modification so during an offline conversation, Kartheek and I agreed that he would be able to finish debugging/making these modifications to the unit tests.


// Assert
Assert.Contains(@$"<PackageReference Include=""X"" />", File.ReadAllText(Path.Combine(testDirectory, "projectA.csproj")));
Assert.DoesNotContain(@$"<Version = ""1.0.0"" />", File.ReadAllText(Path.Combine(testDirectory, "projectA.csproj")));
string updatedProjectFile = File.ReadAllText(Path.Combine(testDirectory, "projectA.csproj"));
Assert.Contains(@$"<PackageReference Include=""X"" />", updatedProjectFile);
Assert.DoesNotContain(@$"<Version = ""1.0.0"" />", updatedProjectFile);
}

[Fact]
[PlatformFact(Platform.Windows)]
public void AddPackageReferenceIntoProjectFileWhenItemGroupDoesExist_Success()
{
// Set up
// Arrange
var testDirectory = TestDirectory.Create();
var projectCollection = new ProjectCollection(
globalProperties: null,
Expand All @@ -142,7 +145,7 @@ public void AddPackageReferenceIntoProjectFileWhenItemGroupDoesExist_Success()
ProjectCollection = projectCollection
};

// Set up project file
// Arrange project file
string projectContent =
@$"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
Expand Down Expand Up @@ -174,14 +177,15 @@ public void AddPackageReferenceIntoProjectFileWhenItemGroupDoesExist_Success()
project.Save();

// Assert
Assert.Contains(@$"<PackageReference Include=""X"" />", File.ReadAllText(Path.Combine(testDirectory, "projectA.csproj")));
Assert.DoesNotContain(@$"<Version = ""1.0.0"" />", File.ReadAllText(Path.Combine(testDirectory, "projectA.csproj")));
string updatedProjectFile = File.ReadAllText(Path.Combine(testDirectory, "projectA.csproj"));
Assert.Contains(@$"<PackageReference Include=""X"" />", updatedProjectFile);
Assert.DoesNotContain(@$"<Version = ""1.0.0"" />", updatedProjectFile);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This always return true, so I think it should be changed to $"PackageReference Includes =""X"" <Version = ""1.0.0"" />". This is along the lines of this comment: #4700 (comment)

}

[Fact]
[PlatformFact(Platform.Windows)]
public void AddPackageVersionIntoPropsFileWhenItemGroupDoesNotExist_Success()
{
// Set up
// Arrange
var testDirectory = TestDirectory.Create();
var projectCollection = new ProjectCollection(
globalProperties: null,
Expand All @@ -200,7 +204,7 @@ public void AddPackageVersionIntoPropsFileWhenItemGroupDoesNotExist_Success()
ProjectCollection = projectCollection
};

// Set up Directory.Packages.props file
// Arrange Directory.Packages.props file
var propsFile =
@$"<Project>
<PropertyGroup>
Expand All @@ -209,7 +213,7 @@ public void AddPackageVersionIntoPropsFileWhenItemGroupDoesNotExist_Success()
</Project>";
File.WriteAllText(Path.Combine(testDirectory, "Directory.Packages.props"), propsFile);

// Set up project file
// Arrange project file
string projectContent =
@$"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
Expand Down Expand Up @@ -243,10 +247,10 @@ public void AddPackageVersionIntoPropsFileWhenItemGroupDoesNotExist_Success()
</ItemGroup>", File.ReadAllText(Path.Combine(testDirectory, "Directory.Packages.props")));
}

[Fact]
[PlatformFact(Platform.Windows)]
public void AddPackageVersionIntoPropsFileWhenItemGroupExists_Success()
{
// Set up
// Arrange
var testDirectory = TestDirectory.Create();
var projectCollection = new ProjectCollection(
globalProperties: null,
Expand All @@ -265,7 +269,7 @@ public void AddPackageVersionIntoPropsFileWhenItemGroupExists_Success()
ProjectCollection = projectCollection
};

// Set up Directory.Packages.props file
// Arrange Directory.Packages.props file
var propsFile =
@$"<Project>
<PropertyGroup>
Expand All @@ -277,7 +281,7 @@ public void AddPackageVersionIntoPropsFileWhenItemGroupExists_Success()
</Project>";
File.WriteAllText(Path.Combine(testDirectory, "Directory.Packages.props"), propsFile);

// Set up project file
// Arrange project file
string projectContent =
@$"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
Expand Down Expand Up @@ -312,10 +316,10 @@ public void AddPackageVersionIntoPropsFileWhenItemGroupExists_Success()
Assert.Contains(@$"<PackageVersion Include=""Y"" Version=""1.0.0"" />", File.ReadAllText(Path.Combine(testDirectory, "Directory.Packages.props")));
}

[Fact]
[PlatformFact(Platform.Windows)]
public void UpdatePackageVersionInPropsFileWhenItExists_Success()
{
// Set up
// Arrange
var testDirectory = TestDirectory.Create();
var projectCollection = new ProjectCollection(
globalProperties: null,
Expand All @@ -334,7 +338,7 @@ public void UpdatePackageVersionInPropsFileWhenItExists_Success()
ProjectCollection = projectCollection
};

// Set up Directory.Packages.props file
// Arrange Directory.Packages.props file
var propsFile =
@$"<Project>
<PropertyGroup>
Expand All @@ -346,7 +350,7 @@ public void UpdatePackageVersionInPropsFileWhenItExists_Success()
</Project>";
File.WriteAllText(Path.Combine(testDirectory, "Directory.Packages.props"), propsFile);

// Set up project file
// Arrange project file
string projectContent =
@$"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
Expand Down Expand Up @@ -376,9 +380,9 @@ public void UpdatePackageVersionInPropsFileWhenItExists_Success()

// Assert
Assert.Equal(projectContent, File.ReadAllText(Path.Combine(testDirectory, "projectA.csproj")));
Assert.Contains(@$"<PackageVersion Include=""X"" Version=""2.0.0"" />", File.ReadAllText(Path.Combine(testDirectory, "Directory.Packages.props")));
Assert.DoesNotContain(@$"<PackageVersion Include=""X"" Version=""1.0.0"" />", File.ReadAllText(Path.Combine(testDirectory, "Directory.Packages.props")));
string updatedPropsFile = File.ReadAllText(Path.Combine(testDirectory, "Directory.Packages.props"));
Assert.Contains(@$"<PackageVersion Include=""X"" Version=""2.0.0"" />", updatedPropsFile);
Assert.DoesNotContain(@$"<PackageVersion Include=""X"" Version=""1.0.0"" />", updatedPropsFile);
}

}
}