Skip to content

Commit 7bf1182

Browse files
authored
Add Path field to SPDX Detector (#112)
1 parent 05c2119 commit 7bf1182

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/Microsoft.ComponentDetection.Contracts/TypedComponent/SpdxComponent.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ private SpdxComponent()
1010
}
1111

1212
public SpdxComponent(string spdxVersion, Uri documentNamespace, string name, string checksum,
13-
string rootElementId)
13+
string rootElementId, string path)
1414
{
1515
SpdxVersion = ValidateRequiredInput(spdxVersion, nameof(SpdxVersion), nameof(ComponentType.Spdx));
1616
DocumentNamespace =
1717
ValidateRequiredInput(documentNamespace, nameof(DocumentNamespace), nameof(ComponentType.Spdx));
1818
Name = ValidateRequiredInput(name, nameof(Name), nameof(ComponentType.Spdx));
1919
Checksum = ValidateRequiredInput(checksum, nameof(Checksum), nameof(ComponentType.Spdx));
2020
RootElementId = ValidateRequiredInput(rootElementId, nameof(RootElementId), nameof(ComponentType.Spdx));
21+
Path = ValidateRequiredInput(path, nameof(Path), nameof(ComponentType.Spdx));
2122
}
2223

2324
public override ComponentType Type => ComponentType.Spdx;
@@ -32,6 +33,8 @@ public SpdxComponent(string spdxVersion, Uri documentNamespace, string name, str
3233

3334
public string Checksum { get; }
3435

36+
public string Path { get; }
37+
3538
public override string Id => $"{Name}-{SpdxVersion}-{Checksum}";
3639
}
3740
}

src/Microsoft.ComponentDetection.Detectors/spdx/Spdx22ComponentDetector.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ private SpdxComponent ConvertJObjectToSbomComponent(ProcessRequest processReques
102102
}
103103

104104
var rootElementId = rootElements?.FirstOrDefault() ?? "SPDXRef-Document";
105-
var component = new SpdxComponent(spdxVersion, new Uri(sbomNamespace), name, fileHash, rootElementId);
105+
var path = processRequest.ComponentStream.Location;
106+
var component = new SpdxComponent(spdxVersion, new Uri(sbomNamespace), name, fileHash, rootElementId, path);
106107

107108
return component;
108109
}

test/Microsoft.ComponentDetection.Detectors.Tests/SPDX22ComponentDetectorTests.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ namespace Microsoft.ComponentDetection.Detectors.Tests
2020
public class Spdx22ComponentDetectorTests
2121
{
2222
private DetectorTestUtility<Spdx22ComponentDetector> detectorTestUtility;
23+
private string tempPath = Path.GetTempPath();
2324

2425
[TestInitialize]
2526
public void TestInitialize()
2627
{
2728
var componentRecorder = new ComponentRecorder(enableManualTrackingOfExplicitReferences: false);
2829
detectorTestUtility = DetectorTestUtilityCreator.Create<Spdx22ComponentDetector>()
29-
.WithScanRequest(new ScanRequest(new DirectoryInfo(Path.GetTempPath()), null, null, new Dictionary<string, string>(), null, componentRecorder));
30+
.WithScanRequest(new ScanRequest(new DirectoryInfo(tempPath), null, null, new Dictionary<string, string>(), null, componentRecorder));
3031
}
3132

3233
[TestMethod]
@@ -96,8 +97,9 @@ public async Task TestSbomDetector_SimpleSbom()
9697
]
9798
}";
9899

100+
var spdxFileName = "manifest.spdx.json";
99101
var (scanResult, componentRecorder) = await detectorTestUtility
100-
.WithFile("manifest.spdx.json", spdxFile)
102+
.WithFile(spdxFileName, spdxFile)
101103
.ExecuteDetector();
102104

103105
Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
@@ -122,6 +124,7 @@ public async Task TestSbomDetector_SimpleSbom()
122124
Assert.AreEqual(sbomComponent.DocumentNamespace, new Uri("https://sbom.microsoft/Test/1.0.0/61de1a5-57cc-4732-9af5-edb321b4a7ee"));
123125
Assert.AreEqual(sbomComponent.SpdxVersion, "SPDX-2.2");
124126
Assert.AreEqual(sbomComponent.Checksum, checksum);
127+
Assert.AreEqual(sbomComponent.Path, Path.Combine(tempPath, spdxFileName));
125128
}
126129

127130
[TestMethod]

0 commit comments

Comments
 (0)