Skip to content

Commit

Permalink
Do not install unlisted package in NuGet.exe (#3795)
Browse files Browse the repository at this point in the history
  • Loading branch information
heng-liu committed Dec 14, 2020
1 parent a63a08c commit e9722d6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
Expand Up @@ -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);
Expand Down
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs
Expand Up @@ -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",
Expand Down
Expand Up @@ -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()
{
Expand Down

0 comments on commit e9722d6

Please sign in to comment.