Skip to content

Commit

Permalink
feat(core, cli): add licensePath to license field (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
F-Kublin authored Jan 7, 2025
1 parent 733a029 commit d561c3c
Show file tree
Hide file tree
Showing 13 changed files with 3,742 additions and 1,465 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function VerboseView({ result, filter }: VerboseViewProps) {
self.findIndex((l) => l.licenseId === license.licenseId) ===
index,
)
.map((license) => license.licenseId)
.map((license) => `${license.licenseId}: ${license.licensePath}`)
.join(", "),
deprecated: detectedLicense.licenses.some(
(license) => license.isDeprecatedLicenseId,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/license-finder/add-license-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import type {
export const addLicenseSource = (
licenses: License[],
source: LicenseSource,
licensePath: string,
): LicenseWithSource[] => {
return licenses.map((license) => ({
...license,
licensePath,
source,
}));
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ describe("extractLicensesFromExpression", () => {
const mitLicense = licenseMap.get("MIT");
const apacheLicense = licenseMap.get("Apache-2.0");

const result = extractLicensesFromExpression(parsedLicense);
const result = extractLicensesFromExpression(
parsedLicense,
"/path/to/package.json",
);

expect(result).toEqual([
{ ...mitLicense, source: "package.json-license-expression" },
{ ...apacheLicense, source: "package.json-license-expression" },
{
...mitLicense,
source: "package.json-license-expression",
licensePath: "/path/to/package.json",
},
{
...apacheLicense,
source: "package.json-license-expression",
licensePath: "/path/to/package.json",
},
]);
});

Expand All @@ -36,10 +47,17 @@ describe("extractLicensesFromExpression", () => {

const mitLicense = licenseMap.get("MIT");

const result = extractLicensesFromExpression(parsedLicense);
const result = extractLicensesFromExpression(
parsedLicense,
"/path/to/package.json",
);

expect(result).toEqual([
{ ...mitLicense, source: "package.json-license-expression" },
{
...mitLicense,
source: "package.json-license-expression",
licensePath: "/path/to/package.json",
},
]);
});

Expand All @@ -55,11 +73,22 @@ describe("extractLicensesFromExpression", () => {
const mitLicense = licenseMap.get("MIT");
const iscLicense = licenseMap.get("ISC");

const result = extractLicensesFromExpression(parsedLicense);
const result = extractLicensesFromExpression(
parsedLicense,
"/path/to/package.json",
);

expect(result).toEqual([
{ ...mitLicense, source: "package.json-license-expression" },
{ ...iscLicense, source: "package.json-license-expression" },
{
...mitLicense,
source: "package.json-license-expression",
licensePath: "/path/to/package.json",
},
{
...iscLicense,
source: "package.json-license-expression",
licensePath: "/path/to/package.json",
},
]);
});

Expand All @@ -76,13 +105,28 @@ describe("extractLicensesFromExpression", () => {
const iscLicense = licenseMap.get("ISC");
const apacheLicense = licenseMap.get("Apache-2.0");

const result = extractLicensesFromExpression(parsedLicense);
const result = extractLicensesFromExpression(
parsedLicense,
"/path/to/package.json",
);

// @ts-ignore
expect(result).toEqual([
{ ...mitLicense, source: "package.json-license-expression" },
{ ...iscLicense, source: "package.json-license-expression" },
{ ...apacheLicense, source: "package.json-license-expression" },
{
...mitLicense,
source: "package.json-license-expression",
licensePath: "/path/to/package.json",
},
{
...iscLicense,
source: "package.json-license-expression",
licensePath: "/path/to/package.json",
},
{
...apacheLicense,
source: "package.json-license-expression",
licensePath: "/path/to/package.json",
},
]);
});

Expand All @@ -97,7 +141,7 @@ describe("extractLicensesFromExpression", () => {
right: { license: "Wrong-2.0" },
} as Info;

extractLicensesFromExpression(parsedLicense);
extractLicensesFromExpression(parsedLicense, "/path/to/package.json");

expect(consoleErrorSpy).toHaveBeenCalledWith(
"Failed to find license:",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { findLicenseById } from "./find-license-by-id.js";

export function extractLicensesFromExpression(
licenseExpressionParsed: Info,
packageJsonPath: string,
): LicenseWithSource[] {
const licenses = new Set<LicenseWithSource>();

Expand All @@ -14,6 +15,7 @@ export function extractLicensesFromExpression(
licenses.add({
...license,
source: LICENSE_SOURCE.packageJsonLicensesExpression,
licensePath: packageJsonPath,
});
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,42 @@ import { retrieveLicenseFromLicenseFileContent } from "./find-license-in-license
describe("retrieveLicenseFromLicenseFileContent", () => {
it("should return an empty array when content does not match any licenses", () => {
const content = "This is some random content without any license keywords.";
const result = retrieveLicenseFromLicenseFileContent(content);
const result = retrieveLicenseFromLicenseFileContent(
content,
"/path/to/LICENSE",
);
expect(result.licenses).toEqual([]);
});

it("should return the correct license when content matches a license key", () => {
const content = "MIT";
const expectedLicense = LicenseSchema.parse(licenseMap.get("MIT"));
const result = retrieveLicenseFromLicenseFileContent(content);
const result = retrieveLicenseFromLicenseFileContent(
content,
"/path/to/LICENSE",
);
expect(result.licenses).toEqual([
{ ...expectedLicense, source: "license-file-content-keywords" },
{
...expectedLicense,
source: "license-file-content-keywords",
licensePath: "/path/to/LICENSE",
},
]);
});

it("should return the correct license when content matches a license name", () => {
const content = "MIT License";
const expectedLicense = LicenseSchema.parse(licenseMap.get("MIT"));
const result = retrieveLicenseFromLicenseFileContent(content);
const result = retrieveLicenseFromLicenseFileContent(
content,
"/path/to/LICENSE",
);
expect(result.licenses).toEqual([
{ ...expectedLicense, source: "license-file-content-keywords" },
{
...expectedLicense,
source: "license-file-content-keywords",
licensePath: "/path/to/LICENSE",
},
]);
});

Expand All @@ -33,13 +50,18 @@ describe("retrieveLicenseFromLicenseFileContent", () => {
{
...LicenseSchema.parse(licenseMap.get("MIT")),
source: "license-file-content-keywords",
licensePath: "/path/to/LICENSE",
},
{
...LicenseSchema.parse(licenseMap.get("Apache-2.0")),
source: "license-file-content-keywords",
licensePath: "/path/to/LICENSE",
},
].sort((a, b) => a.name.localeCompare(b.name));
const result = retrieveLicenseFromLicenseFileContent(content);
const result = retrieveLicenseFromLicenseFileContent(
content,
"/path/to/LICENSE",
);
const sortedLicenses = result.licenses.sort((a, b) =>
a.name.localeCompare(b.name),
);
Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/license-finder/find-license-in-license-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { addLicenseSource } from "./add-license-source.js";
import { detectLicenses } from "./detect-from-license-content.js";
import type { LicensesWithPathAndStatus } from "./licenses-with-path.js";

export function retrieveLicenseFromLicenseFileContent(content: string): {
export function retrieveLicenseFromLicenseFileContent(
content: string,
licensePath: string,
): {
licenses: LicenseWithSource[];
} {
const detectedLicenses = detectLicenses(content);
Expand All @@ -21,7 +24,11 @@ export function retrieveLicenseFromLicenseFileContent(content: string): {
.filter(([key]) => key === detectedLicense.licenseId)
.map((result) => LicenseSchema.parse(result[1]));
return {
licenses: addLicenseSource(licenseArr, LICENSE_SOURCE.licenseFileContent),
licenses: addLicenseSource(
licenseArr,
LICENSE_SOURCE.licenseFileContent,
licensePath,
),
};
}

Expand All @@ -38,6 +45,7 @@ export function retrieveLicenseFromLicenseFileContent(content: string): {
licenses: addLicenseSource(
licenseArr,
LICENSE_SOURCE.licenseFileContextKeywords,
licensePath,
),
};
}
Expand All @@ -54,7 +62,7 @@ export async function findLicenseInLicenseFile(filePath: string): Promise<{
};
}

const result = retrieveLicenseFromLicenseFileContent(content);
const result = retrieveLicenseFromLicenseFileContent(content, filePath);

return {
licenses: result.licenses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ describe("findLicenseInPackageJson", () => {
license: "MIT",
};

const result = findLicenseInPackageJson(packageJson);
const result = findLicenseInPackageJson(
packageJson,
"/path/to/package.json",
);

const expectedLicense = licenseMap.get("MIT");

expect(result.licenses).toEqual([
{ ...expectedLicense, source: "package.json-license" },
{
...expectedLicense,
source: "package.json-license",
licensePath: "/path/to/package.json",
},
]);
});

Expand All @@ -36,14 +43,25 @@ describe("findLicenseInPackageJson", () => {
],
};

const result = findLicenseInPackageJson(packageJson);
const result = findLicenseInPackageJson(
packageJson,
"/path/to/package.json",
);

const mitLicense = licenseMap.get("MIT");
const iscLicense = licenseMap.get("ISC");

expect(result.licenses).toEqual([
{ ...mitLicense, source: "package.json-legacy" },
{ ...iscLicense, source: "package.json-legacy" },
{
...mitLicense,
source: "package.json-legacy",
licensePath: "/path/to/package.json",
},
{
...iscLicense,
source: "package.json-legacy",
licensePath: "/path/to/package.json",
},
]);
});

Expand All @@ -54,14 +72,25 @@ describe("findLicenseInPackageJson", () => {
licenses: ["MIT", "ISC"],
};

const result = findLicenseInPackageJson(packageJson);
const result = findLicenseInPackageJson(
packageJson,
"/path/to/package.json",
);

const mitLicense = licenseMap.get("MIT");
const iscLicense = licenseMap.get("ISC");

expect(result.licenses).toEqual([
{ ...mitLicense, source: "package.json-legacy" },
{ ...iscLicense, source: "package.json-legacy" },
{
...mitLicense,
source: "package.json-legacy",
licensePath: "/path/to/package.json",
},
{
...iscLicense,
source: "package.json-legacy",
licensePath: "/path/to/package.json",
},
]);
});

Expand All @@ -71,7 +100,10 @@ describe("findLicenseInPackageJson", () => {
version: "1.0.0",
};

const result = findLicenseInPackageJson(packageJson);
const result = findLicenseInPackageJson(
packageJson,
"/path/to/package.json",
);
expect(result.licenses).toEqual([]);
});
});
Loading

0 comments on commit d561c3c

Please sign in to comment.