Skip to content

Commit aac5448

Browse files
committed
unlisted
1 parent 3f79933 commit aac5448

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/Package/Download/PackageDownloadRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ await ResolvePackageDownloadVersion(
145145
var packages = await finder.GetMetadataAsync(
146146
packageWithNuGetVersion.Id,
147147
includePrerelease,
148-
includeUnlisted: false,
148+
includeUnlisted: versionSpecified, // only load unlisted if an exact version is specified
149149
sourceCacheContext: cache,
150150
logger,
151151
token);

test/NuGet.Core.Tests/NuGet.CommandLine.Xplat.Tests/Commands/Package/Download/PackageDownloadRunnerTests.cs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
namespace NuGet.CommandLine.Xplat.Tests;
5-
64
using System.Collections.Generic;
75
using System.IO;
86
using System.Threading;
97
using System.Threading.Tasks;
8+
using FluentAssertions;
109
using Moq;
1110
using NuGet.CommandLine.XPlat;
1211
using NuGet.CommandLine.XPlat.Commands.Package;
@@ -16,8 +15,11 @@ namespace NuGet.CommandLine.Xplat.Tests;
1615
using NuGet.Protocol.Core.Types;
1716
using NuGet.Test.Utility;
1817
using NuGet.Versioning;
18+
using Test.Utility;
1919
using Xunit;
2020

21+
namespace NuGet.CommandLine.Xplat.Tests;
22+
2123
public class PackageDownloadRunnerTests
2224
{
2325
public sealed record SourceSpec(string Name, params string[] Versions);
@@ -142,4 +144,61 @@ public async Task ResolvePackageDownloadVersion_Scenarios(ResolveScenario scenar
142144
var expectedPath = Path.Combine(context.WorkingDirectory, scenario.ExpectedSource!);
143145
Assert.Equal(expectedPath, resolvedRepo.PackageSource.Source);
144146
}
147+
148+
[Theory]
149+
[InlineData("1.0.0", true)] // exact version specified -> should find even if unlisted
150+
[InlineData(null, false)] // no exact version -> should return null when unlisted
151+
public async Task ResolvePackageDownloadVersion_UnlistedPackage_BehavesAsExpected(string requestedVersion, bool expectFound)
152+
{
153+
// Arrange
154+
using var pathContext = new SimpleTestPathContext();
155+
using var mockServer = new FileSystemBackedV3MockServer(pathContext.PackageSource);
156+
157+
var cache = new SourceCacheContext();
158+
var logger = new Mock<ILoggerWithColor>();
159+
var token = CancellationToken.None;
160+
161+
var sourceRepo = Repository.Factory.GetCoreV3(mockServer.ServiceIndexUri);
162+
163+
// Create the package and unlist it on the mock server.
164+
var id = "Contoso.unlisted";
165+
var createdVersion = "1.0.0";
166+
var pkg = new SimpleTestPackageContext(id, createdVersion);
167+
await SimpleTestPackageUtility.CreatePackagesAsync(pathContext.PackageSource, pkg);
168+
mockServer.UnlistedPackages.Add(pkg.Identity);
169+
170+
mockServer.Start();
171+
172+
var packageWithNuGetVersion = new PackageWithNuGetVersion
173+
{
174+
Id = id,
175+
NuGetVersion = requestedVersion is null ? null : NuGetVersion.Parse(requestedVersion)
176+
};
177+
178+
// Act
179+
(NuGetVersion foundVersion, SourceRepository foundRepo) = await PackageDownloadRunner.ResolvePackageDownloadVersion(
180+
packageWithNuGetVersion,
181+
[sourceRepo],
182+
cache,
183+
logger.Object,
184+
includePrerelease: false,
185+
token);
186+
187+
mockServer.Stop();
188+
189+
// Assert
190+
if (expectFound)
191+
{
192+
foundVersion.Should().NotBeNull();
193+
foundVersion.OriginalVersion.Should().Be(requestedVersion);
194+
foundRepo.Should().NotBeNull();
195+
foundRepo.PackageSource.Name.Should().Be(sourceRepo.PackageSource.Name);
196+
}
197+
else
198+
{
199+
foundVersion.Should().BeNull();
200+
foundRepo.Should().BeNull();
201+
}
202+
}
203+
145204
}

0 commit comments

Comments
 (0)