Skip to content

Commit

Permalink
fix: Added support for the UNLICENSED NPM magic value (fixes #113) (#134
Browse files Browse the repository at this point in the history
)

* added handling of the UNLICENSED edge case

* added tests for UNLICENSED being non-green
  • Loading branch information
mat-sz authored May 18, 2020
1 parent fb3aeba commit c7966eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ export class LicenseChecker extends EventEmitter {
}

private correctLicenseName(license: string): string | null {
// NPM specific value.
if (license === 'UNLICENSED' || license === 'UNLICENCED') {
console.warn(`Unlicensed package, specified license: ${license}`);
return 'UNLICENSED';
}

const corrected = spdxCorrect(license);
if (this.opts.verbose && corrected && corrected !== license) {
console.warn(`Correcting ${license} to ${corrected}`);
Expand Down
37 changes: 37 additions & 0 deletions test/checker-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,41 @@ describe(__filename, () => {
}
);
});

it('treats UNLICENSED as non-green', () => {
const primaryPackageJson = JSON.stringify({
name: 'hello',
version: '1.0.0',
license: 'UNLICENSED',
});

const pathToPrimary = path.join('path', 'to', 'primary');
return withFixtures(
{
[pathToPrimary]: {
'package.json': primaryPackageJson,
},
},
async () => {
requestedPackages = [];
const nonGreenPackages: string[] = [];
const packageJsonPaths: string[] = [];
const checker = new LicenseChecker();
checker
.on('non-green-license', arg => {
nonGreenPackages.push(`${arg.packageName}@${arg.version}`);
})
.on('package.json', filePath => {
packageJsonPaths.push(filePath);
});
await checker.checkLocalDirectory(pathToPrimary);
console.log('requested packages: ', requestedPackages);
assert.deepStrictEqual(requestedPackages, []);
assert.deepStrictEqual(nonGreenPackages, ['hello@1.0.0']);
assert.deepStrictEqual(packageJsonPaths, [
path.join(pathToPrimary, 'package.json'),
]);
}
);
});
});

0 comments on commit c7966eb

Please sign in to comment.