Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,13 @@ private async Task<LibraryDependencyInfo> GetDependenciesCoreAsync(
FindPackageByIdDependencyInfo packageInfo = null;
try
{
await EnsureResource(cancellationToken);

if (_throttle != null)
{
await _throttle.WaitAsync(cancellationToken);
}

await EnsureResource(cancellationToken);

// Read package info, this will download the package if needed.
packageInfo = await _findPackagesByIdResource.GetDependencyInfoAsync(
match.Name,
Expand Down Expand Up @@ -459,13 +459,13 @@ public async Task<IPackageDownloader> GetPackageDownloaderAsync(

try
{
await EnsureResource(cancellationToken);

if (_throttle != null)
{
await _throttle.WaitAsync(cancellationToken);
}

await EnsureResource(cancellationToken);

cancellationToken.ThrowIfCancellationRequested();

var packageDownloader = await _findPackagesByIdResource.GetPackageDownloaderAsync(
Expand Down Expand Up @@ -634,10 +634,9 @@ internal async Task<IEnumerable<NuGetVersion>> GetAllVersionsInternalAsync(
{
await _throttle.WaitAsync(cancellationToken);
}
if (_findPackagesByIdResource == null)
{
return null;
}

await EnsureResource(cancellationToken);

return await _findPackagesByIdResource.GetAllVersionsAsync(
id,
cacheContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,56 @@ public async Task GetPackageDownloaderAsync_ReturnsPackageDownloader()
}
}

[Fact]
public async Task GetAllVersionsAsync_EnsuresResourceIsInitialized_ReturnsVersions()
{
// Arrange
// This test verifies that GetAllVersionsAsync properly calls EnsureResource
// to initialize _findPackagesByIdResource. Previously, EnsureResource was not called
// and GetAllVersionsInternalAsync would see _findPackagesByIdResource as null,
// silently returning null instead of the actual versions.
var testLogger = new TestLogger();
var cacheContext = new SourceCacheContext();
var expectedVersions = new[] { NuGetVersion.Parse("1.0.0"), NuGetVersion.Parse("2.0.0") };

var findResource = new Mock<FindPackageByIdResource>();
findResource.Setup(s => s.GetAllVersionsAsync(
It.IsAny<string>(),
It.IsAny<SourceCacheContext>(),
It.IsAny<ILogger>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(expectedVersions);

var source = new Mock<SourceRepository>();
source.Setup(s => s.GetResourceAsync<FindPackageByIdResource>(CancellationToken.None))
.ReturnsAsync(findResource.Object);
source.SetupGet(s => s.PackageSource)
.Returns(new PackageSource("http://test/index.json"));

var provider = new SourceRepositoryDependencyProvider(
source.Object,
testLogger,
cacheContext,
ignoreFailedSources: true,
ignoreWarning: true);

// Act
var versions = await provider.GetAllVersionsAsync(
"x",
cacheContext,
testLogger,
CancellationToken.None);

// Assert
versions.Should().BeEquivalentTo(expectedVersions);
source.Verify(s => s.GetResourceAsync<FindPackageByIdResource>(CancellationToken.None), Times.Once);
findResource.Verify(s => s.GetAllVersionsAsync(
"x",
It.IsAny<SourceCacheContext>(),
It.IsAny<ILogger>(),
It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async Task FindLibraryAsync_WhenASourceIsInaccessible_AndFailuresAreNotIgnored_EveryCallLogsAnErrorMessage()
{
Expand Down