Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: allow to combine excludePackages and excludePackagesStartingWith options #50

Merged
merged 1 commit into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,24 +448,14 @@ exports.init = function init(args, callback) {
return resultJson;
}

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

Object.keys(filtered).map((filteredPackage) => {
let isBlacklisted = false;

blacklist.forEach((blacklisted) => {
if (filteredPackage.startsWith(blacklisted)) {
isBlacklisted = true;
}
});
function excludePackagesStartingWith(blacklist, currentResult) {
const resultJson = { ...currentResult };

if (isBlacklisted) {
delete filtered[filteredPackage];
} else {
resultJson[filteredPackage] = 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 @@ -656,23 +646,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 excludeStartStringsArr = getOptionArray(args.excludePackagesStartingWith);
if (excludeStartStringsArr) {
resultJson = excludePackagesStartingWith(excludeStartStringsArr, filtered);
resultJson = excludePackagesStartingWith(excludeStartStringsArr, 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