Skip to content

Commit 15ac17a

Browse files
committed
test: add license test
1 parent 488185b commit 15ac17a

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
"build": "node ./scripts/publish/build.js",
1313
"build:patch": "node ./scripts/patch.js",
1414
"build:packages": "for PKG in packages/*; do echo Building $PKG...; tsc -p $PKG; done",
15-
"test": "npm-run-all -c test:packages test:cli test:deps",
15+
"test": "npm-run-all -c test:packages test:cli test:deps test:licenses",
1616
"e2e": "npm run test:e2e",
1717
"e2e:nightly": "node tests/run_e2e.js --nightly",
1818
"test:e2e": "node tests/run_e2e.js",
1919
"test:cli": "node tests/runner",
2020
"test:deps": "node scripts/publish/validate_dependencies.js",
2121
"test:inspect": "node --inspect --debug-brk tests/runner",
22+
"test:licenses": "node scripts/test-licenses.js",
2223
"test:packages": "node scripts/run-packages-spec.js",
2324
"eslint": "eslint .",
2425
"tslint": "tslint \"**/*.ts\" -c tslint.json -e \"**/tests/**\" -e \"**/blueprints/*/files/**/*.ts\" -e \"node_modules/**\" -e \"tmp/**\" -e \"dist/**\"",
@@ -135,6 +136,7 @@
135136
"express": "^4.14.0",
136137
"jasmine": "^2.4.1",
137138
"jasmine-spec-reporter": "^3.2.0",
139+
"license-checker": "^8.0.3",
138140
"minimist": "^1.2.0",
139141
"mocha": "^3.2.0",
140142
"mock-fs": "^4.0.0",
@@ -145,6 +147,7 @@
145147
"resolve-bin": "^0.4.0",
146148
"rewire": "^2.5.1",
147149
"sinon": "^1.17.3",
150+
"spdx-satisfies": "^0.1.3",
148151
"through": "^2.3.6",
149152
"tree-kill": "^1.0.0",
150153
"ts-node": "^2.0.0",

scripts/test-licenses.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const licenseChecker = require('license-checker');
2+
const spdxSatisfies = require('spdx-satisfies');
3+
const denodeify = require('denodeify');
4+
const licenseCheckerInit = denodeify(licenseChecker.init);
5+
6+
7+
// SPDX defined licenses, see https://spdx.org/licenses/.
8+
const acceptedSpdxLicenses = [
9+
'MIT',
10+
'ISC',
11+
'Apache-2.0',
12+
'BSD-2-Clause',
13+
'BSD-3-Clause',
14+
'BSD-4-Clause',
15+
'CC-BY-3.0',
16+
'CC-BY-4.0',
17+
'Beerware',
18+
'Unlicense'
19+
];
20+
21+
// Name variations of SPDX licenses that some packages have.
22+
const ignoredLicenseVariations = [
23+
'MIT*',
24+
'MIT/X11',
25+
'AFLv2.1',
26+
'AFLv3.0',
27+
'Apache-2.0',
28+
'Apache2',
29+
'BSD',
30+
'BSD*',
31+
'BSD-like',
32+
'NPL',
33+
'L/GPL',
34+
'Public Domain'
35+
];
36+
37+
// Specific packages to ignore, add a reason in a comment. Format: package-name@version.
38+
const ignoredPackages = [
39+
'[email protected]', // old license format, lists `AFLv2.1, BSD`
40+
'[email protected]', // old license format, lists `MIT, Apache2`
41+
'[email protected]' // `Apache License, Version 2.0`, but licence-checker can't handle commas
42+
];
43+
44+
function testSpdx(licenses) {
45+
try {
46+
return spdxSatisfies(licenses, `(${acceptedSpdxLicenses.join(' OR ')})`)
47+
} catch (_) {
48+
return false;
49+
}
50+
}
51+
52+
// Use license-checker first, it can filter more licenses.
53+
licenseCheckerInit({
54+
start: './',
55+
exclude: acceptedSpdxLicenses.concat(ignoredLicenseVariations).join(),
56+
customFormat: { name: '', _location: '' }
57+
})
58+
.then(json => {
59+
let badPackages = Object.keys(json)
60+
.map(key => Object.assign({}, json[key], { id: key }))
61+
// Then run the remaining ones through spdx-satisfies.
62+
.filter(pkg => !testSpdx(pkg.licenses))
63+
.filter(pkg => !ignoredPackages.find(ignored => ignored === pkg.id));
64+
65+
if (badPackages.length > 0) {
66+
console.log('Invalid package licences found:\n');
67+
badPackages.forEach(pkg => console.log(`- ${pkg.id} (${pkg._location}): ${pkg.licenses}`));
68+
process.exit(1);
69+
} else {
70+
console.log('All package licenses are valid.');
71+
}
72+
})
73+
.catch(err => {
74+
console.log(err);
75+
process.exit(1);
76+
});

0 commit comments

Comments
 (0)