2023-04-11 22:42:21 +00:00
|
|
|
import ComponentDetection from "./componentDetection";
|
|
|
|
|
import fs from "fs";
|
2023-01-21 21:56:51 +00:00
|
|
|
|
2023-04-11 22:42:21 +00:00
|
|
|
test("Downloads CLI", async () => {
|
2023-04-07 16:21:17 -07:00
|
|
|
await ComponentDetection.downloadLatestRelease();
|
2023-01-22 20:19:41 +00:00
|
|
|
expect(fs.existsSync(ComponentDetection.componentDetectionPath));
|
2023-01-21 21:56:51 +00:00
|
|
|
});
|
|
|
|
|
|
2023-04-11 22:42:21 +00:00
|
|
|
test("Runs CLI", async () => {
|
2023-04-07 16:21:17 -07:00
|
|
|
await ComponentDetection.downloadLatestRelease();
|
2023-04-11 22:42:21 +00:00
|
|
|
await ComponentDetection.runComponentDetection("./test");
|
2023-04-07 16:21:17 -07:00
|
|
|
expect(fs.existsSync(ComponentDetection.outputPath));
|
2025-04-07 10:39:02 +00:00
|
|
|
}, 10000);
|
2023-01-22 01:06:08 +00:00
|
|
|
|
2023-04-11 22:42:21 +00:00
|
|
|
test("Parses CLI output", async () => {
|
2023-04-07 16:21:17 -07:00
|
|
|
await ComponentDetection.downloadLatestRelease();
|
2023-04-11 22:42:21 +00:00
|
|
|
await ComponentDetection.runComponentDetection("./test");
|
2023-01-22 20:19:41 +00:00
|
|
|
var manifests = await ComponentDetection.getManifestsFromResults();
|
|
|
|
|
expect(manifests?.length == 2);
|
2023-04-11 22:42:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("ComponentDetection.makePackageUrl", () => {
|
2023-06-02 16:07:51 +00:00
|
|
|
test("returns a valid package url from saturated object", () => {
|
2023-04-11 22:42:21 +00:00
|
|
|
const packageUrl = ComponentDetection.makePackageUrl({
|
|
|
|
|
Scheme: "pkg",
|
|
|
|
|
Type: "npm",
|
|
|
|
|
Namespace: "github",
|
|
|
|
|
Name: "component-detection-action",
|
|
|
|
|
Version: "0.0.2",
|
|
|
|
|
Qualifiers: {
|
|
|
|
|
arch: "amd64",
|
|
|
|
|
os: "linux",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
expect(packageUrl).toBe(
|
|
|
|
|
"pkg:npm/github/component-detection-action@0.0.2?arch=amd64&os=linux"
|
|
|
|
|
);
|
|
|
|
|
});
|
2023-06-02 16:07:51 +00:00
|
|
|
|
|
|
|
|
test("returns valid package url without dangling ? with empty qualifers", () => {
|
|
|
|
|
const packageUrl = ComponentDetection.makePackageUrl({
|
|
|
|
|
Scheme: "pkg",
|
|
|
|
|
Type: "npm",
|
|
|
|
|
Namespace: "github",
|
|
|
|
|
Name: "component-detection-action",
|
|
|
|
|
Version: "0.0.2",
|
|
|
|
|
Qualifiers: { },
|
|
|
|
|
});
|
|
|
|
|
expect(packageUrl).toBe(
|
|
|
|
|
"pkg:npm/github/component-detection-action@0.0.2"
|
|
|
|
|
);
|
|
|
|
|
});
|
2025-05-12 22:05:43 +00:00
|
|
|
|
|
|
|
|
test("returns an empty string when packageUrlJson is null", () => {
|
|
|
|
|
const packageUrl = ComponentDetection.makePackageUrl(null);
|
|
|
|
|
expect(packageUrl).toBe("");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("returns an empty string for null packageUrlJson properties", () => {
|
|
|
|
|
const packageUrl = ComponentDetection.makePackageUrl({
|
|
|
|
|
Scheme: null,
|
|
|
|
|
Type: null,
|
|
|
|
|
Namespace: null,
|
|
|
|
|
Name: null,
|
|
|
|
|
Version: null,
|
|
|
|
|
Qualifiers: null
|
|
|
|
|
});
|
|
|
|
|
expect(packageUrl).toBe("");
|
|
|
|
|
});
|
2023-04-11 22:42:21 +00:00
|
|
|
});
|
2025-06-19 12:14:02 +01:00
|
|
|
|
|
|
|
|
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/test-package@1.0.0",
|
|
|
|
|
isDevelopmentDependency: false,
|
|
|
|
|
topLevelReferrers: [],
|
|
|
|
|
locationsFoundAt: ["package.json"],
|
|
|
|
|
containerDetailIds: [],
|
|
|
|
|
containerLayerIds: [],
|
|
|
|
|
packageID: () => "pkg:npm/test-package@1.0.0",
|
|
|
|
|
packageURL: { toString: () => "pkg:npm/test-package@1.0.0" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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/test-package@2.0.0",
|
|
|
|
|
isDevelopmentDependency: false,
|
|
|
|
|
topLevelReferrers: [{ packageUrl: "pkg:npm/parent-package@1.0.0" }],
|
|
|
|
|
locationsFoundAt: ["package.json"],
|
|
|
|
|
containerDetailIds: [],
|
|
|
|
|
containerLayerIds: [],
|
|
|
|
|
packageID: () => "pkg:npm/test-package@2.0.0",
|
|
|
|
|
packageURL: { toString: () => "pkg:npm/test-package@2.0.0" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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/test-package@3.0.0",
|
|
|
|
|
isDevelopmentDependency: false,
|
|
|
|
|
topLevelReferrers: [],
|
|
|
|
|
locationsFoundAt: ["package.json"],
|
|
|
|
|
containerDetailIds: [],
|
|
|
|
|
containerLayerIds: [],
|
|
|
|
|
packageID: () => "pkg:npm/test-package@3.0.0",
|
|
|
|
|
packageURL: { toString: () => "pkg:npm/test-package@3.0.0" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ComponentDetection.addPackagesToManifests([mockPackage] as any, manifests);
|
|
|
|
|
|
|
|
|
|
expect(manifests).toHaveLength(1);
|
|
|
|
|
expect(manifests[0]).toBe(existingManifest);
|
|
|
|
|
expect(directDependencyCallCount).toBe(1);
|
|
|
|
|
expect(indirectDependencyCallCount).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
});
|