Skip to content

Commit b52999a

Browse files
committed
Refactor component comparison logic to exclude experimental detectors and detectors introduced in the new scan
1 parent ade40f8 commit b52999a

1 file changed

Lines changed: 31 additions & 13 deletions

File tree

test/Microsoft.ComponentDetection.VerificationTests/ComponentDetectionIntegrationTests.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,45 +56,58 @@ public void CheckManifestFiles_ExcludingExperimentalDetectors()
5656
// Parse out array of components
5757
// make sure each component id has identical fields.
5858
// if any are lost, error, new ones should come with a bumped detector version, which is checked during the detectors counts test.
59-
var experimentalDetectorsId = this.GetExperimentalDetectorsId(this.newScanResult.DetectorsInScan, this.oldScanResult.DetectorsInScan);
59+
var excludedDetectorIds = this.GetExcludedDetectorIds(this.newScanResult.DetectorsInScan, this.oldScanResult.DetectorsInScan);
6060

61-
var newComponents = this.newScanResult.ComponentsFound.Where(c => !experimentalDetectorsId.Contains(c.DetectorId));
62-
var oldComponents = this.oldScanResult.ComponentsFound.Where(c => !experimentalDetectorsId.Contains(c.DetectorId));
61+
var newComponents = this.newScanResult.ComponentsFound.Where(c => !excludedDetectorIds.Contains(c.DetectorId));
62+
var oldComponents = this.oldScanResult.ComponentsFound.Where(c => !excludedDetectorIds.Contains(c.DetectorId));
6363

6464
var newComponentDictionary = this.GetComponentDictionary(newComponents);
6565
var oldComponentDictionary = this.GetComponentDictionary(oldComponents);
6666
using (new AssertionScope())
6767
{
6868
this.CompareDetectedComponents(oldComponents, newComponentDictionary, "new");
6969
this.CompareDetectedComponents(newComponents, oldComponentDictionary, "old");
70+
7071
var oldGraphs = this.oldScanResult.DependencyGraphs;
7172
var newGraphs = this.newScanResult.DependencyGraphs;
72-
this.CompareGraphs(oldGraphs, newGraphs, "old", "new");
73-
this.CompareGraphs(newGraphs, oldGraphs, "new", "old");
73+
74+
// Only compare graphs for files present in both scans. New or removed detectors
75+
// may introduce file paths that cannot be compared against the other baseline.
76+
var sharedGraphKeys = new HashSet<string>(oldGraphs.Keys.Intersect(newGraphs.Keys));
77+
this.CompareGraphs(oldGraphs, newGraphs, "old", "new", sharedGraphKeys);
78+
this.CompareGraphs(newGraphs, oldGraphs, "new", "old", sharedGraphKeys);
7479
}
7580
}
7681

77-
private ISet<string> GetExperimentalDetectorsId(IEnumerable<Detector> newScanDetectors, IEnumerable<Detector> oldScanDetectors)
82+
/// <summary>
83+
/// Returns detector IDs that should be excluded from component comparison.
84+
/// Excludes experimental detectors and detectors that only exist in one scan
85+
/// (new detectors not yet in the baseline, or removed detectors).
86+
/// </summary>
87+
private ISet<string> GetExcludedDetectorIds(IEnumerable<Detector> newScanDetectors, IEnumerable<Detector> oldScanDetectors)
7888
{
79-
var experimentalDetectorsId = new HashSet<string>();
89+
var excludedIds = new HashSet<string>();
90+
91+
var newDetectorIds = new HashSet<string>(newScanDetectors.Select(d => d.DetectorId));
92+
var oldDetectorIds = new HashSet<string>(oldScanDetectors.Select(d => d.DetectorId));
8093

8194
foreach (var detector in newScanDetectors)
8295
{
83-
if (detector.IsExperimental)
96+
if (detector.IsExperimental || !oldDetectorIds.Contains(detector.DetectorId))
8497
{
85-
experimentalDetectorsId.Add(detector.DetectorId);
98+
excludedIds.Add(detector.DetectorId);
8699
}
87100
}
88101

89102
foreach (var detector in oldScanDetectors)
90103
{
91-
if (detector.IsExperimental)
104+
if (detector.IsExperimental || !newDetectorIds.Contains(detector.DetectorId))
92105
{
93-
experimentalDetectorsId.Add(detector.DetectorId);
106+
excludedIds.Add(detector.DetectorId);
94107
}
95108
}
96109

97-
return experimentalDetectorsId;
110+
return excludedIds;
98111
}
99112

100113
private void CompareDetectedComponents(IEnumerable<ScannedComponent> leftComponents, Dictionary<string, ScannedComponent> rightComponentDictionary, string rightFileName)
@@ -114,10 +127,15 @@ private void CompareDetectedComponents(IEnumerable<ScannedComponent> leftCompone
114127
}
115128
}
116129

117-
private void CompareGraphs(DependencyGraphCollection leftGraphs, DependencyGraphCollection newGraphs, string leftGraphName, string rightGraphName)
130+
private void CompareGraphs(DependencyGraphCollection leftGraphs, DependencyGraphCollection newGraphs, string leftGraphName, string rightGraphName, HashSet<string> includedKeys = null)
118131
{
119132
foreach (var leftGraph in leftGraphs)
120133
{
134+
if (includedKeys != null && !includedKeys.Contains(leftGraph.Key))
135+
{
136+
continue;
137+
}
138+
121139
newGraphs.TryGetValue(leftGraph.Key, out var rightGraph).Should().BeTrue($"File {leftGraph.Key} is in the {leftGraphName} dependency graph, but not in the {rightGraphName} one.");
122140

123141
if (rightGraph == null)

0 commit comments

Comments
 (0)