|
12 | 12 | using Microsoft.ComponentDetection.Contracts.Internal;
|
13 | 13 | using Microsoft.ComponentDetection.Contracts.TypedComponent;
|
14 | 14 | using Microsoft.ComponentDetection.Detectors.NuGet;
|
| 15 | +using Microsoft.ComponentDetection.Detectors.Tests.Utilities; |
15 | 16 | using Microsoft.ComponentDetection.TestsUtilities;
|
16 | 17 | using Microsoft.Extensions.Logging;
|
17 | 18 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
23 | 24 | public class NuGetComponentDetectorTests : BaseDetectorTest<NuGetComponentDetector>
|
24 | 25 | {
|
25 | 26 | private static readonly IEnumerable<string> DetectorSearchPattern =
|
26 |
| - new List<string> { "*.nupkg", "*.nuspec", "nuget.config", "paket.lock" }; |
| 27 | + new List<string> { "*.nupkg", "*.nuspec", "nuget.config", "packages.lock.json", "paket.lock" }; |
27 | 28 |
|
28 |
| - private readonly Mock<ILogger<NuGetComponentDetector>> mockLogger; |
| 29 | + private ILogger<NuGetComponentDetector> logger; |
29 | 30 |
|
30 |
| - public NuGetComponentDetectorTests() |
| 31 | + public TestContext TestContext { get; set; } |
| 32 | + |
| 33 | + [TestInitialize] |
| 34 | + public void Setup() |
31 | 35 | {
|
32 |
| - this.mockLogger = new Mock<ILogger<NuGetComponentDetector>>(); |
33 |
| - this.DetectorTestUtility.AddServiceMock(this.mockLogger); |
| 36 | + this.logger = new TestLogger<NuGetComponentDetector>(this.TestContext); |
| 37 | + this.DetectorTestUtility.AddService(this.logger); |
34 | 38 | }
|
35 | 39 |
|
36 | 40 | [TestMethod]
|
@@ -114,6 +118,57 @@ public async Task TestNugetDetector_ReturnsValidMixedComponentAsync()
|
114 | 118 | Assert.AreEqual(2, componentRecorder.GetDetectedComponents().Count());
|
115 | 119 | }
|
116 | 120 |
|
| 121 | + [TestMethod] |
| 122 | + public async Task TestNugetDetector_ReturnsPackagesLockfileAsync() |
| 123 | + { |
| 124 | + var lockfile = @"{ |
| 125 | + ""version"": 1, |
| 126 | + ""dependencies"": { |
| 127 | + ""net7.0"": { |
| 128 | + ""Azure.Core"": { |
| 129 | + ""type"": ""Direct"", |
| 130 | + ""requested"": ""[1.25.0, )"", |
| 131 | + ""resolved"": ""1.25.0"", |
| 132 | + ""contentHash"": ""X8Dd4sAggS84KScWIjEbFAdt2U1KDolQopTPoHVubG2y3CM54f9l6asVrP5Uy384NWXjsspPYaJgz5xHc+KvTA=="", |
| 133 | + ""dependencies"": { |
| 134 | + ""Microsoft.Bcl.AsyncInterfaces"": ""1.1.1"", |
| 135 | + ""System.Diagnostics.DiagnosticSource"": ""4.6.0"", |
| 136 | + ""System.Memory.Data"": ""1.0.2"", |
| 137 | + ""System.Numerics.Vectors"": ""4.5.0"", |
| 138 | + ""System.Text.Encodings.Web"": ""4.7.2"", |
| 139 | + ""System.Text.Json"": ""4.7.2"", |
| 140 | + ""System.Threading.Tasks.Extensions"": ""4.5.4"" |
| 141 | + } |
| 142 | + } |
| 143 | + }, |
| 144 | + ""net6.0"": { |
| 145 | + ""Azure.Data.Tables"": { |
| 146 | + ""type"": ""Direct"", |
| 147 | + ""requested"": ""[12.5.0, )"", |
| 148 | + ""resolved"": ""12.5.0"", |
| 149 | + ""contentHash"": ""XeIxPf+rF1NXkX3NJSB0ZTNgU233vyPXGmaFsR0lUVibtWP/lj+Qu1FcPxoslURcX0KC+UgTb226nqVdHjoweQ=="", |
| 150 | + ""dependencies"": { |
| 151 | + ""Azure.Core"": ""1.22.0"", |
| 152 | + ""System.Text.Json"": ""4.7.2"" |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +}"; |
| 158 | + |
| 159 | + var (scanResult, componentRecorder) = await this.DetectorTestUtility |
| 160 | + .WithFile("packages.lock.json", lockfile) |
| 161 | + .ExecuteDetectorAsync(); |
| 162 | + |
| 163 | + Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode); |
| 164 | + |
| 165 | + // should be 2 components found; one per framework |
| 166 | + var components = new HashSet<string>(componentRecorder.GetDetectedComponents().Select(x => x.Component.Id)); |
| 167 | + Assert.AreEqual(2, components.Count); |
| 168 | + Assert.IsTrue(components.Contains("Azure.Core 1.25.0 - NuGet")); |
| 169 | + Assert.IsTrue(components.Contains("Azure.Data.Tables 12.5.0 - NuGet")); |
| 170 | + } |
| 171 | + |
117 | 172 | [TestMethod]
|
118 | 173 | public async Task TestNugetDetector_ReturnsValidPaketComponentAsync()
|
119 | 174 | {
|
@@ -170,16 +225,9 @@ public async Task TestNugetDetector_HandlesMalformedComponentsInComponentListAsy
|
170 | 225 | .WithFile("test.nuspec", nuspec)
|
171 | 226 | .WithFile("test.nupkg", validNupkg)
|
172 | 227 | .WithFile("malformed.nupkg", malformedNupkg)
|
173 |
| - .AddServiceMock(this.mockLogger) |
| 228 | + .AddService(this.logger) |
174 | 229 | .ExecuteDetectorAsync();
|
175 | 230 |
|
176 |
| - this.mockLogger.Verify(x => x.Log( |
177 |
| - It.IsAny<LogLevel>(), |
178 |
| - It.IsAny<EventId>(), |
179 |
| - It.IsAny<It.IsAnyType>(), |
180 |
| - It.IsAny<Exception>(), |
181 |
| - (Func<It.IsAnyType, Exception, string>)It.IsAny<object>())); |
182 |
| - |
183 | 231 | Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
|
184 | 232 | Assert.AreEqual(2, componentRecorder.GetDetectedComponents().Count());
|
185 | 233 | }
|
|
0 commit comments