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-
64using System . Collections . Generic ;
75using System . IO ;
86using System . Threading ;
97using System . Threading . Tasks ;
8+ using FluentAssertions ;
109using Moq ;
1110using NuGet . CommandLine . XPlat ;
1211using NuGet . CommandLine . XPlat . Commands . Package ;
@@ -16,8 +15,11 @@ namespace NuGet.CommandLine.Xplat.Tests;
1615using NuGet . Protocol . Core . Types ;
1716using NuGet . Test . Utility ;
1817using NuGet . Versioning ;
18+ using Test . Utility ;
1919using Xunit ;
2020
21+ namespace NuGet . CommandLine . Xplat . Tests ;
22+
2123public 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