Skip to content

Commit

Permalink
tool-install: Avoid duplicate sources (#44853)
Browse files Browse the repository at this point in the history
  • Loading branch information
edvilme authored Nov 18, 2024
1 parent 5f1d18a commit 9083056
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,19 @@ private List<PackageSource> LoadDefaultSources(PackageId packageId, PackageSourc
continue;
}

if (defaultSources.Any(defaultSource => defaultSource.SourceUri == packageSource.SourceUri))
{
continue;
}

defaultSources.Add(packageSource);
}
}

return defaultSources;
}

private IEnumerable<PackageSource> LoadNuGetSources(PackageId packageId, PackageSourceLocation packageSourceLocation = null, PackageSourceMapping packageSourceMapping = null)
public IEnumerable<PackageSource> LoadNuGetSources(PackageId packageId, PackageSourceLocation packageSourceLocation = null, PackageSourceMapping packageSourceMapping = null)
{
var sources = (packageSourceLocation?.SourceFeedOverrides.Any() ?? false) ?
LoadOverrideSources(packageSourceLocation) :
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="invalid_source" value="https://api.nuget.org/v3/invalid.json" />
<add key="invalid_source_2" value="https://api.nuget.org/v3/invalid-2.json" />
</packageSources>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ public void WhenPassingIgnoreFailedSourcesItShouldNotThrow()
toolInstallGlobalOrToolPathCommand.Execute().Should().Be(0);
_fileSystem.File.Delete(Path.Combine(_temporaryDirectory, "nuget.config"));
}


[Fact]
public void WhenDuplicateSourceIsPassedIgnore()
{
var testAsset = _testAssetsManager
.CopyTestAsset("NuGetConfigRandomPackageSources", allowCopyIfPresent: true)
.WithSource();

var packageSourceLocation = new PackageSourceLocation(
nugetConfig: new FilePath(Path.Combine(testAsset.Path, "NuGet.config")),
rootConfigDirectory: new DirectoryPath(testAsset.Path),
additionalSourceFeeds: ["https://api.nuget.org/v3/invalid.json"]);
var nuGetPackageDownloader = new NuGetPackageDownloader(new DirectoryPath(testAsset.Path));

var sources = nuGetPackageDownloader.LoadNuGetSources(new ToolPackage.PackageId(PackageId), packageSourceLocation);
// There should only be one source
sources.Where(s => s.SourceUri == new Uri("https://api.nuget.org/v3/invalid.json"))
.Should().HaveCount(1);
// It should be the source from the NuGet.config file
sources.Where(s => s.SourceUri == new Uri("https://api.nuget.org/v3/invalid.json")).Single().Name
.Should().Be("invalid_source");
}

[Fact]
public void WhenRunWithPackageIdItShouldCreateValidShim()
{
Expand Down

0 comments on commit 9083056

Please sign in to comment.