Skip to content

Commit

Permalink
Merge branch 'pauldorsch/updatesyftdocs' of https://github.com/micros…
Browse files Browse the repository at this point in the history
…oft/component-detection into pauldorsch/updatesyftdocs
  • Loading branch information
pauld-msft committed Nov 18, 2024
2 parents 8c1e809 + 6ab9023 commit 906ab66
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,73 @@ 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);

// for now there is a bug that adds a forward slash to the path for symbolic links. This will be fixed in a future PR if we can parse dependency graph better for those.
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 906ab66

Please sign in to comment.