Skip to content

Commit

Permalink
Fixed tests and updated CD version (#162)
Browse files Browse the repository at this point in the history
* Fixed tests and updated version

* Updated CD version and removed public feed
  • Loading branch information
aasim authored Oct 21, 2022
1 parent 1756fa7 commit 7723053
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PackageVersion>
</ItemDefinitionGroup>
<PropertyGroup>
<ComponentDetectionPackageVersion>1.1.12</ComponentDetectionPackageVersion>
<ComponentDetectionPackageVersion>2.0.5</ComponentDetectionPackageVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="MinVer" Version="2.5.0"/>
Expand Down
2 changes: 1 addition & 1 deletion nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<configuration>
<packageSources>
<clear />
<add key="SBOMToolsPublic" value="https://pkgs.dev.azure.com/mseng/PipelineTools/_packaging/SBOMToolsPublic/nuget/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async Task Scan(string path)
cliArgumentBuilder.SourceDirectory(buildComponentDirPath);
var cmdLineParams = configuration.ToComponentDetectorCommandLineParams(cliArgumentBuilder);

var scanResult = await Task.Run(() => componentDetector.Scan(cmdLineParams));
var scanResult = await componentDetector.ScanAsync(cmdLineParams);

if (scanResult.ResultCode != ProcessingResultCode.Success)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.Sbom.Api/Utils/ComponentDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.ComponentDetection.Orchestrator;
using System.Threading.Tasks;

namespace Microsoft.Sbom.Api.Utils
{
Expand All @@ -11,10 +12,10 @@ namespace Microsoft.Sbom.Api.Utils
/// </summary>
public class ComponentDetector
{
public virtual ScanResult Scan(string[] args)
public virtual async Task<ScanResult> ScanAsync(string[] args)
{
var orchestrator = new Orchestrator();
return orchestrator.Load(args);
return await orchestrator.LoadAsync(args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Serilog;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;

namespace Microsoft.Sbom.Api.Utils
{
Expand All @@ -31,7 +32,7 @@ public ComponentDetectorCachedExecutor(ILogger log, ComponentDetector detector)
/// </summary>
/// <param name="args">CD arguments.</param>
/// <returns>Result of CD scan.</returns>
public virtual ScanResult Scan(string[] args)
public virtual async Task<ScanResult> ScanAsync(string[] args)
{
if (args is null)
{
Expand All @@ -45,7 +46,7 @@ public virtual ScanResult Scan(string[] args)
return results[argsHashCode];
}

var result = detector.Scan(args);
var result = await detector.ScanAsync(args);
results.TryAdd(argsHashCode, result);
return result;
}
Expand Down
10 changes: 5 additions & 5 deletions test/Microsoft.Sbom.Api.Tests/Executors/PackagesWalkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task ScanSuccessTestAsync()
ComponentsFound = scannedComponents
};

mockDetector.Setup(o => o.Scan(It.IsAny<string[]>())).Returns(scanResult);
mockDetector.Setup(o => o.ScanAsync(It.IsAny<string[]>())).Returns(Task.FromResult(scanResult));
var walker = new PackagesWalker(mockLogger.Object, mockDetector.Object, mockConfiguration.Object, mockSbomConfigs.Object);
var packagesChannelReader = walker.GetComponents("root");

Expand Down Expand Up @@ -122,7 +122,7 @@ public async Task ScanCombinePackagesWithSameNameDifferentCase()
ComponentsFound = scannedComponents
};

mockDetector.Setup(o => o.Scan(It.IsAny<string[]>())).Returns(scanResult);
mockDetector.Setup(o => o.ScanAsync(It.IsAny<string[]>())).Returns(Task.FromResult(scanResult));
var walker = new PackagesWalker(mockLogger.Object, mockDetector.Object, mockConfiguration.Object, mockSbomConfigs.Object);
var packagesChannelReader = walker.GetComponents("root");

Expand Down Expand Up @@ -153,7 +153,7 @@ public void ScanWithNullOrEmptyPathSuccessTest()
walker.GetComponents(null);
walker.GetComponents(string.Empty);

mockDetector.Verify(mock => mock.Scan(It.IsAny<string[]>()), Times.Never());
mockDetector.Verify(mock => mock.ScanAsync(It.IsAny<string[]>()), Times.Never());
}

[TestMethod]
Expand All @@ -167,7 +167,7 @@ public async Task ScanFailureTestAsync()
ComponentsFound = null
};

mockDetector.Setup(o => o.Scan(It.IsAny<string[]>())).Returns(scanResult);
mockDetector.Setup(o => o.ScanAsync(It.IsAny<string[]>())).Returns(Task.FromResult(scanResult));
var walker = new PackagesWalker(mockLogger.Object, mockDetector.Object, mockConfiguration.Object, mockSbomConfigs.Object);
var packagesChannelReader = walker.GetComponents("root");
ComponentDetectorException actualError = null;
Expand Down Expand Up @@ -215,7 +215,7 @@ public async Task ScanIgnoreSbomComponents()
ComponentsFound = scannedComponents
};

mockDetector.Setup(o => o.Scan(It.IsAny<string[]>())).Returns(scanResult);
mockDetector.Setup(o => o.ScanAsync(It.IsAny<string[]>())).Returns(Task.FromResult(scanResult));
var walker = new PackagesWalker(mockLogger.Object, mockDetector.Object, mockConfiguration.Object, mockSbomConfigs.Object);
var packagesChannelReader = walker.GetComponents("root");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task GetComponents()
ComponentsFound = scannedComponents
};

mockDetector.Setup(o => o.Scan(It.IsAny<string[]>())).Returns(scanResult);
mockDetector.Setup(o => o.ScanAsync(It.IsAny<string[]>())).Returns(Task.FromResult(scanResult));
var walker = new SBOMComponentsWalker(mockLogger.Object, mockDetector.Object, mockConfiguration.Object, mockSbomConfigs.Object);
var packagesChannelReader = walker.GetComponents("root");

Expand Down Expand Up @@ -108,7 +108,7 @@ public async Task GetComponentsWithFiltering()
ComponentsFound = scannedComponents
};

mockDetector.Setup(o => o.Scan(It.IsAny<string[]>())).Returns(scanResult);
mockDetector.Setup(o => o.ScanAsync(It.IsAny<string[]>())).Returns(Task.FromResult(scanResult));
var walker = new SBOMComponentsWalker(mockLogger.Object, mockDetector.Object, mockConfiguration.Object, mockSbomConfigs.Object);
var packagesChannelReader = walker.GetComponents("root");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Serilog;
using System.Threading.Tasks;

namespace Microsoft.Sbom.Api.Tests.Utils
{
Expand All @@ -23,28 +24,28 @@ public void TestInitialize()
}

[TestMethod]
public void Scan()
public async Task Scan()
{
var executor = new ComponentDetectorCachedExecutor(logger.Object, detector.Object);
var arguments = new string[] { "a", "b", "c" };
var expectedResult = new ScanResult();

detector.Setup(x => x.Scan(arguments)).Returns(expectedResult);
var result = executor.Scan(arguments);
detector.Setup(x => x.ScanAsync(arguments)).Returns(Task.FromResult(expectedResult));
var result = await executor.ScanAsync(arguments);
Assert.AreEqual(result, expectedResult);
Assert.IsTrue(detector.Invocations.Count == 1);
}

[TestMethod]
public void ScanWithCache()
public async Task ScanWithCache()
{
var executor = new ComponentDetectorCachedExecutor(logger.Object, detector.Object);
var arguments = new string[] { "a", "b", "c" };
var expectedResult = new ScanResult();

detector.Setup(x => x.Scan(arguments)).Returns(expectedResult);
executor.Scan(arguments);
var result = executor.Scan(arguments);
detector.Setup(x => x.ScanAsync(arguments)).Returns(Task.FromResult(expectedResult));
await executor.ScanAsync(arguments);
var result = await executor.ScanAsync(arguments);
Assert.AreEqual(result, expectedResult);
Assert.IsTrue(detector.Invocations.Count == 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Microsoft.Sbom.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using MoreLinq;
using Serilog;
using System.Collections.Generic;
using System.IO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public async Task ManifestGenerationWorkflowTests_Succeeds(bool deleteExistingMa
ComponentsFound = scannedComponents
};

mockDetector.Setup(o => o.Scan(It.IsAny<string[]>())).Returns(scanResult);
mockDetector.Setup(o => o.ScanAsync(It.IsAny<string[]>())).Returns(Task.FromResult(scanResult));

var packagesChannel = Channel.CreateUnbounded<SBOMPackage>();
var errorsChannel = Channel.CreateUnbounded<FileValidationResult>();
Expand Down

0 comments on commit 7723053

Please sign in to comment.