Skip to content

Commit caa69e1

Browse files
committed
Extract addPackagesToManifests to unit test
There is a but here we would like to test
1 parent 04aaaf6 commit caa69e1

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

componentDetection.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,78 @@ describe("ComponentDetection.makePackageUrl", () => {
6868
expect(packageUrl).toBe("");
6969
});
7070
});
71+
72+
describe("ComponentDetection.addPackagesToManifests", () => {
73+
test("adds package as direct dependency when no top level referrers", () => {
74+
const manifests: any[] = [];
75+
76+
const mockPackage = {
77+
id: "test-package-1",
78+
packageUrl: "pkg:npm/test-package@1.0.0",
79+
isDevelopmentDependency: false,
80+
topLevelReferrers: [],
81+
locationsFoundAt: ["package.json"],
82+
containerDetailIds: [],
83+
containerLayerIds: [],
84+
packageID: () => "pkg:npm/test-package@1.0.0",
85+
packageURL: { toString: () => "pkg:npm/test-package@1.0.0" }
86+
};
87+
88+
ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests);
89+
90+
expect(manifests).toHaveLength(1);
91+
expect(manifests[0].name).toBe("package.json");
92+
});
93+
94+
test("adds package as indirect dependency when has top level referrers", () => {
95+
const manifests: any[] = [];
96+
97+
const mockPackage = {
98+
id: "test-package-2",
99+
packageUrl: "pkg:npm/test-package@2.0.0",
100+
isDevelopmentDependency: false,
101+
topLevelReferrers: [{ packageUrl: "pkg:npm/parent-package@1.0.0" }],
102+
locationsFoundAt: ["package.json"],
103+
containerDetailIds: [],
104+
containerLayerIds: [],
105+
packageID: () => "pkg:npm/test-package@2.0.0",
106+
packageURL: { toString: () => "pkg:npm/test-package@2.0.0" }
107+
};
108+
109+
ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests);
110+
111+
expect(manifests).toHaveLength(1);
112+
expect(manifests[0].name).toBe("package.json");
113+
});
114+
115+
test("reuses existing manifest when same location found", () => {
116+
let directDependencyCallCount = 0;
117+
let indirectDependencyCallCount = 0;
118+
119+
const existingManifest = {
120+
name: "package.json",
121+
addDirectDependency: () => { directDependencyCallCount++; },
122+
addIndirectDependency: () => { indirectDependencyCallCount++; }
123+
};
124+
const manifests: any[] = [existingManifest];
125+
126+
const mockPackage = {
127+
id: "test-package-3",
128+
packageUrl: "pkg:npm/test-package@3.0.0",
129+
isDevelopmentDependency: false,
130+
topLevelReferrers: [],
131+
locationsFoundAt: ["package.json"],
132+
containerDetailIds: [],
133+
containerLayerIds: [],
134+
packageID: () => "pkg:npm/test-package@3.0.0",
135+
packageURL: { toString: () => "pkg:npm/test-package@3.0.0" }
136+
};
137+
138+
ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests);
139+
140+
expect(manifests).toHaveLength(1);
141+
expect(manifests[0]).toBe(existingManifest);
142+
expect(directDependencyCallCount).toBe(1);
143+
expect(indirectDependencyCallCount).toBe(0);
144+
});
145+
});

componentDetection.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ export default class ComponentDetection {
135135
const manifests: Array<Manifest> = [];
136136

137137
// Check the locationsFoundAt for every package and add each as a manifest
138+
this.addPackagesToManifests(packages, manifests);
139+
140+
return manifests;
141+
}
142+
143+
public static addPackagesToManifests(packages: Array<ComponentDetectionPackage>, manifests: Array<Manifest>): void {
138144
packages.forEach(async (pkg: ComponentDetectionPackage) => {
139145
pkg.locationsFoundAt.forEach(async (location: any) => {
140146
if (!manifests.find((manifest: Manifest) => manifest.name == location)) {
@@ -148,7 +154,6 @@ export default class ComponentDetection {
148154
}
149155
});
150156
});
151-
return manifests;
152157
}
153158

154159
private static getDependencyScope(pkg: ComponentDetectionPackage) {

0 commit comments

Comments
 (0)