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

Adds an entry point for the sbom-tool use the ScanCommand class. #936

Merged
merged 5 commits into from Dec 19, 2023
Merged
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
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>()));
}
}