Extract addPackagesToManifests to unit test
There is a but here we would like to test
This commit is contained in:
@@ -68,3 +68,78 @@ describe("ComponentDetection.makePackageUrl", () => {
|
|||||||
expect(packageUrl).toBe("");
|
expect(packageUrl).toBe("");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("ComponentDetection.addPackagesToManifests", () => {
|
||||||
|
test("adds package as direct dependency when no top level referrers", () => {
|
||||||
|
const manifests: any[] = [];
|
||||||
|
|
||||||
|
const mockPackage = {
|
||||||
|
id: "test-package-1",
|
||||||
|
packageUrl: "pkg:npm/[email protected]",
|
||||||
|
isDevelopmentDependency: false,
|
||||||
|
topLevelReferrers: [],
|
||||||
|
locationsFoundAt: ["package.json"],
|
||||||
|
containerDetailIds: [],
|
||||||
|
containerLayerIds: [],
|
||||||
|
packageID: () => "pkg:npm/[email protected]",
|
||||||
|
packageURL: { toString: () => "pkg:npm/[email protected]" }
|
||||||
|
};
|
||||||
|
|
||||||
|
ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests);
|
||||||
|
|
||||||
|
expect(manifests).toHaveLength(1);
|
||||||
|
expect(manifests[0].name).toBe("package.json");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("adds package as indirect dependency when has top level referrers", () => {
|
||||||
|
const manifests: any[] = [];
|
||||||
|
|
||||||
|
const mockPackage = {
|
||||||
|
id: "test-package-2",
|
||||||
|
packageUrl: "pkg:npm/[email protected]",
|
||||||
|
isDevelopmentDependency: false,
|
||||||
|
topLevelReferrers: [{ packageUrl: "pkg:npm/[email protected]" }],
|
||||||
|
locationsFoundAt: ["package.json"],
|
||||||
|
containerDetailIds: [],
|
||||||
|
containerLayerIds: [],
|
||||||
|
packageID: () => "pkg:npm/[email protected]",
|
||||||
|
packageURL: { toString: () => "pkg:npm/[email protected]" }
|
||||||
|
};
|
||||||
|
|
||||||
|
ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests);
|
||||||
|
|
||||||
|
expect(manifests).toHaveLength(1);
|
||||||
|
expect(manifests[0].name).toBe("package.json");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("reuses existing manifest when same location found", () => {
|
||||||
|
let directDependencyCallCount = 0;
|
||||||
|
let indirectDependencyCallCount = 0;
|
||||||
|
|
||||||
|
const existingManifest = {
|
||||||
|
name: "package.json",
|
||||||
|
addDirectDependency: () => { directDependencyCallCount++; },
|
||||||
|
addIndirectDependency: () => { indirectDependencyCallCount++; }
|
||||||
|
};
|
||||||
|
const manifests: any[] = [existingManifest];
|
||||||
|
|
||||||
|
const mockPackage = {
|
||||||
|
id: "test-package-3",
|
||||||
|
packageUrl: "pkg:npm/[email protected]",
|
||||||
|
isDevelopmentDependency: false,
|
||||||
|
topLevelReferrers: [],
|
||||||
|
locationsFoundAt: ["package.json"],
|
||||||
|
containerDetailIds: [],
|
||||||
|
containerLayerIds: [],
|
||||||
|
packageID: () => "pkg:npm/[email protected]",
|
||||||
|
packageURL: { toString: () => "pkg:npm/[email protected]" }
|
||||||
|
};
|
||||||
|
|
||||||
|
ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests);
|
||||||
|
|
||||||
|
expect(manifests).toHaveLength(1);
|
||||||
|
expect(manifests[0]).toBe(existingManifest);
|
||||||
|
expect(directDependencyCallCount).toBe(1);
|
||||||
|
expect(indirectDependencyCallCount).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -135,6 +135,12 @@ export default class ComponentDetection {
|
|||||||
const manifests: Array<Manifest> = [];
|
const manifests: Array<Manifest> = [];
|
||||||
|
|
||||||
// Check the locationsFoundAt for every package and add each as a manifest
|
// Check the locationsFoundAt for every package and add each as a manifest
|
||||||
|
this.addPackagesToManifests(packages, manifests);
|
||||||
|
|
||||||
|
return manifests;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static addPackagesToManifests(packages: Array<ComponentDetectionPackage>, manifests: Array<Manifest>): void {
|
||||||
packages.forEach(async (pkg: ComponentDetectionPackage) => {
|
packages.forEach(async (pkg: ComponentDetectionPackage) => {
|
||||||
pkg.locationsFoundAt.forEach(async (location: any) => {
|
pkg.locationsFoundAt.forEach(async (location: any) => {
|
||||||
if (!manifests.find((manifest: Manifest) => manifest.name == location)) {
|
if (!manifests.find((manifest: Manifest) => manifest.name == location)) {
|
||||||
@@ -148,7 +154,6 @@ export default class ComponentDetection {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return manifests;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getDependencyScope(pkg: ComponentDetectionPackage) {
|
private static getDependencyScope(pkg: ComponentDetectionPackage) {
|
||||||
|
|||||||
Reference in New Issue
Block a user