From 1848b7134660d80c3366de302d3051085e47ee9c Mon Sep 17 00:00:00 2001 From: sch0rsch Date: Sat, 26 Jun 2021 13:00:00 +0200 Subject: [PATCH 1/4] add tests adding tests to verify that license-ls runs correctly and that npmList(...) calls handle complex options --- tests/cli.test.js | 23 ++++++++++++++++++----- tests/npm-ls.test.js | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/tests/cli.test.js b/tests/cli.test.js index 1749f78..3e50a12 100644 --- a/tests/cli.test.js +++ b/tests/cli.test.js @@ -1,7 +1,20 @@ -const cli = require('../cli') const test = require('ava') +const spawn = require("cross-spawn"); -test('Does the thing', async (t) => { - // TODO: how do I test this since it's not a function? - t.true(true) -}) \ No newline at end of file +test('runs successfully', async (t) => { + t.timeout(10000) + const args = [ + 'cli.js', + '--format=json', + '--depth=0', + '--prod', + ] + + const sync = spawn.sync('node', args, {encoding: 'utf-8'}) + + t.regex(sync.stderr, /^- Analyzing\n(√|✔) Analyzing\n- Building output format\n(√|✔) Building output format\n$/) + t.is(sync.status, 0) + + const output = JSON.parse(sync.stdout) + t.is(output.length, 12, 'number of packages in the output should match the direct dependencies of license-ls') +}) diff --git a/tests/npm-ls.test.js b/tests/npm-ls.test.js index 7b0c1ac..91c3f35 100644 --- a/tests/npm-ls.test.js +++ b/tests/npm-ls.test.js @@ -1,5 +1,6 @@ const npmList = require('../helpers/npm-list') const test = require('ava') +const fs = require('fs') test('Returns a non-empty array of package paths', async (t) => { const actual = await npmList() @@ -32,3 +33,21 @@ test('Produces different results when given different options', async (t) => { t.notDeepEqual(prodOnly, devOnly) }) +test('Handles more complex options correctly', async (t) => { + const opts = { + csv: { delimiter: ',' }, + depth: 0, + format: 'json', + prod: true, + production: true, + include: [ 'id', 'name', 'version', 'license', 'repository', 'author', 'homepage', 'dependencyLevel' ], + table: {}, + xml: { asAttrs: false, 'as-attrs': false } + } + + const actual = await npmList(opts); + + t.true(actual.length > 0) + actual.forEach(path => t.true(fs.existsSync('' + path), `invalid path: ${path}`)) +}) + From 353757497460be7daa3763d43d9b43e1cb989588 Mon Sep 17 00:00:00 2001 From: sch0rsch Date: Sat, 26 Jun 2021 14:32:11 +0200 Subject: [PATCH 2/4] filter additional arguments causing problems with npm@7 --- helpers/npm-list.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/npm-list.js b/helpers/npm-list.js index ad36811..a67ebf4 100644 --- a/helpers/npm-list.js +++ b/helpers/npm-list.js @@ -11,7 +11,7 @@ const optionsToArgv = require('./options-to-args') * @returns {Promise>} */ module.exports = function (opts = {}) { - const blackListOpts = ['format'] + const blackListOpts = ['format', 'csv'] const options = optionsToArgv(opts, blackListOpts) return new Promise((resolve, reject) => { From 12efbf0aae6459b3d0c427e28788375d9339a8fb Mon Sep 17 00:00:00 2001 From: sch0rsch Date: Sat, 26 Jun 2021 14:33:56 +0200 Subject: [PATCH 3/4] refactoring: remove redundant await --- lib.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib.js b/lib.js index bd91c79..6d676ec 100644 --- a/lib.js +++ b/lib.js @@ -12,7 +12,7 @@ const glob = promisify(require('glob')) */ module.exports = async function (options = {}) { const pathList = await npmLs(options) - return await Promise.all(pathList.map(async (path, index) => { + return Promise.all(pathList.map(async (path, index) => { const pkg = await getPackageDetails(path) const licShortName = extractLicenseText(pkg.license || pkg.licenses || pkg.licence || pkg.licences) const licLongName = getExpandedLicName(licShortName) || 'unknown' From a7e5b0377b0b9ddfea616673530ba6fadb9475c9 Mon Sep 17 00:00:00 2001 From: sch0rsch Date: Sat, 26 Jun 2021 15:14:34 +0200 Subject: [PATCH 4/4] improve error handling and stop spinning Catch errors, log them and stop the spinner so the processr terminates when failures happen --- cli.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 8138ce9..5c07ffe 100755 --- a/cli.js +++ b/cli.js @@ -58,4 +58,8 @@ lib(options) } spinner.succeed() console.log(output) - }) \ No newline at end of file + }) + .catch(reason => { + spinner.fail('Unexpected error') + console.error(reason); + })