Skip to content

Commit

Permalink
Fix detector component filepath custom overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
grvillic committed Nov 16, 2024
1 parent 9408a0e commit 0a8d2b5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ private IEnumerable<DetectedComponent> GatherSetOfDetectedComponentsUnmerged(IEn
// clone custom locations and make them relative to root.
var declaredRawFilePaths = component.FilePaths ?? [];
var componentCustomLocations = JsonConvert.DeserializeObject<HashSet<string>>(JsonConvert.SerializeObject(declaredRawFilePaths));
component.FilePaths?.Clear();
if (updateLocations)
{
component.FilePaths?.Clear();
}
// Information about each component is relative to all of the graphs it is present in, so we take all graphs containing a given component and apply the graph data.
foreach (var graphKvp in dependencyGraphsByLocation.Where(x => x.Value.Contains(component.Component.Id)))
Expand Down Expand Up @@ -269,7 +273,7 @@ private HashSet<string> MakeFilePathsRelative(ILogger logger, DirectoryInfo root
try
{
var relativePath = rootUri.MakeRelativeUri(new Uri(path)).ToString();
if (!relativePath.StartsWith('/'))
if (!relativePath.StartsWith('/') && !relativePath.Contains(':', StringComparison.CurrentCultureIgnoreCase))
{
relativePath = "/" + relativePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,72 @@ public void GenerateScanResultFromResult_WithCustomLocations()
actualNpmComponent.Should().BeEquivalentTo(expectedNpmComponent);
actualNugetComponent.Should().BeEquivalentTo(expectedNugetComponent);
}

[TestMethod]
public void GenerateScanResultFromResult_WithCustomLocations_WithExperimentsDryRun()
{
var detectedFilePath = "/some/file/path";
var npmCustomPath = "/custom/path.js";
var npmCustomPath2 = "D:/dummy/engtools/packages.lock.json";
var nugetCustomPath = "/custom/path2.csproj";
var relatedFilePath = "/generic/relevant/path";

var singleFileComponentRecorder = this.componentRecorder.CreateSingleFileComponentRecorder(Path.Join(this.sourceDirectory.FullName, detectedFilePath));
var processingResult = new DetectorProcessingResult
{
ResultCode = ProcessingResultCode.Success,
ContainersDetailsMap = new Dictionary<int, ContainerDetails>
{
{
this.sampleContainerDetails.Id, this.sampleContainerDetails
},
},
ComponentRecorders = [(this.componentDetectorMock.Object, this.componentRecorder)],
};

var expectedNpmComponent = new NpmComponent("npm-component", "1.2.3");
var expectedNugetComponent = new NuGetComponent("nugetComponent", "4.5.6");
var detectedNpmComponent = new DetectedComponent(expectedNpmComponent);
var detectedNugetComponent = new DetectedComponent(expectedNugetComponent);

// Any Related File will be reported for ALL components found in this graph
singleFileComponentRecorder.AddAdditionalRelatedFile(Path.Join(this.sourceDirectory.FullName, relatedFilePath));

// Registering components in same manifest with different custom paths
detectedNpmComponent.AddComponentFilePath(Path.Join(this.sourceDirectory.FullName, npmCustomPath));
detectedNpmComponent.AddComponentFilePath(npmCustomPath2);
detectedNugetComponent.AddComponentFilePath(Path.Join(this.sourceDirectory.FullName, nugetCustomPath));

singleFileComponentRecorder.RegisterUsage(detectedNpmComponent, isDevelopmentDependency: false);
singleFileComponentRecorder.RegisterUsage(detectedNugetComponent, isDevelopmentDependency: true);

var settings = new ScanSettings
{
SourceDirectory = this.sourceDirectory,
};

// Experiments tool generates the graph but should not update the locations at all.
var result = this.serviceUnderTest.GenerateScanResultFromProcessingResult(processingResult, settings, updateLocations: false);
result.Should().NotBeNull();
result.ComponentsFound.Should().HaveCount(2);
result.ResultCode.Should().Be(ProcessingResultCode.Success);

// next run will record the actual locations from non-experimental detectors
result = this.serviceUnderTest.GenerateScanResultFromProcessingResult(processingResult, settings);
result.Should().NotBeNull();
result.ComponentsFound.Should().HaveCount(2);
result.ResultCode.Should().Be(ProcessingResultCode.Success);

var resultNpmComponent = result.ComponentsFound.Single(c => c.Component.Type == ComponentType.Npm);
var resultNugetComponent = result.ComponentsFound.Single(c => c.Component.Type == ComponentType.NuGet);

resultNpmComponent.LocationsFoundAt.Should().BeEquivalentTo([npmCustomPath, detectedFilePath, relatedFilePath, npmCustomPath2]);
resultNugetComponent.LocationsFoundAt.Should().BeEquivalentTo([nugetCustomPath, detectedFilePath, relatedFilePath]);

var actualNpmComponent = resultNpmComponent.Component as NpmComponent;
var actualNugetComponent = resultNugetComponent.Component as NuGetComponent;

actualNpmComponent.Should().BeEquivalentTo(expectedNpmComponent);
actualNugetComponent.Should().BeEquivalentTo(expectedNugetComponent);
}
}

0 comments on commit 0a8d2b5

Please sign in to comment.