Skip to content

Commit

Permalink
Handle comments gracefully by the go fallback detector (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfiorenzano authored Mar 14, 2024
1 parent 0bbeeee commit fb464ca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ComponentDetection.Detectors.Go;
namespace Microsoft.ComponentDetection.Detectors.Go;

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -257,6 +257,12 @@ private async Task<bool> UseGoCliToScanAsync(string location, ISingleFileCompone

private void TryRegisterDependencyFromModLine(string line, ISingleFileComponentRecorder singleFileComponentRecorder)
{
if (line.Trim().StartsWith("//"))
{
// this is a comment line, ignore it
return;
}

if (this.TryToCreateGoComponentFromModLine(line, out var goComponent))
{
singleFileComponentRecorder.RegisterUsage(new DetectedComponent(goComponent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ public async Task TestGoModDetectorWithValidFile_ReturnsSuccessfullyAsync()
discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Should().ContainSingle();
}

[TestMethod]
public async Task TestGoModDetector_CommentsOnFile_CommentsAreIgnoredAsync()
{
var goMod =
@"module github.com/Azure/azure-storage-blob-go
require (
// comment
github.com/kr/pretty v0.1.0 // indirect
)";
var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile("go.mod", goMod)
.ExecuteDetectorAsync();

scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);

var detectedComponents = componentRecorder.GetDetectedComponents();
detectedComponents.Should().ContainSingle("there is only one component definition on the file");

var discoveredComponents = detectedComponents.ToArray();
discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Should().ContainSingle();
}

[TestMethod]
public async Task TestGoSumDetectorWithValidFile_ReturnsSuccessfullyAsync()
{
Expand Down

0 comments on commit fb464ca

Please sign in to comment.