diff --git a/tasks/lib/packages/__tests__/fixtures/fixture1/index.js b/tasks/lib/packages/__tests__/fixtures/fixture1/index.js new file mode 100644 index 0000000000000..24b00f58ebd36 --- /dev/null +++ b/tasks/lib/packages/__tests__/fixtures/fixture1/index.js @@ -0,0 +1 @@ +console.log('I am fixture 1'); diff --git a/tasks/lib/packages/__tests__/fixtures/fixture1/node_modules/dep1/index.js b/tasks/lib/packages/__tests__/fixtures/fixture1/node_modules/dep1/index.js new file mode 100644 index 0000000000000..17aefb3c74b91 --- /dev/null +++ b/tasks/lib/packages/__tests__/fixtures/fixture1/node_modules/dep1/index.js @@ -0,0 +1 @@ +console.log('I am dep 1'); diff --git a/tasks/lib/packages/__tests__/fixtures/fixture1/node_modules/dep1/package.json b/tasks/lib/packages/__tests__/fixtures/fixture1/node_modules/dep1/package.json new file mode 100644 index 0000000000000..a2ccb021abcb9 --- /dev/null +++ b/tasks/lib/packages/__tests__/fixtures/fixture1/node_modules/dep1/package.json @@ -0,0 +1,5 @@ +{ + "name": "dep1", + "version": "0.0.2", + "license": "Apache-2.0" +} diff --git a/tasks/lib/packages/__tests__/fixtures/fixture1/package.json b/tasks/lib/packages/__tests__/fixtures/fixture1/package.json new file mode 100644 index 0000000000000..d2d533591bcfd --- /dev/null +++ b/tasks/lib/packages/__tests__/fixtures/fixture1/package.json @@ -0,0 +1,8 @@ +{ + "name": "fixture1", + "version": "0.0.1", + "license": "MIT", + "dependencies": { + "dep1": "0.0.2" + } +} diff --git a/tasks/lib/packages/__tests__/installed_packages.js b/tasks/lib/packages/__tests__/installed_packages.js new file mode 100644 index 0000000000000..4c47412a2a1f2 --- /dev/null +++ b/tasks/lib/packages/__tests__/installed_packages.js @@ -0,0 +1,59 @@ +import { resolve } from 'path'; + +import { uniq } from 'lodash'; +import expect from 'expect.js'; + +import { getInstalledPackages } from '../installed_packages'; + +const KIBANA_ROOT = resolve(__dirname, '../../../../'); +const FIXTURE1_ROOT = resolve(__dirname, 'fixtures/fixture1'); + +describe('tasks/lib/packages', () => { + describe('getInstalledPackages()', function () { + + let kibanaPackages; + let fixture1Packages; + before(async function () { + this.timeout(30 * 1000); + [kibanaPackages, fixture1Packages] = await Promise.all([ + getInstalledPackages({ + directory: KIBANA_ROOT + }), + getInstalledPackages({ + directory: FIXTURE1_ROOT + }), + ]); + }); + + it('requires a directory', async () => { + try { + await getInstalledPackages({}); + throw new Error('expected getInstalledPackages() to reject'); + } catch (err) { + expect(err.message).to.contain('directory'); + } + }); + + it('reads all installed packages of a module', () => { + expect(fixture1Packages).to.eql([ + { + name: 'dep1', + version: '0.0.2', + licenses: [ 'Apache-2.0' ], + directory: resolve(FIXTURE1_ROOT, 'node_modules/dep1'), + relative: 'node_modules/dep1', + } + ]); + }); + + it('returns a single entry for every package/version combo', () => { + const tags = kibanaPackages.map(pkg => `${pkg.name}@${pkg.version}`); + expect(tags).to.eql(uniq(tags)); + }); + + it('does not include root package in the list', async () => { + expect(kibanaPackages.find(pkg => pkg.name === 'kibana')).to.be(undefined); + expect(fixture1Packages.find(pkg => pkg.name === 'fixture1')).to.be(undefined); + }); + }); +}); diff --git a/tasks/lib/packages/installed_packages.js b/tasks/lib/packages/installed_packages.js index 6a479317eb5d7..e34feb7478c4c 100644 --- a/tasks/lib/packages/installed_packages.js +++ b/tasks/lib/packages/installed_packages.js @@ -50,5 +50,6 @@ export async function getInstalledPackages(options = {}) { directory: realPath, relative: relative(directory, realPath) }; - }); + }) + .filter(pkg => pkg.directory !== directory); }