|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.IO; |
7 | 7 | using System.Linq; |
| 8 | +using System.Reflection; |
8 | 9 | using System.Text; |
9 | 10 | using System.Threading; |
10 | 11 | using System.Threading.Tasks; |
@@ -367,5 +368,59 @@ public void FiltersFrameworkPackagesCollectionWithVulnerableMetadata( |
367 | 368 | Assert.Equal(includeTransitivePositives ? 1 : 0, allPackages.First().TransitivePackages.Count()); |
368 | 369 | } |
369 | 370 | } |
| 371 | + |
| 372 | + public class GetPackageMetadataAsyncWithEmptyPackageSources |
| 373 | + { |
| 374 | + [Fact] |
| 375 | + public async Task GetPackageMetadataAsync_WithEmptyPackageSources_DoesNotThrowDivideByZero() |
| 376 | + { |
| 377 | + // Arrange |
| 378 | + var packages = new FrameworkPackages("net40"); |
| 379 | + var topLevelPackages = new List<InstalledPackageReference> |
| 380 | + { |
| 381 | + ListPackageTestHelper.CreateInstalledPackageReference(name: "TestPackage") |
| 382 | + }; |
| 383 | + packages.TopLevelPackages = topLevelPackages; |
| 384 | + var allPackages = new List<FrameworkPackages> { packages }; |
| 385 | + |
| 386 | + var output = new StringBuilder(); |
| 387 | + var error = new StringBuilder(); |
| 388 | + using TextWriter consoleOut = new StringWriter(output); |
| 389 | + using TextWriter consoleError = new StringWriter(error); |
| 390 | + |
| 391 | + // Create ListPackageArgs with empty packageSources list to trigger the divide by zero scenario |
| 392 | + var listPackageArgs = new ListPackageArgs( |
| 393 | + path: "", |
| 394 | + packageSources: new List<PackageSource>(), // Empty package sources - this would cause divide by zero |
| 395 | + frameworks: new List<string>(), |
| 396 | + ReportType.Outdated, // This will trigger the code path that calls GetPackageMetadataAsync |
| 397 | + new ListPackageConsoleRenderer(consoleOut, consoleError), |
| 398 | + includeTransitive: false, |
| 399 | + prerelease: false, |
| 400 | + highestPatch: false, |
| 401 | + highestMinor: false, |
| 402 | + auditSources: null, |
| 403 | + logger: new Mock<ILogger>().Object, |
| 404 | + CancellationToken.None); |
| 405 | + |
| 406 | + var listPackageRunner = new ListPackageCommandRunner(); |
| 407 | + |
| 408 | + // Act & Assert - Test the private method using reflection |
| 409 | + var getPackageMetadataAsyncMethod = typeof(ListPackageCommandRunner) |
| 410 | + .GetMethod("GetPackageMetadataAsync", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); |
| 411 | + |
| 412 | + Assert.NotNull(getPackageMetadataAsyncMethod); |
| 413 | + |
| 414 | + // This should not throw DivideByZeroException |
| 415 | + Exception exception = await Record.ExceptionAsync(async () => |
| 416 | + { |
| 417 | + var task = (Task<Dictionary<string, List<IPackageSearchMetadata>>>)getPackageMetadataAsyncMethod.Invoke( |
| 418 | + listPackageRunner, new object[] { allPackages, listPackageArgs }); |
| 419 | + await task; |
| 420 | + }); |
| 421 | + |
| 422 | + Assert.Null(exception); |
| 423 | + } |
| 424 | + } |
370 | 425 | } |
371 | 426 | } |
0 commit comments