Skip to content

Commit

Permalink
fix: make sure excludePackagesStartingWith and excludePackages can be…
Browse files Browse the repository at this point in the history
… combined
  • Loading branch information
eugene1g committed Jan 15, 2023
1 parent ff44a92 commit 9dc9dd8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
26 changes: 11 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,18 +448,14 @@ exports.init = function init(args, callback) {
return resultJson;
}

function excludePackagesStartingWith(blacklist, filtered) {
const resultJson = {};
function excludePackagesStartingWith(blacklist, currentResult) {
const resultJson = { ...currentResult };

Object.keys(filtered).map((filteredPackage) => {
blacklist.forEach((blacklist) => {
if (!filteredPackage.startsWith(blacklist)) {
resultJson[filteredPackage] = filtered[filteredPackage];
} else {
delete filtered[filteredPackage];
}
});
});
for(const pkgName in resultJson){
for(const denyPrefix of blacklist){
if (pkgName.startsWith(denyPrefix)) delete resultJson[pkgName];
}
}

return resultJson;
}
Expand Down Expand Up @@ -648,23 +644,23 @@ exports.init = function init(args, callback) {
// package whitelist
const whitelist = getOptionArray(args.includePackages);
if (whitelist) {
resultJson = onlyIncludeWhitelist(whitelist, filtered);
resultJson = onlyIncludeWhitelist(whitelist, resultJson);
}

// package blacklist
const blacklist = getOptionArray(args.excludePackages);
if (blacklist) {
resultJson = excludeBlacklist(blacklist, filtered);
resultJson = excludeBlacklist(blacklist, resultJson);
}

// exclude by package name starting with a string
const excludeStartString = getOptionArray(args.excludePackagesStartingWith);
if (excludeStartString) {
resultJson = excludePackagesStartingWith(excludeStartString, filtered);
resultJson = excludePackagesStartingWith(excludeStartString, resultJson);
}

if (args.excludePrivatePackages) {
Object.keys(filtered).forEach(filterDeletePrivatePackages);
Object.keys(resultJson).forEach(filterDeletePrivatePackages);
}

Object.keys(resultJson).forEach(exitIfCheckHits);
Expand Down
42 changes: 42 additions & 0 deletions tests/packages-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,48 @@ describe('bin/license-checker-rseidelsohn', function () {
assert.ok(!illegalPackageFound);
});


it('should combine various types of inclusion and exclusions', function () {
const excludedPrefix = ['@types', 'spdx'];
const excludedNames = ['rimraf'];
const output = spawn(
'node',
[
path.join(__dirname, '../bin/license-checker-rseidelsohn'),
'--json',
'--excludePackages',
excludedNames.join(';'),
'--excludePackagesStartingWith',
excludedPrefix.join(';'),
],
{
cwd: path.join(__dirname, '../'),
},
);
const packages = Object.keys(JSON.parse(output.stdout.toString()));

let illegalPackageFound = false;

packages.forEach(function (p) {
excludedNames.forEach(function (pkgName) {
if(pkgName.indexOf('@')>1){
// check for the exact version
if(p === pkgName) illegalPackageFound = true;
} else if (p.startsWith(`${pkgName}@`)) {
illegalPackageFound = true;
}
});
excludedPrefix.forEach(function (prefix) {
if (p.startsWith(prefix)) {
illegalPackageFound = true;
}
});
});

// If an illegal package was found, the test fails
assert.ok(!illegalPackageFound);
});

it('should exclude private packages from the output', function () {
var output = spawn(
'node',
Expand Down

0 comments on commit 9dc9dd8

Please sign in to comment.