Skip to content

Commit

Permalink
Version 2.0 of central package versions
Browse files Browse the repository at this point in the history
Use <PackageReference Update="" /> instead of <PackageVersion /> because having two lists is hard to get metadata right.

Update README
  • Loading branch information
jeffkl committed May 23, 2018
1 parent 04dd89e commit e283ca7
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 128 deletions.
123 changes: 97 additions & 26 deletions src/CentralPackageVersions.UnitTests/CentralPackageVersionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,93 @@ public class CentralPackageVersionsTests : MSBuildSdkTestBase
[Fact]
public void CanDisableCentralPackageVersions()
{
WritePackagesProps();

ProjectCreator.Templates
.SdkCsproj(
path: Path.Combine(TestRootPath, "test.csproj"),
projectCollection: new ProjectCollection(new Dictionary<string, string>
{
{ "EnableCentralPackageVersions", "false" },
{ "DisableImplicitFrameworkReferences", "true" }
["EnableCentralPackageVersions"] = "false",
["DisableImplicitFrameworkReferences"] = "true"
}),
projectCreator: creator => creator
.ItemPackageReference("Foo", "10.0.0")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Save()
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput)
.Project
.GetItems("PackageReference").ToDictionary(i => i.EvaluatedInclude, i => i.GetMetadataValue("Version"))
.ShouldBe(new Dictionary<string, string>
{
{ "Foo", "10.0.0" }
["Foo"] = "10.0.0"
});

result.ShouldBeTrue(() => buildOutput.GetConsoleLog());
}

[Fact]
public void CanDisableGlobalPackageReferences()
{
WritePackagesProps();

ProjectCreator.Templates
.SdkCsproj(
path: Path.Combine(TestRootPath, "test.csproj"),
projectCollection: new ProjectCollection(new Dictionary<string, string>
{
["DisableImplicitFrameworkReferences"] = "true",
["EnableGlobalPackageReferences"] = "false"
}),
projectCreator: creator => creator
.ItemPackageReference("Foo")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput)
.Project
.GetItems("PackageReference").ToDictionary(i => i.EvaluatedInclude, i => i.GetMetadataValue("Version"))
.ShouldBe(
new Dictionary<string, string>
{
["Foo"] = "1.2.3"
},
ignoreOrder: true);

result.ShouldBeTrue(() => buildOutput.GetConsoleLog());
}

[Fact]
public void CanOverridePackageVersion()
{
WritePackagesProps();

ProjectCreator.Templates
.SdkCsproj(
path: Path.Combine(TestRootPath, "test.csproj"),
projectCollection: new ProjectCollection(new Dictionary<string, string>
{
["DisableImplicitFrameworkReferences"] = "true"
}),
projectCreator: creator => creator
.ItemPackageReference(
"Foo",
metadata: new Dictionary<string, string>
{
["VersionOverride"] = "9.0.1"
})
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput)
.Project
.GetItems("PackageReference").ToDictionary(i => i.EvaluatedInclude, i => i.GetMetadataValue("Version"))
.ShouldBe(
new Dictionary<string, string>
{
["Foo"] = "9.0.1",
["Global1"] = "1.0.0"
},
ignoreOrder: true);

result.ShouldBeTrue(() => buildOutput.GetConsoleLog());
}

[Fact]
public void LogErrorIfProjectSpecifiesGlobalPackageReference()
{
Expand All @@ -54,15 +118,11 @@ public void LogErrorIfProjectSpecifiesGlobalPackageReference()
.ItemPackageReference("Foo")
.ItemPackageReference("Global1")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Save()
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());

buildOutput.Errors
.Select(i => i.Message)
.ToList()
.ShouldBe(new[] { $"The package reference \'Global1\' is already defined as a GlobalPackageReference in \'{packagesProps.FullPath}\'. Individual projects do not need to include a PackageReference if a GlobalPackageReference is declared." });
buildOutput.Errors.ShouldBe(new[] { $"The package reference \'Global1\' is already defined as a GlobalPackageReference in \'{packagesProps.FullPath}\'. Individual projects do not need to include a PackageReference if a GlobalPackageReference is declared." });
}

[Fact]
Expand All @@ -77,15 +137,11 @@ public void LogErrorIfProjectSpecifiesUnknownPackage()
.ItemPackageReference("Foo")
.ItemPackageReference("Baz")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.Save()
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());

buildOutput.Errors
.Select(i => i.Message)
.ToList()
.ShouldBe(new[] { $"The package reference \'Baz\' must have a version defined in \'{packagesProps.FullPath}\'." });
buildOutput.Errors.ShouldBe(new[] { $"The package reference \'Baz\' must have a version defined in \'{packagesProps.FullPath}\'." });
}

[Fact]
Expand All @@ -99,16 +155,34 @@ public void LogErrorIfProjectSpecifiesVersion()
projectCreator: creator => creator
.ItemPackageReference("Foo", "10.0.0")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());

.Save()
buildOutput.Errors.ShouldBe(new[] { $"The package reference \'Foo\' should not specify a version. Please specify the version in \'{packagesProps.FullPath}\' or set VersionOverride to override the centrally defined version." });
}

[Fact]
public void LogErrorIfProjectSpecifiesVersionAndVersionOverrideIsDisabled()
{
ProjectCreator packagesProps = WritePackagesProps();

ProjectCreator.Templates
.SdkCsproj(
path: Path.Combine(TestRootPath, "test.csproj"),
projectCollection: new ProjectCollection(new Dictionary<string, string>
{
["DisableImplicitFrameworkReferences"] = "true",
["EnablePackageVersionOverride"] = "false"
}),
projectCreator: creator => creator
.ItemPackageReference("Foo", "10.0.0")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());

buildOutput.Errors
.Select(i => i.Message)
.ToList()
.ShouldBe(new[] { $"The package reference \'Foo\' should not specify a version. Please specify the version in \'{packagesProps.FullPath}\'." });
buildOutput.Errors.ShouldBe(new[] { $"The package reference \'Foo\' should not specify a version. Please specify the version in \'{packagesProps.FullPath}\'." });
}

[Fact]
Expand All @@ -121,14 +195,12 @@ public void PackageVersionsAreApplied()
path: Path.Combine(TestRootPath, "test.csproj"),
projectCollection: new ProjectCollection(new Dictionary<string, string>
{
{ "DisableImplicitFrameworkReferences", "true" }
["DisableImplicitFrameworkReferences"] = "true"
}),
projectCreator: creator => creator
.ItemPackageReference("Foo")
.ItemPackageReference("Bar")
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))

.Save()
.Project
.GetItems("PackageReference").ToDictionary(i => i.EvaluatedInclude, i => i.GetMetadataValue("Version"))
.ShouldBe(new Dictionary<string, string>
Expand All @@ -144,16 +216,15 @@ private ProjectCreator WritePackagesProps()
return ProjectCreator.Templates
.PackagesProps(
path: Path.Combine(TestRootPath, "Packages.props"),
packageVersions: new Dictionary<string, string>
packageReferences: new Dictionary<string, string>
{
["Foo"] = "1.2.3",
["Bar"] = "4.5.6",
["Global1"] = "1.0.0",
["NETStandard.Library"] = "2.0.0"
},
globalPackageReferences: new List<string>
globalPackageReferences: new Dictionary<string, string>
{
"Global1"
["Global1"] = "1.0.0"
})
.Save();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static ProjectCreator PackagesProps(
string treatAsLocalProperty = null,
ProjectCollection projectCollection = null,
NewProjectFileOptions? projectFileOptions = NewProjectFileOptions.IncludeXmlDeclaration | NewProjectFileOptions.IncludeXmlNamespace,
IReadOnlyDictionary<string, string> packageVersions = null,
IReadOnlyCollection<string> globalPackageReferences = null)
IReadOnlyDictionary<string, string> packageReferences = null,
IReadOnlyDictionary<string, string> globalPackageReferences = null)
{
return ProjectCreator.Create(
path,
Expand All @@ -34,9 +34,9 @@ public static ProjectCreator PackagesProps(
treatAsLocalProperty,
projectCollection,
projectFileOptions)
.ForEach(packageVersions, (i, creator) => creator.ItemPackageVersion(i.Key, i.Value))
.ForEach(packageReferences, (i, creator) => creator.ItemCentralPackageReference(i.Key, i.Value))
.ItemGroup()
.ForEach(globalPackageReferences, (i, creator) => creator.ItemGlobalPackageReference(i))
.ForEach(globalPackageReferences, (i, creator) => creator.ItemGlobalPackageReference(i.Key, i.Value))
.CustomAction(customAction);
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/CentralPackageVersions.UnitTests/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,29 @@ namespace Microsoft.Build.CentralPackageVersions.UnitTests
{
public static class ExtensionMethods
{
public static ProjectCreator ItemGlobalPackageReference(this ProjectCreator creator, string packageId, string includeAssets = null, string excludeAssets = null, string privateAssets = null, IDictionary<string, string> metadata = null, string condition = null)
public static ProjectCreator ItemGlobalPackageReference(this ProjectCreator creator, string packageId, string version, string includeAssets = null, string excludeAssets = null, string privateAssets = null, IDictionary<string, string> metadata = null, string condition = null)
{
return creator.ItemInclude(
itemType: "GlobalPackageReference",
include: packageId,
metadata: metadata.Merge(new Dictionary<string, string>
{
{ "Version", version },
{ "IncludeAssets", includeAssets },
{ "ExcludeAssets", excludeAssets },
{ "PrivateAssets", privateAssets },
}),
condition: condition);
}

public static ProjectCreator ItemPackageVersion(this ProjectCreator creator, string packageId, string version, string includeAssets = null, string excludeAssets = null, string privateAssets = null, IDictionary<string, string> metadata = null, string condition = null)
public static ProjectCreator ItemCentralPackageReference(this ProjectCreator creator, string packageId, string version, IDictionary<string, string> metadata = null, string condition = null)
{
return creator.ItemInclude(
itemType: "PackageVersion",
include: packageId,
return creator.ItemUpdate(
itemType: "PackageReference",
update: packageId,
metadata: metadata.Merge(new Dictionary<string, string>
{
{ "Version", version },
{ "IncludeAssets", includeAssets },
{ "ExcludeAssets", excludeAssets },
{ "PrivateAssets", privateAssets },
}),
condition: condition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="Microsoft.Build.Framework" Version="15.7.179" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.0.13" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="MSBuild.ProjectCreation" Version="1.0.11" />
<PackageReference Include="MSBuild.ProjectCreation" Version="1.1.1" />
<PackageReference Include="Shouldly" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
Expand Down
Loading

0 comments on commit e283ca7

Please sign in to comment.