Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Unable to locate valid bom ref for package error when using a range and there are multiple versions of a package referenced #903

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
57 changes: 55 additions & 2 deletions CycloneDX.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Runtime.Serialization.Formatters;
using System.Security.Cryptography;
using System.Threading.Tasks;
using CycloneDX.Interfaces;
using CycloneDX.Models;
using CycloneDX.Services;
using Moq;
using Xunit;
using static CycloneDX.Models.Component;
using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport;

namespace CycloneDX.Tests
Expand Down Expand Up @@ -75,7 +80,7 @@ public async Task CallingCycloneDX_WithOutputFilename_CreatesOutputFilename()
.Setup(s => s.GetSolutionDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new HashSet<DotnetDependency>());

Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, null);
Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, null);

RunOptions runOptions = new RunOptions
{
Expand Down Expand Up @@ -128,7 +133,55 @@ public async Task CallingCycloneDX_WithSolutionOrProjectFileThatDoesntExistsRetu

var exitCode = await runner.HandleCommandAsync(runOptions);

Assert.NotEqual((int)ExitCode.OK, exitCode);
Assert.NotEqual((int)ExitCode.OK, exitCode);
}

[Fact]
public async Task CallingCycloneDX_WithMultipleReferencesToPackage_ResolvesOne()
{
var solutionFile = "test.sln";
var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{solutionFile,new MockFileData("") },
});
var mockSolutionFileService = new Mock<ISolutionFileService>();
mockSolutionFileService
.Setup(s => s.GetSolutionDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new HashSet<DotnetDependency>
{
new DotnetDependency { Name = "Package 1", Version = "1.2.3" },
new DotnetDependency { Name = "Package 1", Version = "1.3.5" },
new DotnetDependency { Name = "Package 2", Version = "2.0.0", Dependencies = new Dictionary<string, string>{{"Package 1", "[1.2.3, 1.2.3]" }} },
});

var mockNugetService = new Mock<INugetService>();
mockNugetService.Setup(s => s.GetComponentAsync(It.Is<DotnetDependency>(o => o.Name == "Package 1" && o.Version == "1.2.3")))
.ReturnsAsync(new Component { Name = "Package 1", Version = "1.2.3", });
mockNugetService.Setup(s => s.GetComponentAsync(It.Is<DotnetDependency>(o => o.Name == "Package 1" && o.Version == "1.3.5")))
.ReturnsAsync(new Component { Name = "Package 1", Version = "1.3.5", });
mockNugetService.Setup(s => s.GetComponentAsync(It.Is<DotnetDependency>(o => o.Name == "Package 2" && o.Version == "2.0.0")))
.ReturnsAsync(new Component { Name = "Package 2", Version = "2.0.0", });

var mockNugetServiceFactory = new Mock<INugetServiceFactory>();
mockNugetServiceFactory
.Setup(s => s.Create(It.IsAny<RunOptions>(), It.IsAny<IFileSystem>(), It.IsAny<IGithubService>(), It.IsAny<List<string>>()))
.Returns(mockNugetService.Object);

Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, nugetServiceFactory: mockNugetServiceFactory.Object);

RunOptions runOptions = new RunOptions
{
SolutionOrProjectFile = XFS.Path(solutionFile),
scanProjectReferences = true,
outputDirectory = XFS.Path(@"c:\NewDirectory"),
outputFilename = XFS.Path(@"my_bom.xml")
};

var exitCode = await runner.HandleCommandAsync(runOptions);

Assert.Equal((int)ExitCode.OK, exitCode);
var output = mockFileSystem.GetFile("/NewDirectory/my_bom.xml");
Assert.NotNull(output);
}
}
}
2 changes: 1 addition & 1 deletion CycloneDX/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public async Task<int> HandleCommandAsync(RunOptions options)
if (!bomRefLookup.ContainsKey(lookupKey))
{
var packageNameMatch = bomRefLookup.Where(x => x.Key.Item1 == dep.Key.ToLower(CultureInfo.InvariantCulture)).ToList();
if (packageNameMatch.Count == 1)
if (packageNameMatch.Count >= 1)
{
lookupKey = packageNameMatch.First().Key;
}
Expand Down