diff --git a/index.js b/index.js index d90f153..7de79f8 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,11 @@ var parse = require('spdx-expression-parse') var satisfies = require('semver').satisfies var spdxAllowed = require('spdx-whitelisted') -function licensee (configuration, path, callback) { +async function licensee (configurationOriginal, path, callback) { + // We do not want to modify original object when looping this method + // 'structuredClone' is not defined in test + var configuration = JSON.parse(JSON.stringify(configurationOriginal)) + if (!validConfiguration(configuration)) { return callback(new Error('Invalid configuration')) } @@ -34,7 +38,7 @@ function licensee (configuration, path, callback) { callback(new Error('No licenses or packages allowed.')) } else { var arborist = new Arborist({ path }) - arborist.loadActual({ forceActual: true }) + return arborist.loadActual({ forceActual: true }) .then(function (tree) { var dependencies = Array.from(tree.inventory.values()) .filter(function (dependency) { @@ -43,7 +47,7 @@ function licensee (configuration, path, callback) { if (configuration.filterPackages) { dependencies = configuration.filterPackages(dependencies) } - callback(null, findIssues(configuration, dependencies)) + return callback(null, findIssues(configuration, dependencies)) }) .catch(function (error) { return callback(error) @@ -108,9 +112,14 @@ function findIssues (configuration, dependencies) { } }) - results.sort(function (a, b) { - return a.name.localeCompare(b.name) - }) + // Non existing symlinks will not have metadata + const noName = results.filter(x => !x.name) + + if (!noName) { + results.sort(function (a, b) { + return a.name.localeCompare(b.name) + }) + } return results } @@ -174,6 +183,11 @@ function resultForPackage (configuration, tree) { typeof ignore.author === 'string' && personMatches(result.author, ignore.author) ) return true + if ( + ignore.path && + typeof ignore.path === 'string' && + result.path.includes(ignore.path) + ) return true return false }) if (ignored) { @@ -224,6 +238,9 @@ function resultForPackage (configuration, tree) { } function startsWith (string, prefix) { + if (!string) { + return false + } return string.toLowerCase().indexOf(prefix.toLowerCase()) === 0 } diff --git a/tests/nonexistent-symlinked-subdependency/.licensee.json b/tests/nonexistent-symlinked-subdependency/.licensee.json new file mode 100644 index 0000000..905b4e7 --- /dev/null +++ b/tests/nonexistent-symlinked-subdependency/.licensee.json @@ -0,0 +1,9 @@ +{ + "licenses": { + "spdx": [ + "Apache-2.0" + ] + }, + "packages": {}, + "ignore": [{ "path": "nonexistent/symlinked/node_modules/subdependency" }] +} diff --git a/tests/nonexistent-symlinked-subdependency/node_modules/@nonexistent/symlinked/node_modules/subdependency b/tests/nonexistent-symlinked-subdependency/node_modules/@nonexistent/symlinked/node_modules/subdependency new file mode 120000 index 0000000..4428094 --- /dev/null +++ b/tests/nonexistent-symlinked-subdependency/node_modules/@nonexistent/symlinked/node_modules/subdependency @@ -0,0 +1 @@ +../src/js-vendor/tablesorter \ No newline at end of file diff --git a/tests/nonexistent-symlinked-subdependency/node_modules/@nonexistent/symlinked/package.json b/tests/nonexistent-symlinked-subdependency/node_modules/@nonexistent/symlinked/package.json new file mode 100644 index 0000000..e0bfab4 --- /dev/null +++ b/tests/nonexistent-symlinked-subdependency/node_modules/@nonexistent/symlinked/package.json @@ -0,0 +1,7 @@ +{ + "name": "nonexistent/symlinked", + "license": "Apache-2.0", + "dependencies": { + "subdependency": "file:./not_exists" + } +} diff --git a/tests/nonexistent-symlinked-subdependency/package.json b/tests/nonexistent-symlinked-subdependency/package.json new file mode 100644 index 0000000..ed6dd65 --- /dev/null +++ b/tests/nonexistent-symlinked-subdependency/package.json @@ -0,0 +1,7 @@ +{ + "name": "nonexistent-symlinked-subdependency", + "dependencies": { + "@nonexistent/symlinked": "*" + }, + "private": true +} diff --git a/tests/nonexistent-symlinked-subdependency/test.js b/tests/nonexistent-symlinked-subdependency/test.js new file mode 100644 index 0000000..1401580 --- /dev/null +++ b/tests/nonexistent-symlinked-subdependency/test.js @@ -0,0 +1,15 @@ +var tap = require('tap') + +var results = require('../run')(['--ndjson'], __dirname) + +tap.equal(results.status, 0) + +var output = results.stdout.trim().split('\n').map(line => JSON.parse(line)) + +tap.assert( + output.some(result => result.name === 'nonexistent/symlinked' && result.approved === true) +) + +tap.assert( + output.some(result => result.version === '' && result.approved === true) +)