From ce76f5df2646620e42b076c907ddcbeac374e539 Mon Sep 17 00:00:00 2001 From: Sebastian Gomez <69322674+sebasgomez238@users.noreply.github.com> Date: Tue, 19 Dec 2023 14:17:37 -0500 Subject: [PATCH] Adds an entry point for the sbom-tool use the ScanCommand class. (#936) * Add Author/License to LinuxComponent * Add method to scan that returns a ScanResult Object * Revert "Add Author/License to LinuxComponent" This reverts commit 643dc093932d0f62e761653bcf32dd772149c8b2. * Add unit tests --------- Co-authored-by: Sebastian Gomez --- .../Commands/ScanCommand.cs | 13 ++++++++ .../Commands/ScanCommandTests.cs | 31 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanCommand.cs b/src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanCommand.cs index a2885641c..e2d7d39e0 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanCommand.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/Commands/ScanCommand.cs @@ -45,6 +45,19 @@ public override async Task ExecuteAsync(CommandContext context, ScanSetting return 0; } + /// + /// Method to provide a way to execute the scan command and obtain the ScanResult object. + /// + /// ScanSettings object specifying the parameters for the scan execution. + /// A ScanResult object. + public async Task 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; diff --git a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/ScanCommandTests.cs b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/ScanCommandTests.cs index d582d602f..c2c8f7154 100644 --- a/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/ScanCommandTests.cs +++ b/test/Microsoft.ComponentDetection.Orchestrator.Tests/Commands/ScanCommandTests.cs @@ -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()), Times.Once); + this.fileWritingServiceMock.Verify(x => x.AppendToFile(It.IsAny(), It.IsAny())); + + 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(x => x == settings.ManifestFile), It.IsAny())); + } }