Skip to content

Commit

Permalink
Add ability to allow a license for a particular package (#135336)
Browse files Browse the repository at this point in the history
  • Loading branch information
hop-dev authored Jun 28, 2022
1 parent 4e3ef39 commit 7955dc4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ describe('tasks/lib/licenses', () => {
}).toThrow(PACKAGE.name);
});

it('throw an error when the packages license is invalid and not overriden', () => {
expect(() => {
assertLicensesValid({
packages: [PACKAGE],
validLicenses: [`not ${PACKAGE.licenses[0]}`],
perPackageOverrides: {
[`${PACKAGE.name}-${PACKAGE.version}`]: [`also not ${PACKAGE.licenses[0]}`],
},
});
}).toThrow(PACKAGE.name);
});

it('should return undefined if the package license is invalid but has an ovveride', () => {
expect(
assertLicensesValid({
packages: [PACKAGE],
validLicenses: [`not ${PACKAGE.licenses[0]}`],
perPackageOverrides: {
[`${PACKAGE.name}-${PACKAGE.version}`]: PACKAGE.licenses,
},
})
).toEqual(undefined);
});

it('throws an error when the package has no licenses', () => {
expect(() => {
assertLicensesValid({
Expand Down
15 changes: 13 additions & 2 deletions src/dev/license_checker/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@ interface Options {
licenses: string[];
}>;
validLicenses: string[];
perPackageOverrides?: Record<string, string[]>;
}

/**
* When given a list of packages and the valid license
* options, either throws an error with details about
* violations or returns undefined.
*/
export function assertLicensesValid({ packages, validLicenses }: Options) {
export function assertLicensesValid({
packages,
validLicenses,
perPackageOverrides = {},
}: Options) {
const invalidMsgs = packages.reduce((acc, pkg) => {
const invalidLicenses = pkg.licenses.filter((license) => !validLicenses.includes(license));
const isValidLicense = (license: string) => validLicenses.includes(license);
const isValidLicenseForPackage = (license: string) =>
(perPackageOverrides[`${pkg.name}-${pkg.version}`] || []).includes(license);

const invalidLicenses = pkg.licenses.filter(
(license) => !isValidLicense(license) && !isValidLicenseForPackage(license)
);

if (pkg.licenses.length && !invalidLicenses.length) {
return acc;
Expand Down

0 comments on commit 7955dc4

Please sign in to comment.