From f1a52d03ac2ef0612be0867ba8cf79dbedc03b1a Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Tue, 1 Dec 2020 14:26:18 -0800 Subject: [PATCH 01/30] Fix 'dotnet add package' with user supplied source feed works if no version is specified. --- .../AddPackageReferenceCommandRunner.cs | 8 ++++---- .../Utility/AddPackageCommandUtility.cs | 14 +++++++++++++- .../AddPackageCommandUtilityTests.cs | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index ed0025d6a9a..e4944a56dc5 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -105,13 +105,13 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, PackageDependency packageDependency = default; if (packageReferenceArgs.NoVersion) { - var latestVersion = await GetLatestVersionAsync(originalPackageSpec, packageReferenceArgs.PackageId, packageReferenceArgs.Logger, packageReferenceArgs.Prerelease); + var latestVersion = await GetLatestVersionAsync(originalPackageSpec, packageReferenceArgs.Sources, packageReferenceArgs.PackageId, packageReferenceArgs.Logger, packageReferenceArgs.Prerelease); if (latestVersion == null) { if (!packageReferenceArgs.Prerelease) { - latestVersion = await GetLatestVersionAsync(originalPackageSpec, packageReferenceArgs.PackageId, packageReferenceArgs.Logger, !packageReferenceArgs.Prerelease); + latestVersion = await GetLatestVersionAsync(originalPackageSpec, packageReferenceArgs.Sources, packageReferenceArgs.PackageId, packageReferenceArgs.Logger, !packageReferenceArgs.Prerelease); if (latestVersion != null) { throw new CommandException(string.Format(CultureInfo.CurrentCulture, Strings.PrereleaseVersionsAvailable, latestVersion)); @@ -232,9 +232,9 @@ private static string GetAliasForFramework(PackageSpec spec, NuGetFramework fram return spec.TargetFrameworks.Where(e => e.FrameworkName.Equals(framework)).FirstOrDefault()?.TargetAlias; } - public static async Task GetLatestVersionAsync(PackageSpec originalPackageSpec, string packageId, ILogger logger, bool prerelease) + public static async Task GetLatestVersionAsync(PackageSpec originalPackageSpec, string[] additionalSources, string packageId, ILogger logger, bool prerelease) { - IList sources = AddPackageCommandUtility.EvaluateSources(originalPackageSpec.RestoreMetadata.Sources, originalPackageSpec.RestoreMetadata.ConfigFilePaths); + IList sources = AddPackageCommandUtility.EvaluateSources(originalPackageSpec.RestoreMetadata.Sources, additionalSources, originalPackageSpec.RestoreMetadata.ConfigFilePaths); return await AddPackageCommandUtility.GetLatestVersionFromSourcesAsync(sources, logger, packageId, prerelease); } diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs index 78ae900102d..8ba074334e4 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs @@ -88,9 +88,10 @@ public static async Task GetLatestVersionFromSourceAsync(PackageSo /// Returns the PackageSource with its credentials if available /// /// Sources to match + /// Additional user supplied source feeds as argument /// Config to use for credentials /// Return a list of package sources - public static List EvaluateSources(IList requestedSources, IList configFilePaths) + public static List EvaluateSources(IList requestedSources, string[] additionalSources, IList configFilePaths) { using (var settingsLoadingContext = new SettingsLoadingContext()) { @@ -100,6 +101,17 @@ public static List EvaluateSources(IList requested var packageSourceProvider = new PackageSourceProvider(settings); IEnumerable packageProviderSources = packageSourceProvider.LoadPackageSources(); + if (additionalSources?.Any() ?? false) + { + foreach (string additionalSource in additionalSources) + { + if (!string.IsNullOrWhiteSpace(additionalSource)) + { + packageSources.Add(new PackageSource(additionalSource)); + } + } + } + for (int i = 0; i < requestedSources.Count; i++) { PackageSource matchedSource = packageProviderSources.FirstOrDefault(e => e.Source == requestedSources[i].Source); diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/AddPackageCommandUtilityTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/AddPackageCommandUtilityTests.cs index b4aeddde949..cc9b36dc486 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/AddPackageCommandUtilityTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/AddPackageCommandUtilityTests.cs @@ -46,7 +46,7 @@ public void EvaluateSources_GivenConfigWithCredentials_ReturnsPackageSourceWithC var settingsLoadContext = new SettingsLoadingContext(); var settings = Settings.LoadImmutableSettingsGivenConfigPaths(new string[] { configPath }, settingsLoadContext); - var result = AddPackageCommandUtility.EvaluateSources(source, settings.GetConfigFilePaths()); + var result = AddPackageCommandUtility.EvaluateSources(source, null, settings.GetConfigFilePaths()); // Asert Assert.Equal(2, result.Count); From b1994d9b013e587c95744995624f4eca17719206 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Tue, 1 Dec 2020 14:49:33 -0800 Subject: [PATCH 02/30] Prevent from user enter duplicate source from cli. --- .../Utility/AddPackageCommandUtility.cs | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs index 8ba074334e4..93117ba89b0 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs @@ -101,17 +101,6 @@ public static List EvaluateSources(IList requested var packageSourceProvider = new PackageSourceProvider(settings); IEnumerable packageProviderSources = packageSourceProvider.LoadPackageSources(); - if (additionalSources?.Any() ?? false) - { - foreach (string additionalSource in additionalSources) - { - if (!string.IsNullOrWhiteSpace(additionalSource)) - { - packageSources.Add(new PackageSource(additionalSource)); - } - } - } - for (int i = 0; i < requestedSources.Count; i++) { PackageSource matchedSource = packageProviderSources.FirstOrDefault(e => e.Source == requestedSources[i].Source); @@ -125,6 +114,32 @@ public static List EvaluateSources(IList requested } } + HashSet currentPackagesSources = packageSources.Select(p => p.Source).ToHashSet(StringComparer.OrdinalIgnoreCase); + + if (additionalSources?.Any() ?? false) + { + foreach (string additionalSource in additionalSources) + { + if (string.IsNullOrWhiteSpace(additionalSource) || currentPackagesSources.Contains(additionalSource)) + { + continue; + } + + PackageSource matchedSource = packageProviderSources.FirstOrDefault(e => e.Source == additionalSource); + + if (matchedSource == null) + { + packageSources.Add(new PackageSource(additionalSource)); + } + else + { + packageSources.Add(matchedSource); + } + + currentPackagesSources.Add(additionalSource); + } + } + return packageSources; } } From 5ebec22be78f4ee2e3b4d08d1134b7bec2adf6b2 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 2 Dec 2020 17:28:30 -0800 Subject: [PATCH 03/30] Add unit tests for 'dotnet pack Package' command with user supplied source feed. --- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 98 ++++++++++++++++++- .../NuGet.XPlat.FuncTest/XPlatTestUtils.cs | 6 ++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index acf4cd7595d..0bce0d22a54 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -7,16 +7,17 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using FluentAssertions; using Microsoft.Extensions.CommandLineUtils; using Moq; using NuGet.CommandLine.XPlat; -using NuGet.CommandLine.XPlat.Utility; using NuGet.Commands; using NuGet.Common; using NuGet.Configuration; using NuGet.Packaging; using NuGet.Packaging.Core; using NuGet.Test.Utility; +using NuGet.Versioning; using Xunit; namespace NuGet.XPlat.FuncTest @@ -745,6 +746,101 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } + [Fact] + public async Task AddPkg_WithAdditionalSourceFeed_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks); + var mySourcePath = Path.Combine(pathContext.WorkingDirectory, "MySource"); + + TestDirectory.Create(mySourcePath); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + mySourcePath, + PackageSaveMode.Defaultv3, + packageX); + + string originalPackageXNupkg = Path.Combine(mySourcePath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); + string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); + + // We're testing case we have only Nupkg file created at top. + File.Move(originalPackageXNupkg, mySourcePackageXNupkg); + + // Remove other files from source + XPlatTestUtils.DeleteDirectory(Path.Combine(mySourcePath, packageX.Id)); + + // Since user is not inputing a version, it is converted to a " * " in the command + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, "*", + projectA, + sources: mySourcePath); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(0, result); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX.Id); + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(NuGetVersion.Parse(packageX.Version))); + } + } + + [Fact] + public async Task AddPkg_WithAdditionalSourceFeed_VersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks); + var mySourcePath = Path.Combine(pathContext.WorkingDirectory, "MySource"); + + TestDirectory.Create(mySourcePath); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + mySourcePath, + PackageSaveMode.Defaultv3, + packageX); + + string originalPackageXNupkg = Path.Combine(mySourcePath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); + string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); + + // We're testing case we have only Nupkg file created at top. + File.Move(originalPackageXNupkg, mySourcePackageXNupkg); + + // Remove other files from source + XPlatTestUtils.DeleteDirectory(Path.Combine(mySourcePath, packageX.Id)); + + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, packageX.Version, + projectA, + sources: mySourcePath); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(0, result); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX.Id); + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(NuGetVersion.Parse(packageX.Version))); + } + } + [Theory] [InlineData("net46", "net46; netcoreapp1.0", "net46")] [InlineData("net46; netcoreapp1.0", "net46; netcoreapp1.0", "net46")] diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs index a4e919d5f54..03622972e34 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs @@ -429,5 +429,11 @@ public static string GetReferenceType(PackageType packageType) return referenceType; } + + public static void DeleteDirectory(string path) + { + DirectoryInfo di = new DirectoryInfo(path); + di.Delete(true); + } } } From f2e4d5dd419f478fd98b320c2d0d6d77e8f57ffa Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 3 Dec 2020 07:36:15 -0800 Subject: [PATCH 04/30] Fix test breaking on Linux. --- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index 0bce0d22a54..69ddf7bf8e3 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -769,7 +769,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); // We're testing case we have only Nupkg file created at top. - File.Move(originalPackageXNupkg, mySourcePackageXNupkg); + File.Copy(originalPackageXNupkg, mySourcePackageXNupkg); // Remove other files from source XPlatTestUtils.DeleteDirectory(Path.Combine(mySourcePath, packageX.Id)); @@ -817,7 +817,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); // We're testing case we have only Nupkg file created at top. - File.Move(originalPackageXNupkg, mySourcePackageXNupkg); + File.Copy(originalPackageXNupkg, mySourcePackageXNupkg); // Remove other files from source XPlatTestUtils.DeleteDirectory(Path.Combine(mySourcePath, packageX.Id)); From 29632d9ba9c1fe577cbf27556cb06dd13d2d529c Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 3 Dec 2020 09:26:24 -0800 Subject: [PATCH 05/30] 2nd attempt to fix unit test failing on Linux. --- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index 69ddf7bf8e3..0bef7a6f9d5 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -768,8 +768,16 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( string originalPackageXNupkg = Path.Combine(mySourcePath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); + if (!File.Exists(originalPackageXNupkg)) + { + // On linux it's created different place. + string[] nupkgFiles = Directory.GetFiles(mySourcePath, $"*.nupkg", SearchOption.AllDirectories); + Assert.True(nupkgFiles.Count() == 1); + originalPackageXNupkg = nupkgFiles[0]; + } + // We're testing case we have only Nupkg file created at top. - File.Copy(originalPackageXNupkg, mySourcePackageXNupkg); + File.Move(originalPackageXNupkg, mySourcePackageXNupkg); // Remove other files from source XPlatTestUtils.DeleteDirectory(Path.Combine(mySourcePath, packageX.Id)); @@ -816,8 +824,16 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( string originalPackageXNupkg = Path.Combine(mySourcePath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); + if (!File.Exists(originalPackageXNupkg)) + { + // On linux it's created different place. + string[] nupkgFiles = Directory.GetFiles(mySourcePath, $"*.nupkg", SearchOption.AllDirectories); + Assert.True(nupkgFiles.Count() == 1); + originalPackageXNupkg = nupkgFiles[0]; + } + // We're testing case we have only Nupkg file created at top. - File.Copy(originalPackageXNupkg, mySourcePackageXNupkg); + File.Move(originalPackageXNupkg, mySourcePackageXNupkg); // Remove other files from source XPlatTestUtils.DeleteDirectory(Path.Combine(mySourcePath, packageX.Id)); From d810d92d15cc0cd37a120741c106bc1b62f13ce2 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 3 Dec 2020 13:51:15 -0800 Subject: [PATCH 06/30] Fix failing Linux unit test3. --- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index 0bef7a6f9d5..4e19a1d3744 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -755,23 +755,24 @@ public async Task AddPkg_WithAdditionalSourceFeed_NoVersionSpecified_Success() var packageFrameworks = "net472; netcoreapp2.0"; var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); var mySourcePath = Path.Combine(pathContext.WorkingDirectory, "MySource"); - TestDirectory.Create(mySourcePath); + Directory.CreateDirectory(mySourcePath); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( - mySourcePath, + packageXPath, PackageSaveMode.Defaultv3, packageX); - string originalPackageXNupkg = Path.Combine(mySourcePath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); + string originalPackageXNupkg = Path.Combine(packageXPath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); if (!File.Exists(originalPackageXNupkg)) { - // On linux it's created different place. - string[] nupkgFiles = Directory.GetFiles(mySourcePath, $"*.nupkg", SearchOption.AllDirectories); + // On linux it's created in different place. + string[] nupkgFiles = Directory.GetFiles(packageXPath, $"*.nupkg", SearchOption.AllDirectories); Assert.True(nupkgFiles.Count() == 1); originalPackageXNupkg = nupkgFiles[0]; } @@ -779,8 +780,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // We're testing case we have only Nupkg file created at top. File.Move(originalPackageXNupkg, mySourcePackageXNupkg); - // Remove other files from source - XPlatTestUtils.DeleteDirectory(Path.Combine(mySourcePath, packageX.Id)); + // Remove packageXPath directory to prove we're not using nuget package structured folders. + XPlatTestUtils.DeleteDirectory(packageXPath); // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, "*", @@ -811,23 +812,24 @@ public async Task AddPkg_WithAdditionalSourceFeed_VersionSpecified_Success() var packageFrameworks = "net472; netcoreapp2.0"; var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); var mySourcePath = Path.Combine(pathContext.WorkingDirectory, "MySource"); - TestDirectory.Create(mySourcePath); + Directory.CreateDirectory(mySourcePath); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( - mySourcePath, + packageXPath, PackageSaveMode.Defaultv3, packageX); - string originalPackageXNupkg = Path.Combine(mySourcePath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); + string originalPackageXNupkg = Path.Combine(packageXPath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); if (!File.Exists(originalPackageXNupkg)) { - // On linux it's created different place. - string[] nupkgFiles = Directory.GetFiles(mySourcePath, $"*.nupkg", SearchOption.AllDirectories); + // On linux it's created in different place. + string[] nupkgFiles = Directory.GetFiles(packageXPath, $"*.nupkg", SearchOption.AllDirectories); Assert.True(nupkgFiles.Count() == 1); originalPackageXNupkg = nupkgFiles[0]; } @@ -835,8 +837,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // We're testing case we have only Nupkg file created at top. File.Move(originalPackageXNupkg, mySourcePackageXNupkg); - // Remove other files from source - XPlatTestUtils.DeleteDirectory(Path.Combine(mySourcePath, packageX.Id)); + // Remove packageXPath directory to prove we're not using nuget package structured folders. + XPlatTestUtils.DeleteDirectory(packageXPath); var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, packageX.Version, projectA, From 35c2426df8cabee7e44a45633fe15e88287892f3 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 3 Dec 2020 13:56:16 -0800 Subject: [PATCH 07/30] Remove unneeded utility method. --- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 8 ++++++-- .../NuGet.XPlat.FuncTest/XPlatTestUtils.cs | 6 ------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index 4e19a1d3744..82c6b450695 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -781,7 +781,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( File.Move(originalPackageXNupkg, mySourcePackageXNupkg); // Remove packageXPath directory to prove we're not using nuget package structured folders. - XPlatTestUtils.DeleteDirectory(packageXPath); + var failedDeletes = new List(); + LocalResourceUtils.DeleteDirectoryTree(packageXPath, failedDeletes); // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, "*", @@ -796,6 +797,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Assert Assert.Equal(0, result); + Assert.True(failedDeletes.Count() == 0); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX.Id); @@ -838,7 +840,8 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( File.Move(originalPackageXNupkg, mySourcePackageXNupkg); // Remove packageXPath directory to prove we're not using nuget package structured folders. - XPlatTestUtils.DeleteDirectory(packageXPath); + var failedDeletes = new List(); + LocalResourceUtils.DeleteDirectoryTree(packageXPath, failedDeletes); var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, packageX.Version, projectA, @@ -852,6 +855,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Assert Assert.Equal(0, result); + Assert.True(failedDeletes.Count() == 0); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX.Id); diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs index 03622972e34..a4e919d5f54 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs @@ -429,11 +429,5 @@ public static string GetReferenceType(PackageType packageType) return referenceType; } - - public static void DeleteDirectory(string path) - { - DirectoryInfo di = new DirectoryInfo(path); - di.Delete(true); - } } } From 528f0cd8e36d8144c25e619f387ab1333433f733 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 4 Dec 2020 16:09:29 -0800 Subject: [PATCH 08/30] Address PR review by Nikolche. --- .../NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs index 93117ba89b0..64fa180e33a 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs @@ -116,7 +116,7 @@ public static List EvaluateSources(IList requested HashSet currentPackagesSources = packageSources.Select(p => p.Source).ToHashSet(StringComparer.OrdinalIgnoreCase); - if (additionalSources?.Any() ?? false) + if (additionalSources?.Any() == true) { foreach (string additionalSource in additionalSources) { From c2c9692133af03fd3e50d1cf9fb7c6080da2ef16 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 4 Dec 2020 16:42:35 -0800 Subject: [PATCH 09/30] Implemented V2 unget source feed instead of V3 source feed for unit tests. --- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 147 ++++++++++-------- 1 file changed, 86 insertions(+), 61 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index 82c6b450695..e0a7923878f 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -752,42 +752,21 @@ public async Task AddPkg_WithAdditionalSourceFeed_NoVersionSpecified_Success() using (var pathContext = new SimpleTestPathContext()) { var projectFrameworks = "net472"; - var packageFrameworks = "net472; netcoreapp2.0"; var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); - var mySourcePath = Path.Combine(pathContext.WorkingDirectory, "MySource"); - - Directory.CreateDirectory(mySourcePath); // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packageXPath, - PackageSaveMode.Defaultv3, - packageX); - - string originalPackageXNupkg = Path.Combine(packageXPath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); - string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); - - if (!File.Exists(originalPackageXNupkg)) - { - // On linux it's created in different place. - string[] nupkgFiles = Directory.GetFiles(packageXPath, $"*.nupkg", SearchOption.AllDirectories); - Assert.True(nupkgFiles.Count() == 1); - originalPackageXNupkg = nupkgFiles[0]; - } - - // We're testing case we have only Nupkg file created at top. - File.Move(originalPackageXNupkg, mySourcePackageXNupkg); - - // Remove packageXPath directory to prove we're not using nuget package structured folders. - var failedDeletes = new List(); - LocalResourceUtils.DeleteDirectoryTree(packageXPath, failedDeletes); + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); // Since user is not inputing a version, it is converted to a " * " in the command - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, "*", + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: mySourcePath); + sources: packageXPath); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -797,55 +776,68 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Assert Assert.Equal(0, result); - Assert.True(failedDeletes.Count() == 0); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX.Id); - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(NuGetVersion.Parse(packageX.Version))); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); } } [Fact] - public async Task AddPkg_WithAdditionalSourceFeed_VersionSpecified_Success() + public async Task AddPkg_WithAdditionalSourceFeed_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { var projectFrameworks = "net472"; - var packageFrameworks = "net472; netcoreapp2.0"; var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); - var mySourcePath = Path.Combine(pathContext.WorkingDirectory, "MySource"); - Directory.CreateDirectory(mySourcePath); + // Generate Package, but packageX is not in v2 nuget feed. + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageY_V1 }); - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packageXPath, - PackageSaveMode.Defaultv3, - packageX); + // Since user is not inputing a version, it is converted to a " * " in the command + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", + projectA, + sources: packageXPath); - string originalPackageXNupkg = Path.Combine(packageXPath, packageX.Id, packageX.Version, $"{packageX.Id}.{ packageX.Version}.nupkg"); - string mySourcePackageXNupkg = Path.Combine(mySourcePath, $"{packageX.Id}.{ packageX.Version}.nupkg"); + var commandRunner = new AddPackageReferenceCommandRunner(); - if (!File.Exists(originalPackageXNupkg)) - { - // On linux it's created in different place. - string[] nupkgFiles = Directory.GetFiles(packageXPath, $"*.nupkg", SearchOption.AllDirectories); - Assert.True(nupkgFiles.Count() == 1); - originalPackageXNupkg = nupkgFiles[0]; - } + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(1, result); + } + } - // We're testing case we have only Nupkg file created at top. - File.Move(originalPackageXNupkg, mySourcePackageXNupkg); + [Fact] + public async Task AddPkg_WithAdditionalSourceFeed_VersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); - // Remove packageXPath directory to prove we're not using nuget package structured folders. - var failedDeletes = new List(); - LocalResourceUtils.DeleteDirectoryTree(packageXPath, failedDeletes); + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, packageX.Version, + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), projectA, - sources: mySourcePath); + sources: packageXPath); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -855,11 +847,44 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Assert Assert.Equal(0, result); - Assert.True(failedDeletes.Count() == 0); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX.Id); - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(NuGetVersion.Parse(packageX.Version))); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); + } + } + + [Fact] + public async Task AddPkg_WithAdditionalSourceFeed_VersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); + + // Generate Package, but V3 is not in V2 feed. + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), + projectA, + sources: packageXPath); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(1, result); } } From b5ab7cabe3ac44ee2a7cb4f69c52fd4910b953a2 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Tue, 8 Dec 2020 08:21:44 -0800 Subject: [PATCH 10/30] Correct conflicting FindLocalPackagesResourceV2Provider, FindLocalPackagesResourceV3Provider vs LocalV2FindPackageByIdResourceProvider, LocalV3FindPackageByIdResourceProvider. Discovery of version and actual resolution of version had different logics. --- .../LocalRepositories/FindLocalPackagesResourceV2Provider.cs | 2 +- .../LocalRepositories/FindLocalPackagesResourceV3Provider.cs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV2Provider.cs b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV2Provider.cs index 45a95d7d728..e830b73fcab 100644 --- a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV2Provider.cs +++ b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV2Provider.cs @@ -21,7 +21,7 @@ public override async Task> TryCreate(SourceReposito var feedType = await source.GetFeedType(token); if (feedType == FeedType.FileSystemV2 - || feedType == FeedType.FileSystemUnknown) + || feedType == FeedType.FileSystemUnzipped) { curResource = new FindLocalPackagesResourceV2(source.PackageSource.Source); } diff --git a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV3Provider.cs b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV3Provider.cs index 1809a1c2a04..1859f2793b4 100644 --- a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV3Provider.cs +++ b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV3Provider.cs @@ -18,8 +18,10 @@ public FindLocalPackagesResourceV3Provider() public override async Task> TryCreate(SourceRepository source, CancellationToken token) { FindLocalPackagesResource curResource = null; + var feedType = await source.GetFeedType(token); - if (await source.GetFeedType(token) == FeedType.FileSystemV3) + if (feedType == FeedType.FileSystemV3 + || feedType == FeedType.FileSystemUnknown) { curResource = new FindLocalPackagesResourceV3(source.PackageSource.Source); } From cbd7a3632590aadbed16a8e64127f60f490463f6 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 9 Dec 2020 08:32:24 -0800 Subject: [PATCH 11/30] Fix relative path is not discovered when resolving package from local. --- .../AddPackageReferenceCommandRunner.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index e4944a56dc5..5a7dbb3a863 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -102,6 +102,17 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, var originalPackageSpec = matchingPackageSpecs.FirstOrDefault(); + // Convert relative path to absolute path if there is any + if (packageReferenceArgs.Sources?.Any() == true) + { + var projectDirectory = Path.GetDirectoryName(packageReferenceArgs.ProjectPath); + + for (int i = 0; i < packageReferenceArgs.Sources.Length; i++) + { + packageReferenceArgs.Sources[i] = UriUtility.GetAbsolutePath(projectDirectory, packageReferenceArgs.Sources[i]); + } + } + PackageDependency packageDependency = default; if (packageReferenceArgs.NoVersion) { From 1a9398ea2c12d5a263ebc47b4979467d8673e32c Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 9 Dec 2020 13:43:22 -0800 Subject: [PATCH 12/30] Add more unit test covering all possible scenarios. --- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 474 +++++++++++++++++- 1 file changed, 464 insertions(+), 10 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index e0a7923878f..ed12ad60d33 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -747,7 +747,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithAdditionalSourceFeed_NoVersionSpecified_Success() + public async Task AddPkg_WithV2AdditionalSourceFeed_AbsolutePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -756,7 +756,7 @@ public async Task AddPkg_WithAdditionalSourceFeed_NoVersionSpecified_Success() var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -785,7 +785,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithAdditionalSourceFeed_NoVersionSpecified_Fail() + public async Task AddPkg_WithV2AdditionalSourceFeed_AbsolutePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -795,9 +795,9 @@ public async Task AddPkg_WithAdditionalSourceFeed_NoVersionSpecified_Fail() var packageY = "packageY"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - // Generate Package, but packageX is not in v2 nuget feed. + // Generate Package, but packageX is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( packageXPath, //not using solution source folder new PackageIdentity[] { packageY_V1 }); @@ -819,7 +819,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithAdditionalSourceFeed_VersionSpecified_Success() + public async Task AddPkg_WithV2AdditionalSourceFeed_AbsolutePath_VersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -828,7 +828,7 @@ public async Task AddPkg_WithAdditionalSourceFeed_VersionSpecified_Success() var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -856,7 +856,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithAdditionalSourceFeed_VersionSpecified_Fail() + public async Task AddPkg_WithV2AdditionalSourceFeed_AbsolutePath_VersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -866,9 +866,9 @@ public async Task AddPkg_WithAdditionalSourceFeed_VersionSpecified_Fail() var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "packageX"); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - // Generate Package, but V3 is not in V2 feed. + // Generate Package, but V3 is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( packageXPath, //not using solution source folder new PackageIdentity[] { packageX_V1, packageX_V2 }); @@ -888,6 +888,460 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } } + [Fact] + public async Task AddPkg_WithV3AdditionalSourceFeed_AbsolutePath_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packagesPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + // Since user is not inputing a version, it is converted to a " * " in the command + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", + projectA, + sources: packagesPath); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(0, result); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + } + } + + [Fact] + public async Task AddPkg_WithV3AdditionalSourceFeed_AbsolutePath_NoVersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, frameworkString: packageFrameworks); + var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY, frameworkString: packageFrameworks); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packagesPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageY_V1_Context }); + + // Since user is not inputing a version, it is converted to a " * " in the command + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX, "*", + projectA, + sources: packagesPath); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(1, result); + } + } + + [Fact] + public async Task AddPkg_WithV3AdditionalSourceFeed_AbsolutePath_VersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packagesPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), + projectA, + sources: packagesPath); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(0, result); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); + } + } + + [Fact] + public async Task AddPkg_WithV3AdditionalSourceFeed_AbsolutePath_VersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packagesPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), + projectA, + sources: packagesPath); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(1, result); + } + } + + [Fact] + public async Task AddPkg_WithV2AdditionalSourceFeed_RelativePath_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + // Since user is not inputing a version, it is converted to a " * " in the command + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", + projectA, + sources: "..\\..\\Custompackages"); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(0, result); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + } + } + + [Fact] + public async Task AddPkg_WithV2AdditionalSourceFeed_RelativePath_NoVersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package, but packageX is not in nuget feed. + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageY_V1 }); + + // Since user is not inputing a version, it is converted to a " * " in the command + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", + projectA, + sources: "..\\..\\Custompackages"); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(1, result); + } + } + + [Fact] + public async Task AddPkg_WithV2AdditionalSourceFeed_RelativePath_VersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), + projectA, + sources: "..\\..\\Custompackages"); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(0, result); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); + } + } + + [Fact] + public async Task AddPkg_WithV2AdditionalSourceFeed_RelativePath_VersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package, but V3 is not in nuget feed. + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), + projectA, + sources: "..\\..\\Custompackages"); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(1, result); + } + } + + [Fact] + public async Task AddPkg_WithV3AdditionalSourceFeed_RelativePath_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packagesPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + // Since user is not inputing a version, it is converted to a " * " in the command + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", + projectA, + sources: "..\\..\\Custompackages"); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(0, result); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + } + } + + [Fact] + public async Task AddPkg_WithV3AdditionalSourceFeed_RelativePath_NoVersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, frameworkString: packageFrameworks); + var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY, frameworkString: packageFrameworks); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packagesPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageY_V1_Context }); + + // Since user is not inputing a version, it is converted to a " * " in the command + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX, "*", + projectA, + sources: "..\\..\\Custompackages"); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(1, result); + } + } + + [Fact] + public async Task AddPkg_WithV3AdditionalSourceFeed_RelativePath_VersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packagesPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), + projectA, + sources: "..\\..\\Custompackages"); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(0, result); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); + } + } + + [Fact] + public async Task AddPkg_WithV3AdditionalSourceFeed_RelativePath_VersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packagesPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), + projectA, + sources: "..\\..\\Custompackages"); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Act + var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) + .Result; + + // Assert + Assert.Equal(1, result); + } + } + [Theory] [InlineData("net46", "net46; netcoreapp1.0", "net46")] [InlineData("net46; netcoreapp1.0", "net46; netcoreapp1.0", "net46")] From 586e3ab6db09892b67e7487496dea5e33c2ab451 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 9 Dec 2020 18:10:38 -0800 Subject: [PATCH 13/30] Fix Linux unit tests. --- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index ed12ad60d33..c8308ae665b 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -1064,7 +1064,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: "..\\..\\Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1103,7 +1103,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: "..\\..\\Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1135,7 +1135,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), projectA, - sources: "..\\..\\Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1173,7 +1173,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), projectA, - sources: "..\\..\\Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1210,7 +1210,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: "..\\..\\Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1251,7 +1251,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX, "*", projectA, - sources: "..\\..\\Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1287,7 +1287,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), projectA, - sources: "..\\..\\Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1329,7 +1329,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), projectA, - sources: "..\\..\\Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); var commandRunner = new AddPackageReferenceCommandRunner(); From a87d7f2c5a3243cca3386a9049319b42e9f7e873 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 18 Dec 2020 10:06:42 -0800 Subject: [PATCH 14/30] Remove unneeded changes. --- .../AddPackageReferenceCommandRunner.cs | 8 ++--- .../Utility/AddPackageCommandUtility.cs | 29 +------------------ .../AddPackageCommandUtilityTests.cs | 2 +- 3 files changed, 6 insertions(+), 33 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index 5a7dbb3a863..fc18d9b6145 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -116,13 +116,13 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, PackageDependency packageDependency = default; if (packageReferenceArgs.NoVersion) { - var latestVersion = await GetLatestVersionAsync(originalPackageSpec, packageReferenceArgs.Sources, packageReferenceArgs.PackageId, packageReferenceArgs.Logger, packageReferenceArgs.Prerelease); + var latestVersion = await GetLatestVersionAsync(originalPackageSpec, packageReferenceArgs.PackageId, packageReferenceArgs.Logger, packageReferenceArgs.Prerelease); if (latestVersion == null) { if (!packageReferenceArgs.Prerelease) { - latestVersion = await GetLatestVersionAsync(originalPackageSpec, packageReferenceArgs.Sources, packageReferenceArgs.PackageId, packageReferenceArgs.Logger, !packageReferenceArgs.Prerelease); + latestVersion = await GetLatestVersionAsync(originalPackageSpec, packageReferenceArgs.PackageId, packageReferenceArgs.Logger, !packageReferenceArgs.Prerelease); if (latestVersion != null) { throw new CommandException(string.Format(CultureInfo.CurrentCulture, Strings.PrereleaseVersionsAvailable, latestVersion)); @@ -243,9 +243,9 @@ private static string GetAliasForFramework(PackageSpec spec, NuGetFramework fram return spec.TargetFrameworks.Where(e => e.FrameworkName.Equals(framework)).FirstOrDefault()?.TargetAlias; } - public static async Task GetLatestVersionAsync(PackageSpec originalPackageSpec, string[] additionalSources, string packageId, ILogger logger, bool prerelease) + public static async Task GetLatestVersionAsync(PackageSpec originalPackageSpec, string packageId, ILogger logger, bool prerelease) { - IList sources = AddPackageCommandUtility.EvaluateSources(originalPackageSpec.RestoreMetadata.Sources, additionalSources, originalPackageSpec.RestoreMetadata.ConfigFilePaths); + IList sources = AddPackageCommandUtility.EvaluateSources(originalPackageSpec.RestoreMetadata.Sources, originalPackageSpec.RestoreMetadata.ConfigFilePaths); return await AddPackageCommandUtility.GetLatestVersionFromSourcesAsync(sources, logger, packageId, prerelease); } diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs index 64fa180e33a..78ae900102d 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs @@ -88,10 +88,9 @@ public static async Task GetLatestVersionFromSourceAsync(PackageSo /// Returns the PackageSource with its credentials if available /// /// Sources to match - /// Additional user supplied source feeds as argument /// Config to use for credentials /// Return a list of package sources - public static List EvaluateSources(IList requestedSources, string[] additionalSources, IList configFilePaths) + public static List EvaluateSources(IList requestedSources, IList configFilePaths) { using (var settingsLoadingContext = new SettingsLoadingContext()) { @@ -114,32 +113,6 @@ public static List EvaluateSources(IList requested } } - HashSet currentPackagesSources = packageSources.Select(p => p.Source).ToHashSet(StringComparer.OrdinalIgnoreCase); - - if (additionalSources?.Any() == true) - { - foreach (string additionalSource in additionalSources) - { - if (string.IsNullOrWhiteSpace(additionalSource) || currentPackagesSources.Contains(additionalSource)) - { - continue; - } - - PackageSource matchedSource = packageProviderSources.FirstOrDefault(e => e.Source == additionalSource); - - if (matchedSource == null) - { - packageSources.Add(new PackageSource(additionalSource)); - } - else - { - packageSources.Add(matchedSource); - } - - currentPackagesSources.Add(additionalSource); - } - } - return packageSources; } } diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/AddPackageCommandUtilityTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/AddPackageCommandUtilityTests.cs index cc9b36dc486..b4aeddde949 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/AddPackageCommandUtilityTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/AddPackageCommandUtilityTests.cs @@ -46,7 +46,7 @@ public void EvaluateSources_GivenConfigWithCredentials_ReturnsPackageSourceWithC var settingsLoadContext = new SettingsLoadingContext(); var settings = Settings.LoadImmutableSettingsGivenConfigPaths(new string[] { configPath }, settingsLoadContext); - var result = AddPackageCommandUtility.EvaluateSources(source, null, settings.GetConfigFilePaths()); + var result = AddPackageCommandUtility.EvaluateSources(source, settings.GetConfigFilePaths()); // Asert Assert.Equal(2, result.Count); From 7a8f91c546638f6ee4956f52b42651fc318eb6c8 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Mon, 21 Dec 2020 07:54:36 -0800 Subject: [PATCH 15/30] Add passed sources into dgSpec sources. --- .../AddPackageReferenceCommandRunner.cs | 22 +++++++------- .../Utility/AddPackageCommandUtility.cs | 29 +++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index fc18d9b6145..58ab9df8ef7 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -61,6 +61,17 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, return 0; } + // Convert relative path to absolute path if there is any + if (packageReferenceArgs.Sources?.Any() == true) + { + var projectDirectory = Path.GetDirectoryName(packageReferenceArgs.ProjectPath); + + for (int i = 0; i < packageReferenceArgs.Sources.Length; i++) + { + packageReferenceArgs.Sources[i] = UriUtility.GetAbsolutePath(projectDirectory, packageReferenceArgs.Sources[i]); + } + } + // 1. Get project dg file packageReferenceArgs.Logger.LogDebug("Reading project Dependency Graph"); var dgSpec = ReadProjectDependencyGraph(packageReferenceArgs); @@ -102,16 +113,7 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, var originalPackageSpec = matchingPackageSpecs.FirstOrDefault(); - // Convert relative path to absolute path if there is any - if (packageReferenceArgs.Sources?.Any() == true) - { - var projectDirectory = Path.GetDirectoryName(packageReferenceArgs.ProjectPath); - - for (int i = 0; i < packageReferenceArgs.Sources.Length; i++) - { - packageReferenceArgs.Sources[i] = UriUtility.GetAbsolutePath(projectDirectory, packageReferenceArgs.Sources[i]); - } - } + AddPackageCommandUtility.UpdateSourceFeeds(originalPackageSpec, packageReferenceArgs.Sources); PackageDependency packageDependency = default; if (packageReferenceArgs.NoVersion) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs index 78ae900102d..f43fbe6367c 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using NuGet.Common; using NuGet.Configuration; +using NuGet.ProjectModel; using NuGet.Protocol; using NuGet.Protocol.Core.Types; using NuGet.Versioning; @@ -116,5 +117,33 @@ public static List EvaluateSources(IList requested return packageSources; } } + + /// + /// Updates package sources + /// + /// Package spec which may need update source feed + /// New user supplied source feeds as argument + public static void UpdateSourceFeeds(PackageSpec packageSpec, string[] newSources) + { + HashSet currentPackagesSources = packageSpec.RestoreMetadata.Sources.Select(p => p.Source).ToHashSet(StringComparer.OrdinalIgnoreCase); + List newUniqueSources = new List(); + + if (newSources?.Any() == true) + { + foreach (string newSource in newSources) + { + if (string.IsNullOrWhiteSpace(newSource) || currentPackagesSources.Contains(newSource)) + { + continue; + } + + newUniqueSources.Add(newSource); + } + } + + List updatedSources = new List(packageSpec.RestoreMetadata.Sources); + updatedSources.AddRange(newUniqueSources.Select(ns => new PackageSource(ns))); + packageSpec.RestoreMetadata.Sources = updatedSources; + } } } From 6deb7397c7fb28c1fc56a40f0d2805297cd3fe72 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Mon, 21 Dec 2020 11:34:58 -0800 Subject: [PATCH 16/30] Refactor code: Instead of add source we replace source in dgSpec file. --- .../Utility/AddPackageCommandUtility.cs | 18 +-- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 115 ++++++++++++++++++ .../NuGet.XPlat.FuncTest/XPlatTestUtils.cs | 18 ++- 3 files changed, 132 insertions(+), 19 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs index f43fbe6367c..95d2644e032 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs @@ -125,25 +125,11 @@ public static List EvaluateSources(IList requested /// New user supplied source feeds as argument public static void UpdateSourceFeeds(PackageSpec packageSpec, string[] newSources) { - HashSet currentPackagesSources = packageSpec.RestoreMetadata.Sources.Select(p => p.Source).ToHashSet(StringComparer.OrdinalIgnoreCase); - List newUniqueSources = new List(); - if (newSources?.Any() == true) { - foreach (string newSource in newSources) - { - if (string.IsNullOrWhiteSpace(newSource) || currentPackagesSources.Contains(newSource)) - { - continue; - } - - newUniqueSources.Add(newSource); - } + packageSpec.RestoreMetadata.Sources = + newSources.Where(ns => !string.IsNullOrEmpty(ns)).ToHashSet(StringComparer.OrdinalIgnoreCase).Select(ns => new PackageSource(ns)).ToList(); } - - List updatedSources = new List(packageSpec.RestoreMetadata.Sources); - updatedSources.AddRange(newUniqueSources.Select(ns => new PackageSource(ns))); - packageSpec.RestoreMetadata.Sources = updatedSources; } } } diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index c8308ae665b..cf81b7bf4b8 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -16,6 +16,7 @@ using NuGet.Configuration; using NuGet.Packaging; using NuGet.Packaging.Core; +using NuGet.ProjectModel; using NuGet.Test.Utility; using NuGet.Versioning; using Xunit; @@ -777,6 +778,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Assert Assert.Equal(0, result); + // Make sure source is replaced in generated dgSpec file. + PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); + Assert.Equal(packageSpecs.Count(), 1); + Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); // Should resolve to highest available version. @@ -848,6 +854,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Assert Assert.Equal(0, result); + // Make sure source is replaced in generated dgSpec file. + PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); + Assert.Equal(packageSpecs.Count(), 1); + Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); // Should resolve to specified version. @@ -923,6 +934,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Assert Assert.Equal(0, result); + // Make sure source is replaced in generated dgSpec file. + PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); + Assert.Equal(packageSpecs.Count(), 1); + Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); // Should resolve to highest available version. @@ -1000,6 +1016,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Assert Assert.Equal(0, result); + // Make sure source is replaced in generated dgSpec file. + PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); + Assert.Equal(packageSpecs.Count(), 1); + Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); // Should resolve to specified version. @@ -1075,6 +1096,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Assert Assert.Equal(0, result); + // Make sure source is replaced in generated dgSpec file. + PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); + Assert.Equal(packageSpecs.Count(), 1); + Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); // Should resolve to highest available version. @@ -1146,6 +1172,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Assert Assert.Equal(0, result); + // Make sure source is replaced in generated dgSpec file. + PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); + Assert.Equal(packageSpecs.Count(), 1); + Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); // Should resolve to specified version. @@ -1221,6 +1252,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Assert Assert.Equal(0, result); + // Make sure source is replaced in generated dgSpec file. + PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); + Assert.Equal(packageSpecs.Count(), 1); + Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); // Should resolve to highest available version. @@ -1298,6 +1334,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Assert Assert.Equal(0, result); + // Make sure source is replaced in generated dgSpec file. + PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); + Assert.Equal(packageSpecs.Count(), 1); + Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); // Should resolve to specified version. @@ -1342,6 +1383,80 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } + [Fact] + public async Task AddPkg_WithMixed_V2_V3_AdditionalSourceFeeds_AbsolutePath_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectFrameworks = "net472"; + var packageFrameworks = "net472; netcoreapp2.0"; + var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); + var packageY_V2 = new PackageIdentity(packageY, new NuGetVersion("2.0.0")); + var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageY_V2_Context = XPlatTestUtils.CreatePackage(packageY_V2.Id, packageY_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesX"); + var packageYPath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesY"); + + // Generate V2 Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + // Generate V3 Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packageYPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageY_V1_Context, packageY_V2_Context }); + + var commandRunner = new AddPackageReferenceCommandRunner(); + + // Single source + // Since user is not inputing a version, it is converted to a " * " in the command + var packageArgsX = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", + projectA, + sources: packageXPath); + + // Act + var resultX = commandRunner.ExecuteCommand(packageArgsX, MsBuild) + .Result; + + // Multiple sources + // Since user is not inputing a version, it is converted to a " * " in the command + PackageReferenceArgs packageArgsY = XPlatTestUtils.GetPackageReferenceArgs(packageY_V1.Id, "*", + projectA, + sources: $"{packageXPath};{packageYPath}", + dgFilePath: Path.Combine(projectA.ProjectExtensionsPath, $"{projectA.ProjectName}.csproj.nuget.dgspec.json") + ); + + var resultY = commandRunner.ExecuteCommand(packageArgsY, MsBuild) + .Result; + + // Assert + Assert.Equal(0, resultX); + Assert.Equal(0, resultY); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); + Assert.Equal(packageSpecs.Count(), 1); + string[] sources = packageSpecs[0].RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 2); + sources.Should().Contain(e => e.Contains("CustompackagesX")); + sources.Should().Contain(e => e.Contains("CustompackagesY")); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageY); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageY_V2.Version)); + } + } + [Theory] [InlineData("net46", "net46; netcoreapp1.0", "net46")] [InlineData("net46; netcoreapp1.0", "net46; netcoreapp1.0", "net46")] diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs index a4e919d5f54..75ff658de0b 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs @@ -219,14 +219,15 @@ internal static PackageReferenceArgs GetPackageReferenceArgs(string packageId, S } internal static PackageReferenceArgs GetPackageReferenceArgs(string packageId, string packageVersion, SimpleTestProjectContext project, - string frameworks = "", string packageDirectory = "", string sources = "", bool noRestore = false, bool noVersion = false, bool prerelease = false) + string frameworks = "", string packageDirectory = "", string sources = "", bool noRestore = false, bool noVersion = false, bool prerelease = false, string dgFilePath = "") { var logger = new TestCommandOutputLogger(); - var dgFilePath = string.Empty; - if (!noRestore) + + if (string.IsNullOrEmpty(dgFilePath) && !noRestore) { dgFilePath = CreateDGFileForProject(project); } + return new PackageReferenceArgs(project.ProjectPath, logger) { Frameworks = MSBuildStringUtility.Split(frameworks), @@ -429,5 +430,16 @@ public static string GetReferenceType(PackageType packageType) return referenceType; } + + internal static PackageSpec[] GetPackageSpecs(SimpleTestProjectContext project) + { + DependencyGraphSpec dgSpec = DependencyGraphSpec.Load(Path.Combine(project.ProjectExtensionsPath, $"{project.ProjectName}.csproj.nuget.dgspec.json")); + + return dgSpec + .Projects + .Where(p => p.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference && + PathUtility.GetStringComparerBasedOnOS().Equals(Path.GetFullPath(p.RestoreMetadata.ProjectPath), project.ProjectPath)) + .ToArray(); + } } } From 3a1b6f1f175c8a851ce826f8e5bcfd89b4b955b4 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Mon, 21 Dec 2020 15:07:42 -0800 Subject: [PATCH 17/30] Revert other feed provider problem fix which need to go in seperate PR. --- .../LocalRepositories/FindLocalPackagesResourceV2Provider.cs | 2 +- .../LocalRepositories/FindLocalPackagesResourceV3Provider.cs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV2Provider.cs b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV2Provider.cs index e830b73fcab..45a95d7d728 100644 --- a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV2Provider.cs +++ b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV2Provider.cs @@ -21,7 +21,7 @@ public override async Task> TryCreate(SourceReposito var feedType = await source.GetFeedType(token); if (feedType == FeedType.FileSystemV2 - || feedType == FeedType.FileSystemUnzipped) + || feedType == FeedType.FileSystemUnknown) { curResource = new FindLocalPackagesResourceV2(source.PackageSource.Source); } diff --git a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV3Provider.cs b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV3Provider.cs index 1859f2793b4..1809a1c2a04 100644 --- a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV3Provider.cs +++ b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/FindLocalPackagesResourceV3Provider.cs @@ -18,10 +18,8 @@ public FindLocalPackagesResourceV3Provider() public override async Task> TryCreate(SourceRepository source, CancellationToken token) { FindLocalPackagesResource curResource = null; - var feedType = await source.GetFeedType(token); - if (feedType == FeedType.FileSystemV3 - || feedType == FeedType.FileSystemUnknown) + if (await source.GetFeedType(token) == FeedType.FileSystemV3) { curResource = new FindLocalPackagesResourceV3(source.PackageSource.Source); } From 86a56b82a674365d519752992fa6e7ba0ba3ee2f Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Mon, 21 Dec 2020 16:20:50 -0800 Subject: [PATCH 18/30] Address code review comments by Nikolche. --- .../AddPackageReferenceCommandRunner.cs | 40 ++++++++++++------- .../Utility/AddPackageCommandUtility.cs | 15 ------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index 58ab9df8ef7..7c731d56b90 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -61,17 +61,6 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, return 0; } - // Convert relative path to absolute path if there is any - if (packageReferenceArgs.Sources?.Any() == true) - { - var projectDirectory = Path.GetDirectoryName(packageReferenceArgs.ProjectPath); - - for (int i = 0; i < packageReferenceArgs.Sources.Length; i++) - { - packageReferenceArgs.Sources[i] = UriUtility.GetAbsolutePath(projectDirectory, packageReferenceArgs.Sources[i]); - } - } - // 1. Get project dg file packageReferenceArgs.Logger.LogDebug("Reading project Dependency Graph"); var dgSpec = ReadProjectDependencyGraph(packageReferenceArgs); @@ -113,7 +102,28 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, var originalPackageSpec = matchingPackageSpecs.FirstOrDefault(); - AddPackageCommandUtility.UpdateSourceFeeds(originalPackageSpec, packageReferenceArgs.Sources); + // Convert relative path to absolute path if there is any + List sourcePaths; + + if (packageReferenceArgs.Sources?.Any() == true) + { + sourcePaths = new List(); + string projectDirectory = Path.GetDirectoryName(packageReferenceArgs.ProjectPath); + + foreach (string source in packageReferenceArgs.Sources) + { + sourcePaths.Add(UriUtility.GetAbsolutePath(projectDirectory, source)); + } + + originalPackageSpec.RestoreMetadata.Sources = + sourcePaths.Where(ns => !string.IsNullOrEmpty(ns)) + .Select(ns => new PackageSource(ns)) + .ToList(); + } + else + { + sourcePaths = packageReferenceArgs.Sources?.ToList(); + } PackageDependency packageDependency = default; if (packageReferenceArgs.NoVersion) @@ -162,7 +172,7 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, packageReferenceArgs.Logger.LogDebug("Running Restore preview"); var restorePreviewResult = await PreviewAddPackageReferenceAsync(packageReferenceArgs, - updatedDgSpec); + updatedDgSpec, sourcePaths); packageReferenceArgs.Logger.LogDebug("Restore Review completed"); @@ -321,7 +331,7 @@ private static LibraryDependency GenerateLibraryDependency( } private static async Task PreviewAddPackageReferenceAsync(PackageReferenceArgs packageReferenceArgs, - DependencyGraphSpec dgSpec) + DependencyGraphSpec dgSpec, List sourcePaths) { // Set user agent and connection settings. XPlatUtility.ConfigureProtocol(); @@ -347,7 +357,7 @@ private static async Task PreviewAddPackageReferenceAsync(Pac MachineWideSettings = new XPlatMachineWideSetting(), GlobalPackagesFolder = packageReferenceArgs.PackageDirectory, PreLoadedRequestProviders = providers, - Sources = packageReferenceArgs.Sources?.ToList() + Sources = sourcePaths }; // Generate Restore Requests. There will always be 1 request here since we are restoring for 1 project. diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs index 95d2644e032..78ae900102d 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/AddPackageCommandUtility.cs @@ -8,7 +8,6 @@ using System.Threading.Tasks; using NuGet.Common; using NuGet.Configuration; -using NuGet.ProjectModel; using NuGet.Protocol; using NuGet.Protocol.Core.Types; using NuGet.Versioning; @@ -117,19 +116,5 @@ public static List EvaluateSources(IList requested return packageSources; } } - - /// - /// Updates package sources - /// - /// Package spec which may need update source feed - /// New user supplied source feeds as argument - public static void UpdateSourceFeeds(PackageSpec packageSpec, string[] newSources) - { - if (newSources?.Any() == true) - { - packageSpec.RestoreMetadata.Sources = - newSources.Where(ns => !string.IsNullOrEmpty(ns)).ToHashSet(StringComparer.OrdinalIgnoreCase).Select(ns => new PackageSource(ns)).ToList(); - } - } } } From f4224c2f29cfb65db57861d59009f2bfecaeb973 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Mon, 21 Dec 2020 22:46:16 -0800 Subject: [PATCH 19/30] Address latest code review comment --- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 141 ++++++++---------- .../NuGet.XPlat.FuncTest/XPlatTestUtils.cs | 11 -- 2 files changed, 61 insertions(+), 91 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index cf81b7bf4b8..401aac12e69 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -748,7 +748,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSourceFeed_AbsolutePath_NoVersionSpecified_Success() + public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -772,16 +772,15 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(0, result); // Make sure source is replaced in generated dgSpec file. - PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); - Assert.Equal(packageSpecs.Count(), 1); - Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -791,7 +790,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSourceFeed_AbsolutePath_NoVersionSpecified_Fail() + public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -816,8 +815,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(1, result); @@ -825,7 +823,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSourceFeed_AbsolutePath_VersionSpecified_Success() + public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_VersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -848,16 +846,15 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(0, result); // Make sure source is replaced in generated dgSpec file. - PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); - Assert.Equal(packageSpecs.Count(), 1); - Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -867,7 +864,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSourceFeed_AbsolutePath_VersionSpecified_Fail() + public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_VersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -891,8 +888,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(1, result); @@ -900,7 +896,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSourceFeed_AbsolutePath_NoVersionSpecified_Success() + public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -928,16 +924,15 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(0, result); // Make sure source is replaced in generated dgSpec file. - PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); - Assert.Equal(packageSpecs.Count(), 1); - Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -947,7 +942,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSourceFeed_AbsolutePath_NoVersionSpecified_Fail() + public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -974,8 +969,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(1, result); @@ -983,7 +977,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSourceFeed_AbsolutePath_VersionSpecified_Success() + public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_VersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -1010,16 +1004,15 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(0, result); // Make sure source is replaced in generated dgSpec file. - PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); - Assert.Equal(packageSpecs.Count(), 1); - Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -1029,7 +1022,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSourceFeed_AbsolutePath_VersionSpecified_Fail() + public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_VersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -1057,8 +1050,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(1, result); @@ -1066,7 +1058,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSourceFeed_RelativePath_NoVersionSpecified_Success() + public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -1090,16 +1082,15 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(0, result); // Make sure source is replaced in generated dgSpec file. - PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); - Assert.Equal(packageSpecs.Count(), 1); - Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -1109,7 +1100,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSourceFeed_RelativePath_NoVersionSpecified_Fail() + public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -1134,8 +1125,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(1, result); @@ -1143,7 +1133,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSourceFeed_RelativePath_VersionSpecified_Success() + public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -1166,16 +1156,15 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(0, result); // Make sure source is replaced in generated dgSpec file. - PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); - Assert.Equal(packageSpecs.Count(), 1); - Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -1185,7 +1174,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSourceFeed_RelativePath_VersionSpecified_Fail() + public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -1209,8 +1198,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(1, result); @@ -1218,7 +1206,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSourceFeed_RelativePath_NoVersionSpecified_Success() + public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -1246,16 +1234,15 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(0, result); // Make sure source is replaced in generated dgSpec file. - PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); - Assert.Equal(packageSpecs.Count(), 1); - Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -1265,7 +1252,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSourceFeed_RelativePath_NoVersionSpecified_Fail() + public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -1292,8 +1279,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(1, result); @@ -1301,7 +1287,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSourceFeed_RelativePath_VersionSpecified_Success() + public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -1328,16 +1314,15 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(0, result); // Make sure source is replaced in generated dgSpec file. - PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); - Assert.Equal(packageSpecs.Count(), 1); - Assert.True(packageSpecs[0].RestoreMetadata.Sources[0].Name.Contains("Custompackages")); + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -1347,7 +1332,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSourceFeed_RelativePath_VersionSpecified_Fail() + public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -1375,8 +1360,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var commandRunner = new AddPackageReferenceCommandRunner(); // Act - var result = commandRunner.ExecuteCommand(packageArgs, MsBuild) - .Result; + var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); // Assert Assert.Equal(1, result); @@ -1384,7 +1368,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithMixed_V2_V3_AdditionalSourceFeeds_AbsolutePath_NoVersionSpecified_Success() + public async Task AddPkg_WithMixed_V2_V3_AdditionalSources_AbsolutePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -1422,8 +1406,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( sources: packageXPath); // Act - var resultX = commandRunner.ExecuteCommand(packageArgsX, MsBuild) - .Result; + var resultX = await commandRunner.ExecuteCommand(packageArgsX, MsBuild); // Multiple sources // Since user is not inputing a version, it is converted to a " * " in the command @@ -1433,17 +1416,15 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( dgFilePath: Path.Combine(projectA.ProjectExtensionsPath, $"{projectA.ProjectName}.csproj.nuget.dgspec.json") ); - var resultY = commandRunner.ExecuteCommand(packageArgsY, MsBuild) - .Result; + var resultY = await commandRunner.ExecuteCommand(packageArgsY, MsBuild); // Assert Assert.Equal(0, resultX); Assert.Equal(0, resultY); // Make sure source is replaced in generated dgSpec file. - PackageSpec[] packageSpecs = XPlatTestUtils.GetPackageSpecs(projectA); - Assert.Equal(packageSpecs.Count(), 1); - string[] sources = packageSpecs[0].RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); Assert.Equal(sources.Count(), 2); sources.Should().Contain(e => e.Contains("CustompackagesX")); sources.Should().Contain(e => e.Contains("CustompackagesY")); diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs index 75ff658de0b..9cd7fbfb43f 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs @@ -430,16 +430,5 @@ public static string GetReferenceType(PackageType packageType) return referenceType; } - - internal static PackageSpec[] GetPackageSpecs(SimpleTestProjectContext project) - { - DependencyGraphSpec dgSpec = DependencyGraphSpec.Load(Path.Combine(project.ProjectExtensionsPath, $"{project.ProjectName}.csproj.nuget.dgspec.json")); - - return dgSpec - .Projects - .Where(p => p.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference && - PathUtility.GetStringComparerBasedOnOS().Equals(Path.GetFullPath(p.RestoreMetadata.ProjectPath), project.ProjectPath)) - .ToArray(); - } } } From eabc1005d4fb0ee89027cfb830a515e71740cd10 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 31 Dec 2020 22:51:58 -0800 Subject: [PATCH 20/30] Instead of relative to project directory, now resolve sources relative to startup directory. --- .../AddPackageReferenceCommandRunner.cs | 2 +- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 97 ++++++++++++++----- 2 files changed, 72 insertions(+), 27 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index 7c731d56b90..3e18ae70a83 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -112,7 +112,7 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, foreach (string source in packageReferenceArgs.Sources) { - sourcePaths.Add(UriUtility.GetAbsolutePath(projectDirectory, source)); + sourcePaths.Add(UriUtility.GetAbsolutePath(Environment.CurrentDirectory, source)); } originalPackageSpec.RestoreMetadata.Sources = diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index 401aac12e69..dcb28043108 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -757,7 +757,7 @@ public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_NoVersionSpecified_ var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -800,7 +800,7 @@ public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_NoVersionSpecified_ var packageY = "packageY"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package, but packageX is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -832,7 +832,7 @@ public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_VersionSpecified_Su var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -874,7 +874,7 @@ public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_VersionSpecified_Fa var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package, but V3 is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -908,7 +908,7 @@ public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_NoVersionSpecified_ var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -953,7 +953,7 @@ public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_NoVersionSpecified_ var packageY = "packageY"; var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, frameworkString: packageFrameworks); var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY, frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -989,7 +989,7 @@ public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_VersionSpecified_Su var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -1035,7 +1035,7 @@ public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_VersionSpecified_Fa var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -1067,7 +1067,7 @@ public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_ var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -1077,7 +1077,12 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); + + // Set startup directory. + var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); + Directory.CreateDirectory(startUpDirectory); + Environment.CurrentDirectory = startUpDirectory; var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1110,7 +1115,7 @@ public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_ var packageY = "packageY"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package, but packageX is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -1120,7 +1125,12 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); + + // Set startup directory. + var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); + Directory.CreateDirectory(startUpDirectory); + Environment.CurrentDirectory = startUpDirectory; var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1142,7 +1152,7 @@ public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Su var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -1151,7 +1161,12 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); + + // Set startup directory. + var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); + Directory.CreateDirectory(startUpDirectory); + Environment.CurrentDirectory = startUpDirectory; var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1184,7 +1199,7 @@ public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Fa var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package, but V3 is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -1193,7 +1208,12 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); + + // Set startup directory. + var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); + Directory.CreateDirectory(startUpDirectory); + Environment.CurrentDirectory = startUpDirectory; var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1218,7 +1238,7 @@ public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_ var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -1229,7 +1249,12 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); + + // Set startup directory. + var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); + Directory.CreateDirectory(startUpDirectory); + Environment.CurrentDirectory = startUpDirectory; var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1263,7 +1288,7 @@ public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_ var packageY = "packageY"; var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, frameworkString: packageFrameworks); var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY, frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -1274,7 +1299,12 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX, "*", projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); + + // Set startup directory. + var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); + Directory.CreateDirectory(startUpDirectory); + Environment.CurrentDirectory = startUpDirectory; var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1299,7 +1329,7 @@ public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Su var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -1309,7 +1339,12 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); + + // Set startup directory. + var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); + Directory.CreateDirectory(startUpDirectory); + Environment.CurrentDirectory = startUpDirectory; var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1345,7 +1380,7 @@ public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Fa var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -1355,7 +1390,12 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"); + sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); + + // Set startup directory. + var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); + Directory.CreateDirectory(startUpDirectory); + Environment.CurrentDirectory = startUpDirectory; var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1383,8 +1423,8 @@ public async Task AddPkg_WithMixed_V2_V3_AdditionalSources_AbsolutePath_NoVersio var packageY_V2 = new PackageIdentity(packageY, new NuGetVersion("2.0.0")); var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageY_V2_Context = XPlatTestUtils.CreatePackage(packageY_V2.Id, packageY_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesX"); - var packageYPath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesY"); + var packageXPath = Path.Combine(pathContext.PackageSource, "CustompackagesX"); + var packageYPath = Path.Combine(pathContext.PackageSource, "CustompackagesY"); // Generate V2 Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -1397,6 +1437,11 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( PackageSaveMode.Defaultv3, new SimpleTestPackageContext[] { packageY_V1_Context, packageY_V2_Context }); + // Set startup directory. + var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); + Directory.CreateDirectory(startUpDirectory); + Environment.CurrentDirectory = startUpDirectory; + var commandRunner = new AddPackageReferenceCommandRunner(); // Single source From aabcaf5d691dc736ba73d8314a25ef72237ef522 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Tue, 5 Jan 2021 14:36:53 -0800 Subject: [PATCH 21/30] Start moving dotnet relative source tests to dotnet integration tests. --- .../AddPackageReferenceCommandRunner.cs | 7 +-- .../DotnetSourcesTests.cs | 47 ++++++++++++++ .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 63 +++---------------- 3 files changed, 57 insertions(+), 60 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index 3e18ae70a83..1fa16ce6e94 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -103,12 +103,11 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, var originalPackageSpec = matchingPackageSpecs.FirstOrDefault(); // Convert relative path to absolute path if there is any - List sourcePaths; + List sourcePaths = null; if (packageReferenceArgs.Sources?.Any() == true) { sourcePaths = new List(); - string projectDirectory = Path.GetDirectoryName(packageReferenceArgs.ProjectPath); foreach (string source in packageReferenceArgs.Sources) { @@ -120,10 +119,6 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, .Select(ns => new PackageSource(ns)) .ToList(); } - else - { - sourcePaths = packageReferenceArgs.Sources?.ToList(); - } PackageDependency packageDependency = default; if (packageReferenceArgs.NoVersion) diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs index e980c0ae60c..a8c276f4e31 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs @@ -4,9 +4,16 @@ using System; using System.IO; using System.Linq; +using System.Threading.Tasks; using System.Xml.Linq; +using FluentAssertions; +using NuGet.Common; using NuGet.Configuration; +using NuGet.Packaging.Core; +using NuGet.ProjectModel; using NuGet.Test.Utility; +using NuGet.Versioning; +using NuGet.XPlat.FuncTest; using Xunit; namespace Dotnet.Integration.Test @@ -453,6 +460,46 @@ public void TestCommandInvalidArguments(string command, int badCommandIndex) } } + [Theory] + [InlineData("net5.0")] + public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Success(string targetFrameworks) + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); + var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\source\\Custompackages" : "../../source/Custompackages"; + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); + + LockFileTarget ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + } + } + /// /// Utility for asserting faulty executions of dotnet.exe /// diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index dcb28043108..382983345ac 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -757,7 +757,7 @@ public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_NoVersionSpecified_ var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -800,7 +800,7 @@ public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_NoVersionSpecified_ var packageY = "packageY"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package, but packageX is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -832,7 +832,7 @@ public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_VersionSpecified_Su var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -874,7 +874,7 @@ public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_VersionSpecified_Fa var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package, but V3 is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -908,7 +908,7 @@ public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_NoVersionSpecified_ var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -953,7 +953,7 @@ public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_NoVersionSpecified_ var packageY = "packageY"; var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, frameworkString: packageFrameworks); var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY, frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -989,7 +989,7 @@ public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_VersionSpecified_Su var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -1035,7 +1035,7 @@ public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_VersionSpecified_Fa var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); + var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( @@ -1057,52 +1057,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } - [Fact] - public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - // Since user is not inputing a version, it is converted to a " * " in the command - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", - projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); - - // Set startup directory. - var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); - Directory.CreateDirectory(startUpDirectory); - Environment.CurrentDirectory = startUpDirectory; - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(0, result); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.True(sources[0].Contains("Custompackages")); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to highest available version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - } - } + [Fact] public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Fail() From a901f57ab2e0f36fb8a39acf03f624d59f4c9b97 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Tue, 5 Jan 2021 16:17:06 -0800 Subject: [PATCH 22/30] Move another dotnet relative source test into dotnet.integration.test --- .../DotnetSourcesTests.cs | 36 +- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 381 ------------------ 2 files changed, 34 insertions(+), 383 deletions(-) diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs index a8c276f4e31..b3d34c61335 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs @@ -471,8 +471,8 @@ public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_ var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); - var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\source\\Custompackages" : "../../source/Custompackages"; + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( @@ -500,6 +500,38 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } } + + [Theory] + [InlineData("net5.0")] + public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Fail(string targetFrameworks) + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; + + // Generate Package, but packageX is not in nuget feed. + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageY_V1 }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); + + // Assert + // It should fail because there is no packageX, only packageY in source directory. + result.Success.Should().BeFalse(because: result.AllOutput); + } + } + /// /// Utility for asserting faulty executions of dotnet.exe /// diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index 382983345ac..648316fd862 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -1057,387 +1057,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } - - - [Fact] - public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Fail() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageY = "packageY"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); - - // Generate Package, but packageX is not in nuget feed. - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageY_V1 }); - - // Since user is not inputing a version, it is converted to a " * " in the command - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", - projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); - - // Set startup directory. - var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); - Directory.CreateDirectory(startUpDirectory); - Environment.CurrentDirectory = startUpDirectory; - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(1, result); - } - } - - [Fact] - public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), - projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); - - // Set startup directory. - var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); - Directory.CreateDirectory(startUpDirectory); - Environment.CurrentDirectory = startUpDirectory; - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(0, result); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.True(sources[0].Contains("Custompackages")); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to specified version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); - } - } - - [Fact] - public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Fail() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var packageXPath = Path.Combine(pathContext.PackageSource, "Custompackages"); - - // Generate Package, but V3 is not in nuget feed. - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), - projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); - - // Set startup directory. - var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); - Directory.CreateDirectory(startUpDirectory); - Environment.CurrentDirectory = startUpDirectory; - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(1, result); - } - } - - [Fact] - public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var packageFrameworks = "net472; netcoreapp2.0"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packagesPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); - - // Since user is not inputing a version, it is converted to a " * " in the command - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", - projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); - - // Set startup directory. - var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); - Directory.CreateDirectory(startUpDirectory); - Environment.CurrentDirectory = startUpDirectory; - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(0, result); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.True(sources[0].Contains("Custompackages")); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to highest available version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - } - } - - [Fact] - public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_Fail() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var packageFrameworks = "net472; netcoreapp2.0"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageY = "packageY"; - var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, frameworkString: packageFrameworks); - var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY, frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packagesPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageY_V1_Context }); - - // Since user is not inputing a version, it is converted to a " * " in the command - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX, "*", - projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); - - // Set startup directory. - var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); - Directory.CreateDirectory(startUpDirectory); - Environment.CurrentDirectory = startUpDirectory; - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(1, result); - } - } - - [Fact] - public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var packageFrameworks = "net472; netcoreapp2.0"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packagesPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); - - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), - projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); - - // Set startup directory. - var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); - Directory.CreateDirectory(startUpDirectory); - Environment.CurrentDirectory = startUpDirectory; - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(0, result); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.True(sources[0].Contains("Custompackages")); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to specified version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); - } - } - - [Fact] - public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Fail() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var packageFrameworks = "net472; netcoreapp2.0"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.PackageSource, "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packagesPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); - - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), - projectA, - sources: RuntimeEnvironmentHelper.IsWindows ? "..\\source\\Custompackages" : "../source/Custompackages"); - - // Set startup directory. - var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); - Directory.CreateDirectory(startUpDirectory); - Environment.CurrentDirectory = startUpDirectory; - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(1, result); - } - } - - [Fact] - public async Task AddPkg_WithMixed_V2_V3_AdditionalSources_AbsolutePath_NoVersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var packageFrameworks = "net472; netcoreapp2.0"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageY = "packageY"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageY_V2 = new PackageIdentity(packageY, new NuGetVersion("2.0.0")); - var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageY_V2_Context = XPlatTestUtils.CreatePackage(packageY_V2.Id, packageY_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageXPath = Path.Combine(pathContext.PackageSource, "CustompackagesX"); - var packageYPath = Path.Combine(pathContext.PackageSource, "CustompackagesY"); - - // Generate V2 Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - // Generate V3 Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packageYPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageY_V1_Context, packageY_V2_Context }); - - // Set startup directory. - var startUpDirectory = Path.Combine(pathContext.WorkingDirectory, "StartUpDirectory"); - Directory.CreateDirectory(startUpDirectory); - Environment.CurrentDirectory = startUpDirectory; - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Single source - // Since user is not inputing a version, it is converted to a " * " in the command - var packageArgsX = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", - projectA, - sources: packageXPath); - - // Act - var resultX = await commandRunner.ExecuteCommand(packageArgsX, MsBuild); - - // Multiple sources - // Since user is not inputing a version, it is converted to a " * " in the command - PackageReferenceArgs packageArgsY = XPlatTestUtils.GetPackageReferenceArgs(packageY_V1.Id, "*", - projectA, - sources: $"{packageXPath};{packageYPath}", - dgFilePath: Path.Combine(projectA.ProjectExtensionsPath, $"{projectA.ProjectName}.csproj.nuget.dgspec.json") - ); - - var resultY = await commandRunner.ExecuteCommand(packageArgsY, MsBuild); - - // Assert - Assert.Equal(0, resultX); - Assert.Equal(0, resultY); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 2); - sources.Should().Contain(e => e.Contains("CustompackagesX")); - sources.Should().Contain(e => e.Contains("CustompackagesY")); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageY); - // Should resolve to highest available version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageY_V2.Version)); - } - } - [Theory] [InlineData("net46", "net46; netcoreapp1.0", "net46")] [InlineData("net46; netcoreapp1.0", "net46; netcoreapp1.0", "net46")] From 73a31419dd58777e4622f11a883ee115c7fd97ab Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Tue, 5 Jan 2021 17:20:48 -0800 Subject: [PATCH 23/30] Fix empty source passed situation for dotnet add package. --- .../AddPackageReferenceCommandRunner.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index 1fa16ce6e94..cc619761e04 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -103,12 +103,10 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, var originalPackageSpec = matchingPackageSpecs.FirstOrDefault(); // Convert relative path to absolute path if there is any - List sourcePaths = null; + List sourcePaths = new List(); if (packageReferenceArgs.Sources?.Any() == true) { - sourcePaths = new List(); - foreach (string source in packageReferenceArgs.Sources) { sourcePaths.Add(UriUtility.GetAbsolutePath(Environment.CurrentDirectory, source)); From f46d327fdebb145480ce0a33ad7ed30063eff7fd Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 6 Jan 2021 10:00:33 -0800 Subject: [PATCH 24/30] Test additional frameworks --- .../Dotnet.Integration.Test/DotnetSourcesTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs index b3d34c61335..e0052314a08 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs @@ -461,6 +461,10 @@ public void TestCommandInvalidArguments(string command, int badCommandIndex) } [Theory] + [InlineData("netcoreapp2.1")] + [InlineData("netcoreapp3.0")] + [InlineData("netcoreapp3.1")] + [InlineData("net472")] [InlineData("net5.0")] public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Success(string targetFrameworks) { From 3646937341fd09e7bd0ce21e3bfd8b9a751a2482 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 6 Jan 2021 11:24:00 -0800 Subject: [PATCH 25/30] Finish moving dotnet relative source unit tests to dotnet.integration.test --- .../DotnetSourcesTests.cs | 295 +++++++++++++++++- 1 file changed, 291 insertions(+), 4 deletions(-) diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs index e0052314a08..4782bf102f9 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs @@ -9,6 +9,7 @@ using FluentAssertions; using NuGet.Common; using NuGet.Configuration; +using NuGet.Packaging; using NuGet.Packaging.Core; using NuGet.ProjectModel; using NuGet.Test.Utility; @@ -461,10 +462,6 @@ public void TestCommandInvalidArguments(string command, int badCommandIndex) } [Theory] - [InlineData("netcoreapp2.1")] - [InlineData("netcoreapp3.0")] - [InlineData("netcoreapp3.1")] - [InlineData("net472")] [InlineData("net5.0")] public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Success(string targetFrameworks) { @@ -536,6 +533,296 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } } + [Theory] + [InlineData("net5.0")] + public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Success(string targetFrameworks) + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V1.Version}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); + } + } + + [Theory] + [InlineData("net5.0")] + public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Fail(string targetFrameworks) + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V3.Version}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeFalse(because: result.AllOutput); + } + } + + [Theory] + [InlineData("net5.0")] + public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_Success(string targetFrameworks) + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packageXPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + } + } + + [Theory] + [InlineData("net5.0")] + public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_Fail(string targetFrameworks) + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packageXPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageY_V1_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeFalse(because: result.AllOutput); + } + } + + [Theory] + [InlineData("net5.0")] + public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Success(string targetFrameworks) + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packageXPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V1.Version}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.True(sources[0].Contains("Custompackages")); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); + } + } + + [Theory] + [InlineData("net5.0")] + public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Fail(string targetFrameworks) + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packageXPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V2.Version}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeFalse(because: result.AllOutput); + } + } + + [Theory] + [InlineData("net5.0")] + public async Task AddPkg_WithMixed_V2_V3_AdditionalSources_AbsolutePath_NoVersionSpecified_Success(string targetFrameworks) + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); + var packageY_V2 = new PackageIdentity(packageY, new NuGetVersion("2.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageY_V2_Context = XPlatTestUtils.CreatePackage(packageY_V2.Id, packageY_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageXPath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesX"); + var packageYPath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesY"); + var sourceRelativePathX = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\CustompackagesX" : "../../CustompackagesX"; + var sourceRelativePathY = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\CustompackagesY" : "../../CustompackagesY"; + + // Generate V2 Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + packageXPath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + // Generate V3 Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + packageYPath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageY_V1_Context, packageY_V2_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + + // Single source + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePathX}", ignoreExitCode: true); + result.Success.Should().BeTrue(because: result.AllOutput); + + // Multiple sources + result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageY} -s {sourceRelativePathY};{sourceRelativePathX}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 2); + sources.Should().Contain(e => e.Contains("CustompackagesX")); + sources.Should().Contain(e => e.Contains("CustompackagesY")); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + // Should resolve to specified version. + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageY); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageY_V2.Version)); + } + } + /// /// Utility for asserting faulty executions of dotnet.exe /// From f4598c17e72b9aec6afdfae84f469c886b4d6b12 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Wed, 6 Jan 2021 14:09:14 -0800 Subject: [PATCH 26/30] Fix new unit test names. --- .../DotnetSourcesTests.cs | 22 +++++++++++-------- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 20 ++++++++++------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs index 4782bf102f9..750814f7420 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs @@ -463,7 +463,7 @@ public void TestCommandInvalidArguments(string command, int badCommandIndex) [Theory] [InlineData("net5.0")] - public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Success(string targetFrameworks) + public async Task AddPkg_WithV2_RelativePath_NoVersionSpecified_Success(string targetFrameworks) { using (var pathContext = new SimpleTestPathContext()) { @@ -492,6 +492,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Make sure source is replaced in generated dgSpec file. PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); Assert.True(sources[0].Contains("Custompackages")); LockFileTarget ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); @@ -504,7 +505,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( [Theory] [InlineData("net5.0")] - public async Task AddPkg_WithV2AdditionalSource_RelativePath_NoVersionSpecified_Fail(string targetFrameworks) + public async Task AddPkg_WithV2_RelativePath_NoVersionSpecified_Fail(string targetFrameworks) { using (var pathContext = new SimpleTestPathContext()) { @@ -535,7 +536,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( [Theory] [InlineData("net5.0")] - public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Success(string targetFrameworks) + public async Task AddPkg_WithV2_RelativePath_VersionSpecified_Success(string targetFrameworks) { using (var pathContext = new SimpleTestPathContext()) { @@ -564,6 +565,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Make sure source is replaced in generated dgSpec file. PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); @@ -575,7 +577,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( [Theory] [InlineData("net5.0")] - public async Task AddPkg_WithV2AdditionalSource_RelativePath_VersionSpecified_Fail(string targetFrameworks) + public async Task AddPkg_WithV2_RelativePath_VersionSpecified_Fail(string targetFrameworks) { using (var pathContext = new SimpleTestPathContext()) { @@ -606,7 +608,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( [Theory] [InlineData("net5.0")] - public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_Success(string targetFrameworks) + public async Task AddPkg_WithV3_RelativePath_NoVersionSpecified_Success(string targetFrameworks) { using (var pathContext = new SimpleTestPathContext()) { @@ -639,6 +641,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Make sure source is replaced in generated dgSpec file. PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); @@ -650,7 +653,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( [Theory] [InlineData("net5.0")] - public async Task AddPkg_WithV3AdditionalSource_RelativePath_NoVersionSpecified_Fail(string targetFrameworks) + public async Task AddPkg_WithV3_RelativePath_NoVersionSpecified_Fail(string targetFrameworks) { using (var pathContext = new SimpleTestPathContext()) { @@ -683,7 +686,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( [Theory] [InlineData("net5.0")] - public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Success(string targetFrameworks) + public async Task AddPkg_WithV3_RelativePath_VersionSpecified_Success(string targetFrameworks) { using (var pathContext = new SimpleTestPathContext()) { @@ -716,6 +719,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Make sure source is replaced in generated dgSpec file. PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); @@ -727,7 +731,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( [Theory] [InlineData("net5.0")] - public async Task AddPkg_WithV3AdditionalSource_RelativePath_VersionSpecified_Fail(string targetFrameworks) + public async Task AddPkg_WithV3_RelativePath_VersionSpecified_Fail(string targetFrameworks) { using (var pathContext = new SimpleTestPathContext()) { @@ -760,7 +764,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( [Theory] [InlineData("net5.0")] - public async Task AddPkg_WithMixed_V2_V3_AdditionalSources_AbsolutePath_NoVersionSpecified_Success(string targetFrameworks) + public async Task AddPkg_WithMixed_V2_V3_RelativePath_NoVersionSpecified_Success(string targetFrameworks) { using (var pathContext = new SimpleTestPathContext()) { diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index 648316fd862..e3808f03d7a 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -748,7 +748,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_NoVersionSpecified_Success() + public async Task AddPkg_WithV2Source_AbsolutePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -780,6 +780,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Make sure source is replaced in generated dgSpec file. PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); @@ -790,7 +791,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_NoVersionSpecified_Fail() + public async Task AddPkg_WithV2_AbsolutePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -823,7 +824,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_VersionSpecified_Success() + public async Task AddPkg_WithV2_AbsolutePath_VersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -854,6 +855,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( // Make sure source is replaced in generated dgSpec file. PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); @@ -864,7 +866,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2AdditionalSource_AbsolutePath_VersionSpecified_Fail() + public async Task AddPkg_WithV2_AbsolutePath_VersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -896,7 +898,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_NoVersionSpecified_Success() + public async Task AddPkg_WithV3_AbsolutePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -932,6 +934,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Make sure source is replaced in generated dgSpec file. PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); @@ -942,7 +945,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_NoVersionSpecified_Fail() + public async Task AddPkg_WithV3_AbsolutePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -977,7 +980,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_VersionSpecified_Success() + public async Task AddPkg_WithV3_AbsolutePath_VersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -1012,6 +1015,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( // Make sure source is replaced in generated dgSpec file. PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); Assert.True(sources[0].Contains("Custompackages")); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); @@ -1022,7 +1026,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3AdditionalSource_AbsolutePath_VersionSpecified_Fail() + public async Task AddPkg_WithV3_AbsolutePath_VersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { From 4943245fd5d027247dbed40149d2e5ecb721a759 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 7 Jan 2021 10:37:01 -0800 Subject: [PATCH 27/30] Address PR review by Nkolche. --- .../AddPackageReferenceCommandRunner.cs | 14 +- .../DotnetAddPackageTests.cs | 478 ++++++++++++++++++ .../DotnetSourcesTests.cs | 374 -------------- .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 72 +-- 4 files changed, 522 insertions(+), 416 deletions(-) create mode 100644 test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetAddPackageTests.cs diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index cc619761e04..00963ad1af7 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -103,17 +103,19 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, var originalPackageSpec = matchingPackageSpecs.FirstOrDefault(); // Convert relative path to absolute path if there is any - List sourcePaths = new List(); + List sources = null; if (packageReferenceArgs.Sources?.Any() == true) { + sources = new List(); + foreach (string source in packageReferenceArgs.Sources) { - sourcePaths.Add(UriUtility.GetAbsolutePath(Environment.CurrentDirectory, source)); + sources.Add(UriUtility.GetAbsolutePath(Environment.CurrentDirectory, source)); } originalPackageSpec.RestoreMetadata.Sources = - sourcePaths.Where(ns => !string.IsNullOrEmpty(ns)) + sources.Where(ns => !string.IsNullOrEmpty(ns)) .Select(ns => new PackageSource(ns)) .ToList(); } @@ -165,7 +167,7 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, packageReferenceArgs.Logger.LogDebug("Running Restore preview"); var restorePreviewResult = await PreviewAddPackageReferenceAsync(packageReferenceArgs, - updatedDgSpec, sourcePaths); + updatedDgSpec, sources); packageReferenceArgs.Logger.LogDebug("Restore Review completed"); @@ -349,8 +351,8 @@ private static async Task PreviewAddPackageReferenceAsync(Pac Log = packageReferenceArgs.Logger, MachineWideSettings = new XPlatMachineWideSetting(), GlobalPackagesFolder = packageReferenceArgs.PackageDirectory, - PreLoadedRequestProviders = providers, - Sources = sourcePaths + PreLoadedRequestProviders = providers + // Sources : No need to pass it, because SourceRepositories contains the already built SourceRepository objects }; // Generate Restore Requests. There will always be 1 request here since we are restoring for 1 project. diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetAddPackageTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetAddPackageTests.cs new file mode 100644 index 00000000000..c7184c0ccd5 --- /dev/null +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetAddPackageTests.cs @@ -0,0 +1,478 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using FluentAssertions; +using NuGet.Packaging; +using NuGet.Packaging.Core; +using NuGet.ProjectModel; +using NuGet.Test.Utility; +using NuGet.Versioning; +using NuGet.XPlat.FuncTest; +using Xunit; + +namespace Dotnet.Integration.Test +{ + [Collection("Dotnet Integration Tests")] + public class DotnetAddPackageTests + { + private readonly MsbuildIntegrationTestFixture _fixture; + + public DotnetAddPackageTests(MsbuildIntegrationTestFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public async Task AddPkg_V2LocalSourceFeed_WithRelativePath_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + customSourcePath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); + Assert.Equal(sources[0], customSourcePath); + + LockFileTarget ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + } + } + + [Fact] + public async Task AddPkg_V2LocalSourceFeed_WithRelativePath_NoVersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); + + // Generate Package, but packageX is not in nuget feed. + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + customSourcePath, //not using solution source folder + new PackageIdentity[] { packageY_V1 }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); + + // Assert + // It should fail because there is no packageX, only packageY in source directory. + result.Success.Should().BeFalse(because: result.AllOutput); + } + } + + [Fact] + public async Task AddPkg_V2LocalSourceFeed_WithRelativePath_VersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + customSourcePath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V1.Version}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); + Assert.Equal(sources[0], customSourcePath); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified package. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); + } + } + + [Fact] + public async Task AddPkg_V2LocalSourceFeed_WithRelativePath_VersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + customSourcePath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V3.Version}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeFalse(because: result.AllOutput); + } + } + + [Fact] + public async Task AddPkg_V3LocalSourceFeed_WithRelativePath_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + customSourcePath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); + Assert.Equal(sources[0], customSourcePath); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified package. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + } + } + + [Fact] + public async Task AddPkg_V3LocalSourceFeed_WithRelativePath_NoVersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + customSourcePath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageY_V1_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeFalse(because: result.AllOutput); + } + } + + [Fact] + public async Task AddPkg_V3LocalSourceFeed_WithRelativePath_VersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + customSourcePath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V1.Version}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); + Assert.Equal(sources[0], customSourcePath); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to specified package. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); + } + } + + [Fact] + public async Task AddPkg_V3LocalSourceFeed_WithRelativePath_VersionSpecified_Fail() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); + + // Generate Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + customSourcePath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageX_V1_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V2.Version}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeFalse(because: result.AllOutput); + } + } + + [Fact] + public async Task AddPkg_V2_V3_LocalSourceFeeds_WithRelativePath_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageY = "packageY"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); + var packageY_V2 = new PackageIdentity(packageY, new NuGetVersion("2.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageY_V2_Context = XPlatTestUtils.CreatePackage(packageY_V2.Id, packageY_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + var customPackageX_SourcePath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesX"); + var customPackageY_Sourcepath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesY"); + var sourceRelativePathX = Path.Combine("..", "..", "CustompackagesX"); + var sourceRelativePathY = Path.Combine("..", "..", "CustompackagesY"); + + // Generate V2 Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + customPackageX_SourcePath, //not using solution source folder + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + // Generate V3 Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + customPackageY_Sourcepath, + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageY_V1_Context, packageY_V2_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + + // Single source + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePathX}", ignoreExitCode: true); + result.Success.Should().BeTrue(because: result.AllOutput); + + // Multiple sources + result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageY} -s {sourceRelativePathY};{sourceRelativePathX}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 2); + sources.Should().Contain(e => e.Contains(customPackageX_SourcePath)); + sources.Should().Contain(e => e.Contains(customPackageY_Sourcepath)); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + // Should resolve to specified package. + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageY); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageY_V2.Version)); + } + } + + [Fact] + public async Task AddPkg_V2LocalSourceFeed_WithDefaultSolutiuonSource_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageX = "packageX"; + var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); + var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); + + // Generate V2 Package + await SimpleTestPackageUtility.CreateFolderFeedV2Async( + pathContext.PackageSource, // using default solution source folder, not passing source as parameter. + new PackageIdentity[] { packageX_V1, packageX_V2 }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX}", ignoreExitCode: true); + result.Success.Should().BeTrue(because: result.AllOutput); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); + Assert.Equal(sources[0], pathContext.PackageSource); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + // Should resolve to specified package. + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); + } + } + + [Fact] + public async Task AddPkg_V3LocalSourceFeed_WithDefaultSolutiuonSource_NoVersionSpecified_Success() + { + using (var pathContext = new SimpleTestPathContext()) + { + var projectName = "projectA"; + var targetFrameworks = "net5.0"; + SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); + var packageY = "packageY"; + var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); + var packageY_V2 = new PackageIdentity(packageY, new NuGetVersion("2.0.0")); + var packageFrameworks = "net472; netcoreapp2.0"; + var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); + var packageY_V2_Context = XPlatTestUtils.CreatePackage(packageY_V2.Id, packageY_V2.Version.Version.ToString(), frameworkString: packageFrameworks); + + // Generate V3 Package + await SimpleTestPackageUtility.CreateFolderFeedV3Async( + pathContext.PackageSource, // using default solution source folder, not passing source as parameter. + PackageSaveMode.Defaultv3, + new SimpleTestPackageContext[] { packageY_V1_Context, packageY_V2_Context }); + + var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); + var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); + + // Act + CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageY}", ignoreExitCode: true); + + // Assert + result.Success.Should().BeTrue(because: result.AllOutput); + + // Make sure source is replaced in generated dgSpec file. + PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; + string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); + Assert.Equal(sources.Count(), 1); + Assert.Equal(sources[0], pathContext.PackageSource); + + var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); + // Should resolve to specified package. + ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageY); + // Should resolve to highest available version. + ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageY_V2.Version)); + } + } + } +} diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs index 750814f7420..e980c0ae60c 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetSourcesTests.cs @@ -4,17 +4,9 @@ using System; using System.IO; using System.Linq; -using System.Threading.Tasks; using System.Xml.Linq; -using FluentAssertions; -using NuGet.Common; using NuGet.Configuration; -using NuGet.Packaging; -using NuGet.Packaging.Core; -using NuGet.ProjectModel; using NuGet.Test.Utility; -using NuGet.Versioning; -using NuGet.XPlat.FuncTest; using Xunit; namespace Dotnet.Integration.Test @@ -461,372 +453,6 @@ public void TestCommandInvalidArguments(string command, int badCommandIndex) } } - [Theory] - [InlineData("net5.0")] - public async Task AddPkg_WithV2_RelativePath_NoVersionSpecified_Success(string targetFrameworks) - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeTrue(because: result.AllOutput); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 1); - Assert.True(sources[0].Contains("Custompackages")); - - LockFileTarget ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to highest available version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - } - } - - - [Theory] - [InlineData("net5.0")] - public async Task AddPkg_WithV2_RelativePath_NoVersionSpecified_Fail(string targetFrameworks) - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageY = "packageY"; - var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; - - // Generate Package, but packageX is not in nuget feed. - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageY_V1 }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); - - // Assert - // It should fail because there is no packageX, only packageY in source directory. - result.Success.Should().BeFalse(because: result.AllOutput); - } - } - - [Theory] - [InlineData("net5.0")] - public async Task AddPkg_WithV2_RelativePath_VersionSpecified_Success(string targetFrameworks) - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V1.Version}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeTrue(because: result.AllOutput); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 1); - Assert.True(sources[0].Contains("Custompackages")); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to specified version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); - } - } - - [Theory] - [InlineData("net5.0")] - public async Task AddPkg_WithV2_RelativePath_VersionSpecified_Fail(string targetFrameworks) - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V3.Version}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeFalse(because: result.AllOutput); - } - } - - [Theory] - [InlineData("net5.0")] - public async Task AddPkg_WithV3_RelativePath_NoVersionSpecified_Success(string targetFrameworks) - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageFrameworks = "net472; netcoreapp2.0"; - var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packageXPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeTrue(because: result.AllOutput); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 1); - Assert.True(sources[0].Contains("Custompackages")); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to specified version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - } - } - - [Theory] - [InlineData("net5.0")] - public async Task AddPkg_WithV3_RelativePath_NoVersionSpecified_Fail(string targetFrameworks) - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageY = "packageY"; - var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageFrameworks = "net472; netcoreapp2.0"; - var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packageXPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageY_V1_Context }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeFalse(because: result.AllOutput); - } - } - - [Theory] - [InlineData("net5.0")] - public async Task AddPkg_WithV3_RelativePath_VersionSpecified_Success(string targetFrameworks) - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageFrameworks = "net472; netcoreapp2.0"; - var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packageXPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V1.Version}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeTrue(because: result.AllOutput); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 1); - Assert.True(sources[0].Contains("Custompackages")); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to specified version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); - } - } - - [Theory] - [InlineData("net5.0")] - public async Task AddPkg_WithV3_RelativePath_VersionSpecified_Fail(string targetFrameworks) - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageFrameworks = "net472; netcoreapp2.0"; - var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\Custompackages" : "../../Custompackages"; - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packageXPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageX_V1_Context }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V2.Version}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeFalse(because: result.AllOutput); - } - } - - [Theory] - [InlineData("net5.0")] - public async Task AddPkg_WithMixed_V2_V3_RelativePath_NoVersionSpecified_Success(string targetFrameworks) - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageY = "packageY"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageY_V2 = new PackageIdentity(packageY, new NuGetVersion("2.0.0")); - var packageFrameworks = "net472; netcoreapp2.0"; - var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageY_V2_Context = XPlatTestUtils.CreatePackage(packageY_V2.Id, packageY_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesX"); - var packageYPath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesY"); - var sourceRelativePathX = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\CustompackagesX" : "../../CustompackagesX"; - var sourceRelativePathY = RuntimeEnvironmentHelper.IsWindows ? "..\\..\\CustompackagesY" : "../../CustompackagesY"; - - // Generate V2 Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - // Generate V3 Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packageYPath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageY_V1_Context, packageY_V2_Context }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - - // Single source - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePathX}", ignoreExitCode: true); - result.Success.Should().BeTrue(because: result.AllOutput); - - // Multiple sources - result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageY} -s {sourceRelativePathY};{sourceRelativePathX}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeTrue(because: result.AllOutput); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 2); - sources.Should().Contain(e => e.Contains("CustompackagesX")); - sources.Should().Contain(e => e.Contains("CustompackagesY")); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - // Should resolve to specified version. - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageY); - // Should resolve to highest available version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageY_V2.Version)); - } - } - /// /// Utility for asserting faulty executions of dotnet.exe /// diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index e3808f03d7a..feba73e9a79 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -748,7 +748,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV2Source_AbsolutePath_NoVersionSpecified_Success() + public async Task AddPkg_V2LocalSourceFeed_WithAbsolutePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -757,17 +757,17 @@ public async Task AddPkg_WithV2Source_AbsolutePath_NoVersionSpecified_Success() var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder + customSourcePath, //not using solution source folder new PackageIdentity[] { packageX_V1, packageX_V2 }); // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: packageXPath); + sources: customSourcePath); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -781,7 +781,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); Assert.Equal(sources.Count(), 1); - Assert.True(sources[0].Contains("Custompackages")); + Assert.Equal(sources[0], customSourcePath); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -791,7 +791,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2_AbsolutePath_NoVersionSpecified_Fail() + public async Task AddPkg_V2LocalSourceFeed_WithAbsolutePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -801,17 +801,17 @@ public async Task AddPkg_WithV2_AbsolutePath_NoVersionSpecified_Fail() var packageY = "packageY"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package, but packageX is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder + customSourcePath, //not using solution source folder new PackageIdentity[] { packageY_V1 }); // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: packageXPath); + sources: customSourcePath); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -824,7 +824,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2_AbsolutePath_VersionSpecified_Success() + public async Task AddPkg_V2LocalSourceFeed_WithAbsolutePath_VersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -833,16 +833,16 @@ public async Task AddPkg_WithV2_AbsolutePath_VersionSpecified_Success() var packageX = "packageX"; var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder + customSourcePath, //not using solution source folder new PackageIdentity[] { packageX_V1, packageX_V2 }); var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), projectA, - sources: packageXPath); + sources: customSourcePath); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -856,7 +856,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); Assert.Equal(sources.Count(), 1); - Assert.True(sources[0].Contains("Custompackages")); + Assert.Equal(sources[0], customSourcePath); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -866,7 +866,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV2_AbsolutePath_VersionSpecified_Fail() + public async Task AddPkg_V2LocalSourceFeed_WithAbsolutePath_VersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -876,16 +876,16 @@ public async Task AddPkg_WithV2_AbsolutePath_VersionSpecified_Fail() var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var packageXPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package, but V3 is not in nuget feed. await SimpleTestPackageUtility.CreateFolderFeedV2Async( - packageXPath, //not using solution source folder + customSourcePath, //not using solution source folder new PackageIdentity[] { packageX_V1, packageX_V2 }); var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), projectA, - sources: packageXPath); + sources: customSourcePath); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -898,7 +898,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV2Async( } [Fact] - public async Task AddPkg_WithV3_AbsolutePath_NoVersionSpecified_Success() + public async Task AddPkg_V3LocalSourceFeed_WithAbsolutePath_NoVersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -910,18 +910,18 @@ public async Task AddPkg_WithV3_AbsolutePath_NoVersionSpecified_Success() var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packagesPath, + customSourcePath, PackageSaveMode.Defaultv3, new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", projectA, - sources: packagesPath); + sources: customSourcePath); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -935,7 +935,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); Assert.Equal(sources.Count(), 1); - Assert.True(sources[0].Contains("Custompackages")); + Assert.Equal(sources[0], customSourcePath); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -945,7 +945,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3_AbsolutePath_NoVersionSpecified_Fail() + public async Task AddPkg_V3LocalSourceFeed_WithAbsolutePath_NoVersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -956,18 +956,18 @@ public async Task AddPkg_WithV3_AbsolutePath_NoVersionSpecified_Fail() var packageY = "packageY"; var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, frameworkString: packageFrameworks); var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY, frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packagesPath, + customSourcePath, PackageSaveMode.Defaultv3, new SimpleTestPackageContext[] { packageY_V1_Context }); // Since user is not inputing a version, it is converted to a " * " in the command var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX, "*", projectA, - sources: packagesPath); + sources: customSourcePath); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -980,7 +980,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3_AbsolutePath_VersionSpecified_Success() + public async Task AddPkg_V3LocalSourceFeed_WithAbsolutePath_VersionSpecified_Success() { using (var pathContext = new SimpleTestPathContext()) { @@ -992,17 +992,17 @@ public async Task AddPkg_WithV3_AbsolutePath_VersionSpecified_Success() var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX_V1.Id, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX_V2.Id, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packagesPath, + customSourcePath, PackageSaveMode.Defaultv3, new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), projectA, - sources: packagesPath); + sources: customSourcePath); var commandRunner = new AddPackageReferenceCommandRunner(); @@ -1016,7 +1016,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); Assert.Equal(sources.Count(), 1); - Assert.True(sources[0].Contains("Custompackages")); + Assert.Equal(sources[0], customSourcePath); var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); @@ -1026,7 +1026,7 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } [Fact] - public async Task AddPkg_WithV3_AbsolutePath_VersionSpecified_Fail() + public async Task AddPkg_V3LocalSourceFeed_WithAbsolutePath_VersionSpecified_Fail() { using (var pathContext = new SimpleTestPathContext()) { @@ -1039,17 +1039,17 @@ public async Task AddPkg_WithV3_AbsolutePath_VersionSpecified_Fail() var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); var packageX_V1_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V1.Version.Version.ToString(), frameworkString: packageFrameworks); var packageX_V2_Context = XPlatTestUtils.CreatePackage(packageX, packageX_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var packagesPath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); + var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); // Generate Package await SimpleTestPackageUtility.CreateFolderFeedV3Async( - packagesPath, + customSourcePath, PackageSaveMode.Defaultv3, new SimpleTestPackageContext[] { packageX_V1_Context, packageX_V2_Context }); var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), projectA, - sources: packagesPath); + sources: customSourcePath); var commandRunner = new AddPackageReferenceCommandRunner(); From ec6c73f95970acd7c74cb7a2e88d665087ceb91e Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Thu, 7 Jan 2021 11:41:47 -0800 Subject: [PATCH 28/30] Remove unused `sources` parameter passed down. --- .../AddPackageReferenceCommandRunner.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs index 00963ad1af7..fb4c64bd995 100644 --- a/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs +++ b/src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageReferenceCommands/AddPackageReferenceCommandRunner.cs @@ -102,12 +102,10 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, var originalPackageSpec = matchingPackageSpecs.FirstOrDefault(); - // Convert relative path to absolute path if there is any - List sources = null; - if (packageReferenceArgs.Sources?.Any() == true) { - sources = new List(); + // Convert relative path to absolute path if there is any + List sources = new List(); foreach (string source in packageReferenceArgs.Sources) { @@ -167,7 +165,7 @@ public async Task ExecuteCommand(PackageReferenceArgs packageReferenceArgs, packageReferenceArgs.Logger.LogDebug("Running Restore preview"); var restorePreviewResult = await PreviewAddPackageReferenceAsync(packageReferenceArgs, - updatedDgSpec, sources); + updatedDgSpec); packageReferenceArgs.Logger.LogDebug("Restore Review completed"); @@ -326,7 +324,7 @@ private static LibraryDependency GenerateLibraryDependency( } private static async Task PreviewAddPackageReferenceAsync(PackageReferenceArgs packageReferenceArgs, - DependencyGraphSpec dgSpec, List sourcePaths) + DependencyGraphSpec dgSpec) { // Set user agent and connection settings. XPlatUtility.ConfigureProtocol(); From 8f797e1161c27a7b2f64321302f4e4be491642a3 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 8 Jan 2021 14:10:05 -0800 Subject: [PATCH 29/30] Remove V2 sourcefeed unit tests --- .../DotnetAddPackageTests.cs | 250 ------------------ .../NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs | 150 ----------- 2 files changed, 400 deletions(-) diff --git a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetAddPackageTests.cs b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetAddPackageTests.cs index c7184c0ccd5..ce29c557c23 100644 --- a/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetAddPackageTests.cs +++ b/test/NuGet.Core.FuncTests/Dotnet.Integration.Test/DotnetAddPackageTests.cs @@ -25,150 +25,6 @@ public DotnetAddPackageTests(MsbuildIntegrationTestFixture fixture) _fixture = fixture; } - [Fact] - public async Task AddPkg_V2LocalSourceFeed_WithRelativePath_NoVersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - var targetFrameworks = "net5.0"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - customSourcePath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeTrue(because: result.AllOutput); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 1); - Assert.Equal(sources[0], customSourcePath); - - LockFileTarget ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to highest available version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - } - } - - [Fact] - public async Task AddPkg_V2LocalSourceFeed_WithRelativePath_NoVersionSpecified_Fail() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - var targetFrameworks = "net5.0"; - XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageY = "packageY"; - var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); - - // Generate Package, but packageX is not in nuget feed. - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - customSourcePath, //not using solution source folder - new PackageIdentity[] { packageY_V1 }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath}", ignoreExitCode: true); - - // Assert - // It should fail because there is no packageX, only packageY in source directory. - result.Success.Should().BeFalse(because: result.AllOutput); - } - } - - [Fact] - public async Task AddPkg_V2LocalSourceFeed_WithRelativePath_VersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - var targetFrameworks = "net5.0"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - customSourcePath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V1.Version}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeTrue(because: result.AllOutput); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 1); - Assert.Equal(sources[0], customSourcePath); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to specified package. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); - } - } - - [Fact] - public async Task AddPkg_V2LocalSourceFeed_WithRelativePath_VersionSpecified_Fail() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - var targetFrameworks = "net5.0"; - XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - var sourceRelativePath = Path.Combine("..", "..", "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - customSourcePath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePath} -v {packageX_V3.Version}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeFalse(because: result.AllOutput); - } - } - [Fact] public async Task AddPkg_V3LocalSourceFeed_WithRelativePath_NoVersionSpecified_Success() { @@ -325,112 +181,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } - [Fact] - public async Task AddPkg_V2_V3_LocalSourceFeeds_WithRelativePath_NoVersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - var targetFrameworks = "net5.0"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageY = "packageY"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var packageY_V2 = new PackageIdentity(packageY, new NuGetVersion("2.0.0")); - var packageFrameworks = "net472; netcoreapp2.0"; - var packageY_V1_Context = XPlatTestUtils.CreatePackage(packageY_V1.Id, packageY_V1.Version.Version.ToString(), frameworkString: packageFrameworks); - var packageY_V2_Context = XPlatTestUtils.CreatePackage(packageY_V2.Id, packageY_V2.Version.Version.ToString(), frameworkString: packageFrameworks); - var customPackageX_SourcePath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesX"); - var customPackageY_Sourcepath = Path.Combine(pathContext.WorkingDirectory, "CustompackagesY"); - var sourceRelativePathX = Path.Combine("..", "..", "CustompackagesX"); - var sourceRelativePathY = Path.Combine("..", "..", "CustompackagesY"); - - // Generate V2 Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - customPackageX_SourcePath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - // Generate V3 Package - await SimpleTestPackageUtility.CreateFolderFeedV3Async( - customPackageY_Sourcepath, - PackageSaveMode.Defaultv3, - new SimpleTestPackageContext[] { packageY_V1_Context, packageY_V2_Context }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - - // Single source - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX} -s {sourceRelativePathX}", ignoreExitCode: true); - result.Success.Should().BeTrue(because: result.AllOutput); - - // Multiple sources - result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageY} -s {sourceRelativePathY};{sourceRelativePathX}", ignoreExitCode: true); - - // Assert - result.Success.Should().BeTrue(because: result.AllOutput); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 2); - sources.Should().Contain(e => e.Contains(customPackageX_SourcePath)); - sources.Should().Contain(e => e.Contains(customPackageY_Sourcepath)); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - // Should resolve to specified package. - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageY); - // Should resolve to highest available version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageY_V2.Version)); - } - } - - [Fact] - public async Task AddPkg_V2LocalSourceFeed_WithDefaultSolutiuonSource_NoVersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectName = "projectA"; - var targetFrameworks = "net5.0"; - SimpleTestProjectContext projectA = XPlatTestUtils.CreateProject(projectName, pathContext, targetFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - - // Generate V2 Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - pathContext.PackageSource, // using default solution source folder, not passing source as parameter. - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var projectDirectory = Path.Combine(pathContext.SolutionRoot, projectName); - var projectFilePath = Path.Combine(projectDirectory, $"{projectName}.csproj"); - - // Act - CommandRunnerResult result = _fixture.RunDotnet(projectDirectory, $"add {projectFilePath} package {packageX}", ignoreExitCode: true); - result.Success.Should().BeTrue(because: result.AllOutput); - - // Assert - result.Success.Should().BeTrue(because: result.AllOutput); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 1); - Assert.Equal(sources[0], pathContext.PackageSource); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - // Should resolve to specified package. - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to highest available version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - } - } - [Fact] public async Task AddPkg_V3LocalSourceFeed_WithDefaultSolutiuonSource_NoVersionSpecified_Success() { diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs index feba73e9a79..95a563a2345 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatAddPkgTests.cs @@ -747,156 +747,6 @@ await SimpleTestPackageUtility.CreateFolderFeedV3Async( } } - [Fact] - public async Task AddPkg_V2LocalSourceFeed_WithAbsolutePath_NoVersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - customSourcePath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - // Since user is not inputing a version, it is converted to a " * " in the command - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", - projectA, - sources: customSourcePath); - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(0, result); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 1); - Assert.Equal(sources[0], customSourcePath); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to highest available version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V2.Version)); - } - } - - [Fact] - public async Task AddPkg_V2LocalSourceFeed_WithAbsolutePath_NoVersionSpecified_Fail() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageY = "packageY"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageY_V1 = new PackageIdentity(packageY, new NuGetVersion("1.0.0")); - var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - - // Generate Package, but packageX is not in nuget feed. - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - customSourcePath, //not using solution source folder - new PackageIdentity[] { packageY_V1 }); - - // Since user is not inputing a version, it is converted to a " * " in the command - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, "*", - projectA, - sources: customSourcePath); - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(1, result); - } - } - - [Fact] - public async Task AddPkg_V2LocalSourceFeed_WithAbsolutePath_VersionSpecified_Success() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - - // Generate Package - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - customSourcePath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V1.Id, packageX_V1.Version.ToString(), - projectA, - sources: customSourcePath); - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(0, result); - - // Make sure source is replaced in generated dgSpec file. - PackageSpec packageSpec = projectA.AssetsFile.PackageSpec; - string[] sources = packageSpec.RestoreMetadata.Sources.Select(s => s.Name).ToArray(); - Assert.Equal(sources.Count(), 1); - Assert.Equal(sources[0], customSourcePath); - - var ridlessTarget = projectA.AssetsFile.Targets.Where(e => string.IsNullOrEmpty(e.RuntimeIdentifier)).Single(); - ridlessTarget.Libraries.Should().Contain(e => e.Type == "package" && e.Name == packageX); - // Should resolve to specified version. - ridlessTarget.Libraries.Should().Contain(e => e.Version.Equals(packageX_V1.Version)); - } - } - - [Fact] - public async Task AddPkg_V2LocalSourceFeed_WithAbsolutePath_VersionSpecified_Fail() - { - using (var pathContext = new SimpleTestPathContext()) - { - var projectFrameworks = "net472"; - var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks); - var packageX = "packageX"; - var packageX_V1 = new PackageIdentity(packageX, new NuGetVersion("1.0.0")); - var packageX_V2 = new PackageIdentity(packageX, new NuGetVersion("2.0.0")); - var packageX_V3 = new PackageIdentity(packageX, new NuGetVersion("3.0.0")); - var customSourcePath = Path.Combine(pathContext.WorkingDirectory, "Custompackages"); - - // Generate Package, but V3 is not in nuget feed. - await SimpleTestPackageUtility.CreateFolderFeedV2Async( - customSourcePath, //not using solution source folder - new PackageIdentity[] { packageX_V1, packageX_V2 }); - - var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX_V3.Id, packageX_V3.Version.ToString(), - projectA, - sources: customSourcePath); - - var commandRunner = new AddPackageReferenceCommandRunner(); - - // Act - var result = await commandRunner.ExecuteCommand(packageArgs, MsBuild); - - // Assert - Assert.Equal(1, result); - } - } - [Fact] public async Task AddPkg_V3LocalSourceFeed_WithAbsolutePath_NoVersionSpecified_Success() { From c9e4a0ff4a46b2f0f4eeaec60fd4b41fb4a90fd9 Mon Sep 17 00:00:00 2001 From: Erick Yondon Date: Fri, 8 Jan 2021 15:14:51 -0800 Subject: [PATCH 30/30] Revert unused file change. --- .../NuGet.XPlat.FuncTest/XPlatTestUtils.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs index 9cd7fbfb43f..a4e919d5f54 100644 --- a/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs +++ b/test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/XPlatTestUtils.cs @@ -219,15 +219,14 @@ internal static PackageReferenceArgs GetPackageReferenceArgs(string packageId, S } internal static PackageReferenceArgs GetPackageReferenceArgs(string packageId, string packageVersion, SimpleTestProjectContext project, - string frameworks = "", string packageDirectory = "", string sources = "", bool noRestore = false, bool noVersion = false, bool prerelease = false, string dgFilePath = "") + string frameworks = "", string packageDirectory = "", string sources = "", bool noRestore = false, bool noVersion = false, bool prerelease = false) { var logger = new TestCommandOutputLogger(); - - if (string.IsNullOrEmpty(dgFilePath) && !noRestore) + var dgFilePath = string.Empty; + if (!noRestore) { dgFilePath = CreateDGFileForProject(project); } - return new PackageReferenceArgs(project.ProjectPath, logger) { Frameworks = MSBuildStringUtility.Split(frameworks),