Skip to content

Commit

Permalink
fix: license field issue (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
aalykiot authored Jul 8, 2021
1 parent 96bf3c4 commit 5fb15e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/plugins/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ const licensePlugin = async (pkg, config) => {
// Licenses that will pass but with a warning
const licensesForcePass = config.licenses?.rules[pkg.name]?.override || [];

// We need to check if the the license is defined as a string or an array
const license = Array.isArray(pkg.licenses)
? pkg.licenses.map((v) => v.type).join(',')
: pkg.license;

const isPassing = [...licenses, ...licensesLocal].find((name) =>
matchLicenses(pkg.license, name)
matchLicenses(license, name)
);

const output = stringBuilder('\nChecking top-level license').withPadding(66);
Expand All @@ -22,19 +27,19 @@ const licensePlugin = async (pkg, config) => {
}

const isForcePassing = licensesForcePass.find((name) =>
matchLicenses(pkg.license, name)
matchLicenses(license, name)
);

if (isForcePassing) {
warning(output.get());
return createWarning(
`The module "${pkg.name}" is under the yet undetermined license "${pkg.license}". (Manual review needed)`
`The module "${pkg.name}" is under the yet undetermined license(s) "${license}". (Manual review needed)`
);
}

failure(output.get());
return createError(
`The module "${pkg.name}" is under the non-acceptable license "${pkg.license}".`
`The module "${pkg.name}" is under the non-acceptable license(s) "${license}".`
);
};

Expand Down
26 changes: 26 additions & 0 deletions test/plugins/license.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ it('should return null when license is globally accepted', async () => {
expect(success).toHaveBeenCalled();
});

it('should handle the case where the license field is an array', async () => {
// create a sample env with some dummy data
const testEnv = {
pkg: {
name: 'test',
licenses: [
{
type: 'MIT',
url: 'https://raw.github.com/test/test/master/licence'
}
]
},
config: {
licenses: {
allow: ['MIT', 'Apache-2.0'],
rules: {}
}
}
};

const result = await licensePlugin(testEnv.pkg, testEnv.config);

expect(result).toBe(null);
expect(success).toHaveBeenCalled();
});

it('should return null when the license is locally accepted', async () => {
// create a sample env with some dummy data
const testEnv = {
Expand Down

0 comments on commit 5fb15e7

Please sign in to comment.