Skip to content

Commit

Permalink
chore: fixed unit tests after license parsing refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz-pluszczewski committed Dec 9, 2024
1 parent 4f5259a commit c627f78
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ describe("extractLicensesFromExpression", () => {

const result = extractLicensesFromExpression(parsedLicense);

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

it("should extract a single license from a simple expression", () => {
Expand All @@ -35,7 +38,9 @@ describe("extractLicensesFromExpression", () => {

const result = extractLicensesFromExpression(parsedLicense);

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

it("should extract multiple licenses from a conjunction expression", () => {
Expand All @@ -52,7 +57,10 @@ describe("extractLicensesFromExpression", () => {

const result = extractLicensesFromExpression(parsedLicense);

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

it("should handle nested conjunction and alternative expressions", () => {
Expand All @@ -70,7 +78,12 @@ describe("extractLicensesFromExpression", () => {

const result = extractLicensesFromExpression(parsedLicense);

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

it("should log an error if a license is not found", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,31 @@ describe("retrieveLicenseFromLicenseFileContent", () => {
const content = "MIT";
const expectedLicense = LicenseSchema.parse(licenseMap.get("MIT"));
const result = retrieveLicenseFromLicenseFileContent(content);
expect(result.licenses).toEqual([expectedLicense]);
expect(result.licenses).toEqual([
{ ...expectedLicense, source: "license-file-content-keywords" },
]);
});

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);
expect(result.licenses).toEqual([expectedLicense]);
expect(result.licenses).toEqual([
{ ...expectedLicense, source: "license-file-content-keywords" },
]);
});

it("should return multiple licenses when content matches multiple license keys or names", () => {
const content = "MIT, Apache-2.0";
const expectedLicenses = [
LicenseSchema.parse(licenseMap.get("MIT")),
LicenseSchema.parse(licenseMap.get("Apache-2.0")),
{
...LicenseSchema.parse(licenseMap.get("MIT")),
source: "license-file-content-keywords",
},
{
...LicenseSchema.parse(licenseMap.get("Apache-2.0")),
source: "license-file-content-keywords",
},
].sort((a, b) => a.name.localeCompare(b.name));
const result = retrieveLicenseFromLicenseFileContent(content);
const sortedLicenses = result.licenses.sort((a, b) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ describe("findLicenseInPackageJson", () => {

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

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

it("should return licenses from the 'licenses' field as an array of objects", () => {
Expand All @@ -39,7 +41,10 @@ describe("findLicenseInPackageJson", () => {
const mitLicense = licenseMap.get("MIT");
const iscLicense = licenseMap.get("ISC");

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

it("should return licenses from the 'licenses' field as an array of strings", () => {
Expand All @@ -54,7 +59,10 @@ describe("findLicenseInPackageJson", () => {
const mitLicense = licenseMap.get("MIT");
const iscLicense = licenseMap.get("ISC");

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

it("should return an empty array when neither 'license' nor 'licenses' fields are present", () => {
Expand Down

0 comments on commit c627f78

Please sign in to comment.