From c9b602b39548729a28f3e12ca371175b0045022c Mon Sep 17 00:00:00 2001 From: Kim Joar Bekkelund Date: Fri, 2 Feb 2018 21:55:34 +0100 Subject: [PATCH] Handle licenses for 'private: true' packages (#16480) (#16494) --- tasks/lib/licenses/__tests__/valid.js | 20 +++++++++++++++++++- tasks/lib/licenses/valid.js | 9 +++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tasks/lib/licenses/__tests__/valid.js b/tasks/lib/licenses/__tests__/valid.js index f31326036f9a..47387e38c559 100644 --- a/tasks/lib/licenses/__tests__/valid.js +++ b/tasks/lib/licenses/__tests__/valid.js @@ -4,7 +4,8 @@ import expect from 'expect.js'; import { assertLicensesValid } from '../valid'; -const NODE_MODULES = resolve(__dirname, '../../../../node_modules'); +const ROOT = resolve(__dirname, '../../../../'); +const NODE_MODULES = resolve(ROOT, './node_modules'); const PACKAGE = { name: '@elastic/httpolyglot', @@ -14,6 +15,16 @@ const PACKAGE = { relative: 'node_modules/@elastic/httpolyglot', }; +const INTERNAL_PACKAGE = { + name: '@kbn/internal', + version: '1.0.0', + // `license-checker` marks `private: true` packages as "unlicensed" _even_ if + // you add a `license` field to its `package.json` + licenses: ['UNLICENSED'], + directory: resolve(ROOT, 'packages/kbn-internal'), + relative: 'packages/kbn-internal', +}; + describe('tasks/lib/licenses', () => { describe('assertLicensesValid()', () => { it('returns undefined when package has valid license', () => { @@ -23,6 +34,13 @@ describe('tasks/lib/licenses', () => { })).to.be(undefined); }); + it('returns undefined if internal package that is marked as "UNLICENSED"', () => { + expect(assertLicensesValid({ + packages: [INTERNAL_PACKAGE], + validLicenses: ['MIT', 'Apache-2.0'] + })).to.be(undefined); + }); + it('throw an error when the packages license is invalid', () => { expect(() => { assertLicensesValid({ diff --git a/tasks/lib/licenses/valid.js b/tasks/lib/licenses/valid.js index 13719e7c8c63..04a562f930fe 100644 --- a/tasks/lib/licenses/valid.js +++ b/tasks/lib/licenses/valid.js @@ -33,11 +33,20 @@ export function assertLicensesValid(options = {}) { licenses.filter(license => !validLicenses.includes(license)) ); + // If a package is not located in `node_modules`, we know it's a package from + // within the Kibana repo. The reason we need to exclude these when checking + // for valid licenses , is that our `license-checker` dependency marks all + // packages that have `private: true` in their `package.json` as "UNLICENSED". + const isInternalPackage = pkg => ( + !pkg.relative.includes('node_modules/') + ); + const isPackageInvalid = pkg => ( !pkg.licenses.length || getInvalid(pkg.licenses).length > 0 ); const invalidMsgs = packages + .filter(pkg => !isInternalPackage(pkg)) .filter(isPackageInvalid) .map(describeInvalidLicenses(getInvalid));