From 155ef033f2429541886f88aa606c6be317493255 Mon Sep 17 00:00:00 2001 From: Justin Goshi Date: Tue, 1 Nov 2016 12:10:51 -1000 Subject: [PATCH] Migrate: auto-inject test dependency version (#4565) * Migrate: auto-injected dependencies should overwrite existing dependency version if present * Address PR comments --- .../ConstantPackageVersions.cs | 3 + .../PackageConstants.cs | 65 ++++++++++++++----- .../MigratePackageDependenciesAndToolsRule.cs | 32 ++++----- .../GivenThatIWantToMigrateAspNetTools.cs | 2 +- ...enThatIWantToMigratePackageDependencies.cs | 49 ++++++++++++-- 5 files changed, 111 insertions(+), 40 deletions(-) diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/ConstantPackageVersions.cs b/src/Microsoft.DotNet.ProjectJsonMigration/ConstantPackageVersions.cs index 3071fb81d..6a0597418 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/ConstantPackageVersions.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/ConstantPackageVersions.cs @@ -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"; } } \ No newline at end of file diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/PackageConstants.cs b/src/Microsoft.DotNet.ProjectJsonMigration/PackageConstants.cs index a4822a176..f897d09be 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/PackageConstants.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/PackageConstants.cs @@ -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 AspProjectDependencyToolsPackages = new Dictionary { - {"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 ProjectDependencyPackages = + new Dictionary { + {"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 AspProjectToolsPackages = new Dictionary { - {"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 ProjectToolPackages = + new Dictionary { + {"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} }; } } \ No newline at end of file diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePackageDependenciesAndToolsRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePackageDependenciesAndToolsRule.cs index ab203101d..a9f04e13c 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePackageDependenciesAndToolsRule.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePackageDependenciesAndToolsRule.cs @@ -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 toolsDictionary) + IDictionary 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;} - } } } diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateAspNetTools.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateAspNetTools.cs index ec4423710..7702dcd71 100644 --- a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateAspNetTools.cs +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateAspNetTools.cs @@ -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, diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigratePackageDependencies.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigratePackageDependencies.cs index 50066736f..0fd10e329 100644 --- a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigratePackageDependencies.cs +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigratePackageDependencies.cs @@ -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[] packageSpecs)