Skip to content

Commit 14ae511

Browse files
committed
Factorize PackageDwnloadRunner
1 parent 31a1a2d commit 14ae511

File tree

2 files changed

+65
-35
lines changed

2 files changed

+65
-35
lines changed

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

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -238,53 +238,71 @@ private static bool TryGetRepositoriesForPackage(
238238
out IReadOnlyList<SourceRepository> repositories)
239239
{
240240
var mappedNames = packageSourceMapping.GetConfiguredPackageSources(packageId);
241+
IReadOnlyList<SourceRepository> resultRepositories;
241242

242-
// Only validate insecure sources when mapping produced something
243243
if (mappedNames.Count > 0)
244244
{
245-
var mappedRepos = new List<SourceRepository>(mappedNames.Count);
246-
foreach (var mappedName in mappedNames)
247-
{
248-
SourceRepository? repo = null;
249-
for (int i = 0; i < allRepos.Count; i++)
250-
{
251-
if (string.Equals(allRepos[i].PackageSource.Name, mappedName, StringComparison.OrdinalIgnoreCase))
252-
{
253-
repo = allRepos[i];
254-
break;
255-
}
256-
}
245+
resultRepositories = GetMappedRepositories(mappedNames, allRepos, packageId, logger);
257246

258-
if (repo != null)
259-
{
260-
mappedRepos.Add(repo);
261-
}
262-
else
263-
{
264-
logger.LogVerbose(
265-
string.Format(
266-
CultureInfo.CurrentCulture,
267-
Strings.PackageDownloadCommand_PackageSourceMapping_NoSuchSource,
268-
mappedName,
269-
packageId));
270-
}
271-
}
272-
273-
if (DetectAndReportInsecureSources(args.AllowInsecureConnections, mappedRepos.Select(repo => repo.PackageSource), logger))
247+
if (DetectAndReportInsecureSources(args.AllowInsecureConnections, resultRepositories.Select(repo => repo.PackageSource), logger))
274248
{
275249
repositories = [];
276250
return false;
277251
}
278-
279-
repositories = mappedRepos;
280-
return true;
281252
}
282253
else
283254
{
284-
// No mapping for this package: fall back to all sources
285-
repositories = allRepos;
286-
return true;
255+
// No mapping for this package
256+
resultRepositories = allRepos;
287257
}
258+
259+
repositories = resultRepositories;
260+
return true;
261+
}
262+
263+
private static IReadOnlyList<SourceRepository> GetMappedRepositories(
264+
IReadOnlyList<string> mappedNames,
265+
IReadOnlyList<SourceRepository> allRepos,
266+
string packageId,
267+
ILoggerWithColor logger)
268+
{
269+
var mappedRepos = new List<SourceRepository>(mappedNames.Count);
270+
271+
foreach (var mappedName in mappedNames)
272+
{
273+
SourceRepository? repo = FindRepositoryByName(mappedName, allRepos);
274+
275+
if (repo != null)
276+
{
277+
mappedRepos.Add(repo);
278+
}
279+
else
280+
{
281+
logger.LogVerbose(
282+
string.Format(
283+
CultureInfo.CurrentCulture,
284+
Strings.PackageDownloadCommand_PackageSourceMapping_NoSuchSource,
285+
mappedName,
286+
packageId));
287+
}
288+
}
289+
290+
return mappedRepos;
291+
}
292+
293+
private static SourceRepository? FindRepositoryByName(
294+
string mappedName,
295+
IReadOnlyList<SourceRepository> allRepos)
296+
{
297+
for (int i = 0; i < allRepos.Count; i++)
298+
{
299+
if (string.Equals(allRepos[i].PackageSource.Name, mappedName, StringComparison.OrdinalIgnoreCase))
300+
{
301+
return allRepos[i];
302+
}
303+
}
304+
305+
return null;
288306
}
289307

290308
private static async Task<bool> DownloadPackageAsync(

test/NuGet.Core.FuncTests/NuGet.XPlat.FuncTest/Package/Download/PackageDownloadRunnerTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,18 @@ public static IEnumerable<object[]> Cases()
470470
false,
471471
null!
472472
};
473+
474+
// no --source, mapping -> A&B, package in both A and B. Latest version from A installed
475+
yield return new object[]
476+
{
477+
new List<(string,string)> { ("Contoso.Mapped", "3.0.0") }, // A
478+
new List<(string,string)> { ("Contoso.Mapped", "2.0.0") }, // B
479+
new List<(string,string)> { ("A", "Contoso.*"), ("B", "Contoso.*") }, // mapped to A&B
480+
null,
481+
"Contoso.Mapped", null,
482+
true,
483+
("Contoso.Mapped", "3.0.0")
484+
};
473485
}
474486

475487
[Theory]

0 commit comments

Comments
 (0)