|
18 | 18 | using System; |
19 | 19 | using System.Collections.Generic; |
20 | 20 | using System.IO; |
| 21 | +using System.IO.Abstractions; |
21 | 22 | using System.IO.Abstractions.TestingHelpers; |
| 23 | +using System.Runtime.Serialization.Formatters; |
| 24 | +using System.Security.Cryptography; |
22 | 25 | using System.Threading.Tasks; |
23 | 26 | using CycloneDX.Interfaces; |
24 | 27 | using CycloneDX.Models; |
| 28 | +using CycloneDX.Services; |
25 | 29 | using Moq; |
26 | 30 | using Xunit; |
| 31 | +using static CycloneDX.Models.Component; |
27 | 32 | using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport; |
28 | 33 |
|
29 | 34 | namespace CycloneDX.Tests |
@@ -75,7 +80,7 @@ public async Task CallingCycloneDX_WithOutputFilename_CreatesOutputFilename() |
75 | 80 | .Setup(s => s.GetSolutionDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>())) |
76 | 81 | .ReturnsAsync(new HashSet<DotnetDependency>()); |
77 | 82 |
|
78 | | - Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, null); |
| 83 | + Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, null); |
79 | 84 |
|
80 | 85 | RunOptions runOptions = new RunOptions |
81 | 86 | { |
@@ -128,7 +133,55 @@ public async Task CallingCycloneDX_WithSolutionOrProjectFileThatDoesntExistsRetu |
128 | 133 |
|
129 | 134 | var exitCode = await runner.HandleCommandAsync(runOptions); |
130 | 135 |
|
131 | | - Assert.NotEqual((int)ExitCode.OK, exitCode); |
| 136 | + Assert.NotEqual((int)ExitCode.OK, exitCode); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public async Task CallingCycloneDX_WithMultipleReferencesToPackage_ResolvesOne() |
| 141 | + { |
| 142 | + var solutionFile = "test.sln"; |
| 143 | + var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData> |
| 144 | + { |
| 145 | + {solutionFile,new MockFileData("") }, |
| 146 | + }); |
| 147 | + var mockSolutionFileService = new Mock<ISolutionFileService>(); |
| 148 | + mockSolutionFileService |
| 149 | + .Setup(s => s.GetSolutionDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>())) |
| 150 | + .ReturnsAsync(new HashSet<DotnetDependency> |
| 151 | + { |
| 152 | + new DotnetDependency { Name = "Package 1", Version = "1.2.3" }, |
| 153 | + new DotnetDependency { Name = "Package 1", Version = "1.3.5" }, |
| 154 | + new DotnetDependency { Name = "Package 2", Version = "2.0.0", Dependencies = new Dictionary<string, string>{{"Package 1", "[1.2.3, 1.2.3]" }} }, |
| 155 | + }); |
| 156 | + |
| 157 | + var mockNugetService = new Mock<INugetService>(); |
| 158 | + mockNugetService.Setup(s => s.GetComponentAsync(It.Is<DotnetDependency>(o => o.Name == "Package 1" && o.Version == "1.2.3"))) |
| 159 | + .ReturnsAsync(new Component { Name = "Package 1", Version = "1.2.3", }); |
| 160 | + mockNugetService.Setup(s => s.GetComponentAsync(It.Is<DotnetDependency>(o => o.Name == "Package 1" && o.Version == "1.3.5"))) |
| 161 | + .ReturnsAsync(new Component { Name = "Package 1", Version = "1.3.5", }); |
| 162 | + mockNugetService.Setup(s => s.GetComponentAsync(It.Is<DotnetDependency>(o => o.Name == "Package 2" && o.Version == "2.0.0"))) |
| 163 | + .ReturnsAsync(new Component { Name = "Package 2", Version = "2.0.0", }); |
| 164 | + |
| 165 | + var mockNugetServiceFactory = new Mock<INugetServiceFactory>(); |
| 166 | + mockNugetServiceFactory |
| 167 | + .Setup(s => s.Create(It.IsAny<RunOptions>(), It.IsAny<IFileSystem>(), It.IsAny<IGithubService>(), It.IsAny<List<string>>())) |
| 168 | + .Returns(mockNugetService.Object); |
| 169 | + |
| 170 | + Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, nugetServiceFactory: mockNugetServiceFactory.Object); |
| 171 | + |
| 172 | + RunOptions runOptions = new RunOptions |
| 173 | + { |
| 174 | + SolutionOrProjectFile = XFS.Path(solutionFile), |
| 175 | + scanProjectReferences = true, |
| 176 | + outputDirectory = XFS.Path(@"c:\NewDirectory"), |
| 177 | + outputFilename = XFS.Path(@"my_bom.xml") |
| 178 | + }; |
| 179 | + |
| 180 | + var exitCode = await runner.HandleCommandAsync(runOptions); |
| 181 | + |
| 182 | + Assert.Equal((int)ExitCode.OK, exitCode); |
| 183 | + var output = mockFileSystem.GetFile("/NewDirectory/my_bom.xml"); |
| 184 | + Assert.NotNull(output); |
132 | 185 | } |
133 | 186 | } |
134 | 187 | } |
0 commit comments