Skip to content

Commit

Permalink
Adds an entry point for the sbom-tool use the ScanCommand class. (#936)
Browse files Browse the repository at this point in the history
* Add Author/License to LinuxComponent

* Add method to scan that returns a ScanResult Object

* Revert "Add Author/License to LinuxComponent"

This reverts commit 643dc09.

* Add unit tests

---------

Co-authored-by: Sebastian Gomez <segomez@microsoft.com>
  • Loading branch information
sebasgomez238 and sebasgomez238 authored Dec 19, 2023
1 parent d4ca976 commit ce76f5d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public override async Task<int> ExecuteAsync(CommandContext context, ScanSetting
return 0;
}

/// <summary>
/// Method to provide a way to execute the scan command and obtain the ScanResult object.
/// </summary>
/// <param name="settings">ScanSettings object specifying the parameters for the scan execution.</param>
/// <returns>A ScanResult object.</returns>
public async Task<ScanResult> ExecuteScanCommandAsync(ScanSettings settings)
{
this.fileWritingService.Init(settings.Output);
var result = await this.scanExecutionService.ExecuteScanAsync(settings);
this.WriteComponentManifest(settings, result);
return result;
}

private void WriteComponentManifest(ScanSettings settings, ScanResult scanResult)
{
FileInfo userRequestedManifestPath = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,35 @@ public async Task ScanCommand_ExecutesScanAndPrintsManifestAsync()
await tw.FlushAsync();
ms.Position.Should().BePositive();
}

[TestMethod]
public async Task ExecuteScanCommandAsync_PrintsManifestAsync()
{
var settings = new ScanSettings { Output = "output", PrintManifest = true };
using var ms = new MemoryStream();
await using var tw = new StreamWriter(ms);
Console.SetOut(tw);

var result = await this.command.ExecuteScanCommandAsync(settings);

this.fileWritingServiceMock.Verify(x => x.Init(settings.Output), Times.Once);
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings), Times.Once);
this.fileWritingServiceMock.Verify(x => x.ResolveFilePath(It.IsAny<string>()), Times.Once);
this.fileWritingServiceMock.Verify(x => x.AppendToFile(It.IsAny<string>(), It.IsAny<ScanResult>()));

await tw.FlushAsync();
ms.Position.Should().BePositive();
}

[TestMethod]
public async Task ExecuteScanCommandAsync_WritesUserManifestAsync()
{
var settings = new ScanSettings { Output = "output", ManifestFile = new FileInfo("manifest.json") };

var result = await this.command.ExecuteScanCommandAsync(settings);

this.fileWritingServiceMock.Verify(x => x.Init(settings.Output), Times.Once);
this.scanExecutionServiceMock.Verify(x => x.ExecuteScanAsync(settings), Times.Once);
this.fileWritingServiceMock.Verify(x => x.WriteFile(It.Is<FileInfo>(x => x == settings.ManifestFile), It.IsAny<ScanResult>()));
}
}

0 comments on commit ce76f5d

Please sign in to comment.