Migrate: auto-inject test dependency version (#4565)
* Migrate: auto-injected dependencies should overwrite existing dependency version if present * Address PR comments
This commit is contained in:
parent
ad7c462956
commit
155ef033f2
5 changed files with 111 additions and 40 deletions
|
@ -6,5 +6,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
internal class ConstantPackageVersions
|
||||
{
|
||||
public const string AspNetToolsVersion = "1.0.0-rc1-final";
|
||||
public const string TestSdkPackageVersion = "15.0.0-preview-20161024-02";
|
||||
public const string XUnitPackageVersion = "2.2.0-beta3-build3402";
|
||||
public const string XUnitRunnerPackageVersion = "2.2.0-beta4-build1188";
|
||||
}
|
||||
}
|
|
@ -5,32 +5,65 @@ using System.Collections.Generic;
|
|||
|
||||
namespace Microsoft.DotNet.ProjectJsonMigration
|
||||
{
|
||||
internal class PackageDependencyInfo
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Version { get; set; }
|
||||
public string PrivateAssets { get; set; }
|
||||
}
|
||||
|
||||
internal class PackageConstants
|
||||
{
|
||||
public const string SdkPackageName = "Microsoft.NET.Sdk";
|
||||
public const string WebSdkPackageName = "Microsoft.NET.Sdk.Web";
|
||||
public const string TestSdkPackageName = "Microsoft.NET.Test.Sdk";
|
||||
public const string TestSdkPackageVersion = "15.0.0-preview-20161024-02";
|
||||
public const string XUnitPackageName = "xunit";
|
||||
public const string XUnitPackageVersion = "2.2.0-beta3-build3402";
|
||||
public const string XUnitRunnerPackageName = "xunit.runner.visualstudio";
|
||||
public const string XUnitRunnerPackageVersion = "2.2.0-beta4-build1188";
|
||||
|
||||
public static readonly IDictionary<string, string> AspProjectDependencyToolsPackages = new Dictionary<string, string> {
|
||||
{"Microsoft.EntityFrameworkCore.Tools", "Microsoft.EntityFrameworkCore.Tools"},
|
||||
{"Microsoft.AspNetCore.Razor.Tools", "Microsoft.AspNetCore.Razor.Design"},
|
||||
{"Microsoft.AspNetCore.Razor.Design", "Microsoft.AspNetCore.Razor.Design"},
|
||||
{"Microsoft.VisualStudio.Web.CodeGenerators.Mvc", "Microsoft.VisualStudio.Web.CodGeneration.Design"},
|
||||
{"Microsoft.VisualStudio.Web.CodeGeneration.Tools", ""},
|
||||
public static readonly IDictionary<string, PackageDependencyInfo> ProjectDependencyPackages =
|
||||
new Dictionary<string, PackageDependencyInfo> {
|
||||
{"Microsoft.EntityFrameworkCore.Tools", new PackageDependencyInfo {
|
||||
Name = "Microsoft.EntityFrameworkCore.Tools",
|
||||
Version = ConstantPackageVersions.AspNetToolsVersion } },
|
||||
{ "Microsoft.AspNetCore.Razor.Tools", new PackageDependencyInfo {
|
||||
Name = "Microsoft.AspNetCore.Razor.Design",
|
||||
Version = ConstantPackageVersions.AspNetToolsVersion } },
|
||||
{ "Microsoft.AspNetCore.Razor.Design", new PackageDependencyInfo {
|
||||
Name = "Microsoft.AspNetCore.Razor.Design",
|
||||
Version = ConstantPackageVersions.AspNetToolsVersion } },
|
||||
{ "Microsoft.VisualStudio.Web.CodeGenerators.Mvc", new PackageDependencyInfo {
|
||||
Name = "Microsoft.VisualStudio.Web.CodeGeneration.Design",
|
||||
Version = ConstantPackageVersions.AspNetToolsVersion } },
|
||||
{ "Microsoft.VisualStudio.Web.CodeGeneration.Tools", null},
|
||||
{ TestSdkPackageName, new PackageDependencyInfo {
|
||||
Name = TestSdkPackageName,
|
||||
Version = ConstantPackageVersions.TestSdkPackageVersion } },
|
||||
{ XUnitPackageName, new PackageDependencyInfo {
|
||||
Name = XUnitPackageName,
|
||||
Version = ConstantPackageVersions.XUnitPackageVersion } },
|
||||
{ XUnitRunnerPackageName, new PackageDependencyInfo {
|
||||
Name = XUnitRunnerPackageName,
|
||||
Version = ConstantPackageVersions.XUnitRunnerPackageVersion } },
|
||||
};
|
||||
|
||||
public static readonly IDictionary<string, string> AspProjectToolsPackages = new Dictionary<string, string> {
|
||||
{"Microsoft.EntityFrameworkCore.Tools", "Microsoft.EntityFrameworkCore.Tools.DotNet"},
|
||||
{"Microsoft.AspNetCore.Razor.Tools", "Microsoft.AspNetCore.Razor.Tools"},
|
||||
{"Microsoft.VisualStudio.Web.CodeGeneration.Tools", "Microsoft.VisualStudio.Web.CodeGeneration.Tools"},
|
||||
{"Microsoft.DotNet.Watcher.Tools", "Microsoft.DotNet.Watcher.Tools"},
|
||||
{"Microsoft.Extensions.SecretManager.Tools", "Microsoft.Extensions.SecretManager.Tools"},
|
||||
{"Microsoft.AspNetCore.Server.IISIntegration.Tools", ""}
|
||||
public static readonly IDictionary<string, PackageDependencyInfo> ProjectToolPackages =
|
||||
new Dictionary<string, PackageDependencyInfo> {
|
||||
{"Microsoft.EntityFrameworkCore.Tools", new PackageDependencyInfo {
|
||||
Name = "Microsoft.EntityFrameworkCore.Tools.DotNet",
|
||||
Version = ConstantPackageVersions.AspNetToolsVersion } },
|
||||
{ "Microsoft.AspNetCore.Razor.Tools", new PackageDependencyInfo {
|
||||
Name = "Microsoft.AspNetCore.Razor.Tools",
|
||||
Version = ConstantPackageVersions.AspNetToolsVersion } },
|
||||
{ "Microsoft.VisualStudio.Web.CodeGeneration.Tools", new PackageDependencyInfo {
|
||||
Name = "Microsoft.VisualStudio.Web.CodeGeneration.Tools",
|
||||
Version = ConstantPackageVersions.AspNetToolsVersion } },
|
||||
{ "Microsoft.DotNet.Watcher.Tools", new PackageDependencyInfo {
|
||||
Name = "Microsoft.DotNet.Watcher.Tools",
|
||||
Version = ConstantPackageVersions.AspNetToolsVersion } },
|
||||
{ "Microsoft.Extensions.SecretManager.Tools", new PackageDependencyInfo {
|
||||
Name = "Microsoft.Extensions.SecretManager.Tools",
|
||||
Version = ConstantPackageVersions.AspNetToolsVersion } },
|
||||
{ "Microsoft.AspNetCore.Server.IISIntegration.Tools", null}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -108,7 +108,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
new PackageDependencyInfo
|
||||
{
|
||||
Name = PackageConstants.TestSdkPackageName,
|
||||
Version = PackageConstants.TestSdkPackageVersion
|
||||
Version = ConstantPackageVersions.TestSdkPackageVersion
|
||||
}),
|
||||
noFrameworkPackageReferenceItemGroup,
|
||||
mergeExisting: false);
|
||||
|
@ -120,7 +120,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
new PackageDependencyInfo
|
||||
{
|
||||
Name = PackageConstants.XUnitPackageName,
|
||||
Version = PackageConstants.XUnitPackageVersion
|
||||
Version = ConstantPackageVersions.XUnitPackageVersion
|
||||
}),
|
||||
noFrameworkPackageReferenceItemGroup,
|
||||
mergeExisting: false);
|
||||
|
@ -130,7 +130,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
new PackageDependencyInfo
|
||||
{
|
||||
Name = PackageConstants.XUnitRunnerPackageName,
|
||||
Version = PackageConstants.XUnitRunnerPackageVersion
|
||||
Version = ConstantPackageVersions.XUnitRunnerPackageVersion
|
||||
}),
|
||||
noFrameworkPackageReferenceItemGroup,
|
||||
mergeExisting: false);
|
||||
|
@ -186,7 +186,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
_transformApplicator.Execute(
|
||||
ToolTransform().Transform(ToPackageDependencyInfo(
|
||||
tool,
|
||||
PackageConstants.AspProjectToolsPackages)),
|
||||
PackageConstants.ProjectToolPackages)),
|
||||
itemGroup,
|
||||
mergeExisting: true);
|
||||
}
|
||||
|
@ -243,7 +243,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
_transformApplicator.Execute(
|
||||
transform.Transform(ToPackageDependencyInfo(
|
||||
packageDependency,
|
||||
PackageConstants.AspProjectDependencyToolsPackages)),
|
||||
PackageConstants.ProjectDependencyPackages)),
|
||||
itemGroup,
|
||||
mergeExisting: true);
|
||||
}
|
||||
|
@ -251,22 +251,23 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
|
||||
private PackageDependencyInfo ToPackageDependencyInfo(
|
||||
ProjectLibraryDependency dependency,
|
||||
IDictionary<string, string> toolsDictionary)
|
||||
IDictionary<string, PackageDependencyInfo> dependencyToVersionMap)
|
||||
{
|
||||
var name = dependency.Name;
|
||||
var version = dependency.LibraryRange?.VersionRange?.OriginalString;
|
||||
|
||||
if (toolsDictionary.ContainsKey(name))
|
||||
if (dependencyToVersionMap.ContainsKey(name))
|
||||
{
|
||||
name = toolsDictionary[name];
|
||||
version = ConstantPackageVersions.AspNetToolsVersion;
|
||||
|
||||
if(string.IsNullOrEmpty(name))
|
||||
var dependencyInfo = dependencyToVersionMap[name];
|
||||
if (dependencyInfo == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
name = dependencyInfo.Name;
|
||||
version = dependencyInfo.Version;
|
||||
}
|
||||
|
||||
return new PackageDependencyInfo
|
||||
{
|
||||
Name = name,
|
||||
|
@ -391,12 +392,5 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
"PackageTargetFallback",
|
||||
t => $"$(PackageTargetFallback);{string.Join(";", t.Imports)}",
|
||||
t => t.Imports.OrEmptyIfNull().Any());
|
||||
|
||||
private class PackageDependencyInfo
|
||||
{
|
||||
public string Name {get; set;}
|
||||
public string Version {get; set;}
|
||||
public string PrivateAssets {get; set;}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
[InlineData("Microsoft.EntityFrameworkCore.Tools", "Microsoft.EntityFrameworkCore.Tools", ConstantPackageVersions.AspNetToolsVersion)]
|
||||
[InlineData("Microsoft.AspNetCore.Razor.Tools", "Microsoft.AspNetCore.Razor.Design", ConstantPackageVersions.AspNetToolsVersion)]
|
||||
[InlineData("Microsoft.AspNetCore.Razor.Design", "Microsoft.AspNetCore.Razor.Design", ConstantPackageVersions.AspNetToolsVersion)]
|
||||
[InlineData("Microsoft.VisualStudio.Web.CodeGenerators.Mvc", "Microsoft.VisualStudio.Web.CodGeneration.Design", ConstantPackageVersions.AspNetToolsVersion)]
|
||||
[InlineData("Microsoft.VisualStudio.Web.CodeGenerators.Mvc", "Microsoft.VisualStudio.Web.CodeGeneration.Design", ConstantPackageVersions.AspNetToolsVersion)]
|
||||
public void It_migrates_project_dependencies_to_a_new_name_and_version(
|
||||
string sourceToolName,
|
||||
string targetToolName,
|
||||
|
|
|
@ -273,7 +273,9 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
}");
|
||||
|
||||
mockProj.Items.Should().ContainSingle(
|
||||
i => (i.Include == "Microsoft.NET.Test.Sdk" && i.ItemType == "PackageReference"));
|
||||
i => (i.Include == "Microsoft.NET.Test.Sdk" &&
|
||||
i.ItemType == "PackageReference" &&
|
||||
i.GetMetadataWithName("Version").Value == "15.0.0-preview-20161024-02"));
|
||||
|
||||
mockProj.Items.Should().NotContain(
|
||||
i => (i.Include == "xunit" && i.ItemType == "PackageReference"));
|
||||
|
@ -297,13 +299,52 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
}");
|
||||
|
||||
mockProj.Items.Should().ContainSingle(
|
||||
i => (i.Include == "Microsoft.NET.Test.Sdk" && i.ItemType == "PackageReference"));
|
||||
i => (i.Include == "Microsoft.NET.Test.Sdk" &&
|
||||
i.ItemType == "PackageReference" &&
|
||||
i.GetMetadataWithName("Version").Value == "15.0.0-preview-20161024-02"));
|
||||
|
||||
mockProj.Items.Should().ContainSingle(
|
||||
i => (i.Include == "xunit" && i.ItemType == "PackageReference"));
|
||||
i => (i.Include == "xunit" &&
|
||||
i.ItemType == "PackageReference" &&
|
||||
i.GetMetadataWithName("Version").Value == "2.2.0-beta3-build3402"));
|
||||
|
||||
mockProj.Items.Should().ContainSingle(
|
||||
i => (i.Include == "xunit.runner.visualstudio" && i.ItemType == "PackageReference"));
|
||||
i => (i.Include == "xunit.runner.visualstudio" &&
|
||||
i.ItemType == "PackageReference" &&
|
||||
i.GetMetadataWithName("Version").Value == "2.2.0-beta4-build1188"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void It_migrates_test_projects_to_have_test_sdk_and_xunit_packagedependencies_overwrite_existing_packagedependencies()
|
||||
{
|
||||
var mockProj = RunPackageDependenciesRuleOnPj(@"
|
||||
{
|
||||
""buildOptions"": {
|
||||
""emitEntryPoint"": true
|
||||
},
|
||||
""dependencies"": {
|
||||
""xunit"": ""2.2.0-beta3-build3330""
|
||||
},
|
||||
""frameworks"": {
|
||||
""netcoreapp1.0"": {}
|
||||
},
|
||||
""testRunner"": ""xunit""
|
||||
}");
|
||||
|
||||
mockProj.Items.Should().ContainSingle(
|
||||
i => (i.Include == "Microsoft.NET.Test.Sdk" &&
|
||||
i.ItemType == "PackageReference" &&
|
||||
i.GetMetadataWithName("Version").Value == "15.0.0-preview-20161024-02"));
|
||||
|
||||
mockProj.Items.Should().ContainSingle(
|
||||
i => (i.Include == "xunit" &&
|
||||
i.ItemType == "PackageReference" &&
|
||||
i.GetMetadataWithName("Version").Value == "2.2.0-beta3-build3402"));
|
||||
|
||||
mockProj.Items.Should().ContainSingle(
|
||||
i => (i.Include == "xunit.runner.visualstudio" &&
|
||||
i.ItemType == "PackageReference" &&
|
||||
i.GetMetadataWithName("Version").Value == "2.2.0-beta4-build1188"));
|
||||
}
|
||||
|
||||
private void EmitsPackageReferences(ProjectRootElement mockProj, params Tuple<string, string, string>[] packageSpecs)
|
||||
|
|
Loading…
Reference in a new issue