From e9722d69400a57d2c0508bbce9e9591fcad550f1 Mon Sep 17 00:00:00 2001 From: Heng Liu <45407901+heng-liu@users.noreply.github.com> Date: Mon, 14 Dec 2020 23:07:25 +0000 Subject: [PATCH] Do not install unlisted package in NuGet.exe (#3795) --- .../Commands/InstallCommand.cs | 2 +- .../NuGetInstallCommandTest.cs | 90 +++++++++++++++++++ .../NuGet.CommandLine.Test/Util.cs | 2 +- .../SimpleTestSettingsContext.cs | 10 +++ 4 files changed, 102 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs b/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs index f1f2c5f377b..4b5d14bf6ac 100644 --- a/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs +++ b/src/NuGet.Clients/NuGet.CommandLine/Commands/InstallCommand.cs @@ -286,7 +286,7 @@ private CommandLineSourceRepositoryProvider GetSourceRepositoryProvider() var resolutionContext = new ResolutionContext( dependencyBehavior, includePrelease: allowPrerelease, - includeUnlisted: true, + includeUnlisted: false, versionConstraints: VersionConstraints.None, gatherCache: new GatherCache(), sourceCacheContext: sourceCacheContext); diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs index 7cba915e777..acd6498848e 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/NuGetInstallCommandTest.cs @@ -1719,6 +1719,96 @@ public async Task InstallCommand_LongPathPackage() } } + [SkipMono(Skip = "Mono has issues if the MockServer has anything else running in the same process https://github.com/NuGet/Home/issues/8594")] + public async Task InstallCommand_DoNotSpecifyVersion_IgnoresUnlistedPackagesAsync() + { + using (var pathContext = new SimpleTestPathContext()) + using (var mockServer = new FileSystemBackedV3MockServer(pathContext.PackageSource)) + { + //Replace the default package source of folder to ServiceIndexUri + var settings = pathContext.Settings; + SimpleTestSettingsContext.RemoveSource(settings.XML, "source"); + var section = SimpleTestSettingsContext.GetOrAddSection(settings.XML, "packageSources"); + SimpleTestSettingsContext.AddEntry(section, "source", mockServer.ServiceIndexUri); + settings.Save(); + + // Arrange + var a1 = new SimpleTestPackageContext("a", "1.0.0"); + var a2 = new SimpleTestPackageContext("a", "2.0.0"); + var b1 = new SimpleTestPackageContext("b", "1.0.0"); + var b2 = new SimpleTestPackageContext("b", "2.0.0"); + + SimpleTestPackageContext[] packages = new SimpleTestPackageContext[] { a1, a2, b1, b2 }; + await SimpleTestPackageUtility.CreatePackagesAsync(pathContext.PackageSource, packages); + + //Unlist a2 and b1 + mockServer.UnlistedPackages.Add(a2.Identity); + mockServer.UnlistedPackages.Add(b1.Identity); + + mockServer.Start(); + var pathResolver = new PackagePathResolver(pathContext.SolutionRoot); + + // Act + var r1 = RunInstall(pathContext, "A", 0, "-OutputDirectory", pathContext.SolutionRoot); + var r2 = RunInstall(pathContext, "B", 0, "-OutputDirectory", pathContext.SolutionRoot); + + mockServer.Stop(); + + // Assert + var a1Nupkg = pathResolver.GetInstalledPackageFilePath(a1.Identity); + var a2Nupkg = pathResolver.GetInstalledPackageFilePath(a2.Identity); + var b1Nupkg = pathResolver.GetInstalledPackageFilePath(b1.Identity); + var b2Nupkg = pathResolver.GetInstalledPackageFilePath(b2.Identity); + + r1.Success.Should().BeTrue(); + r2.Success.Should().BeTrue(); + File.Exists(a1Nupkg).Should().BeTrue(); + File.Exists(a2Nupkg).Should().BeFalse(); + File.Exists(b1Nupkg).Should().BeFalse(); + File.Exists(b2Nupkg).Should().BeTrue(); + } + } + + [SkipMono(Skip = "Mono has issues if the MockServer has anything else running in the same process https://github.com/NuGet/Home/issues/8594")] + public async Task InstallCommand_SpecifyUnlistedVersion_InstallUnlistedPackagesAsync() + { + using (var pathContext = new SimpleTestPathContext()) + using (var mockServer = new FileSystemBackedV3MockServer(pathContext.PackageSource)) + { + //Replace the default package source of folder to ServiceIndexUri + var settings = pathContext.Settings; + SimpleTestSettingsContext.RemoveSource(settings.XML, "source"); + var section = SimpleTestSettingsContext.GetOrAddSection(settings.XML, "packageSources"); + SimpleTestSettingsContext.AddEntry(section, "source", mockServer.ServiceIndexUri); + settings.Save(); + + // Arrange + var a1 = new SimpleTestPackageContext("a", "1.0.0"); + var a2 = new SimpleTestPackageContext("a", "2.0.0"); + + SimpleTestPackageContext[] packages = new SimpleTestPackageContext[] { a1, a2 }; + await SimpleTestPackageUtility.CreatePackagesAsync(pathContext.PackageSource, packages); + + //Unlist a2 + mockServer.UnlistedPackages.Add(a2.Identity); + + mockServer.Start(); + var pathResolver = new PackagePathResolver(pathContext.SolutionRoot); + + // Act + var r1 = RunInstall(pathContext, "a", 0, "-Version", "2.0.0", "-OutputDirectory", pathContext.SolutionRoot); + + mockServer.Stop(); + + // Assert + var a1Nupkg = pathResolver.GetInstalledPackageFilePath(a1.Identity); + var a2Nupkg = pathResolver.GetInstalledPackageFilePath(a2.Identity); + + r1.Success.Should().BeTrue(); + File.Exists(a1Nupkg).Should().BeFalse(); + File.Exists(a2Nupkg).Should().BeTrue(); + } + } public static CommandRunnerResult RunInstall(SimpleTestPathContext pathContext, string input, int expectedExitCode = 0, params string[] additionalArgs) { var nugetexe = Util.GetNuGetExePath(); diff --git a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs index bc936f0f1f2..60ceb8bf19d 100644 --- a/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs +++ b/test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs @@ -762,7 +762,7 @@ private static JObject GetPackageRegistrationItem(MockServer server, string id, var catalogEntry = new JObject(); item.Add(new JProperty("catalogEntry", catalogEntry)); - item.Add(new JProperty("packageContent", $"{server.Uri}packages/{id}.{version}.nupkg")); + item.Add(new JProperty("packageContent", $"{server.Uri}flat/{id}/{version}/{id}.{version}.nupkg")); item.Add(new JProperty("registration", indexUrl)); catalogEntry.Add(new JProperty("@id", diff --git a/test/TestUtilities/Test.Utility/SimpleTestSetup/SimpleTestSettingsContext.cs b/test/TestUtilities/Test.Utility/SimpleTestSetup/SimpleTestSettingsContext.cs index 34d1ec90871..f416dc3e3ee 100644 --- a/test/TestUtilities/Test.Utility/SimpleTestSetup/SimpleTestSettingsContext.cs +++ b/test/TestUtilities/Test.Utility/SimpleTestSetup/SimpleTestSettingsContext.cs @@ -139,6 +139,16 @@ public static void RemoveSetting(XDocument doc, string key) } } + public static void RemoveSource(XDocument doc, string key) + { + var packageSources = GetOrAddSection(doc, "packageSources"); + + foreach (var item in packageSources.Elements(XName.Get("add")).Where(e => e.FirstAttribute.Value.Equals(key, StringComparison.OrdinalIgnoreCase)).ToArray()) + { + item.Remove(); + } + } + // Add NetStandard.Library and NetCorePlatforms to the feed and save the file. public void AddNetStandardFeeds() {