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

Add ignore dependencies option in config file #37

Merged
merged 9 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function main(cli, { policyDetails, setupProjectFn }) {
cli.showHelp(1);
} else {
const currentDate = processDate(cli.flags.currentDate) || undefined;
let ignoredDependencies = cli.flags.ignoredDependencies || [];

let policies = {
primary: DEFAULT_PRIMARY_POLICY,
Expand All @@ -28,6 +29,7 @@ async function main(cli, { policyDetails, setupProjectFn }) {
let configuredPolicies = JSON.parse(configFile);
if (configuredPolicies.primary) {
policies.primary = configuredPolicies.primary;
ignoredDependencies = configuredPolicies.primary.ignoredDependencies || ignoredDependencies;
SparshithNR marked this conversation as resolved.
Show resolved Hide resolved
}
if (configuredPolicies.custom) {
policies.custom = {};
Expand All @@ -38,6 +40,11 @@ async function main(cli, { policyDetails, setupProjectFn }) {
`The dependency ${dep} was found multiple times in the config file. Please refer Rules section in configuration.md`,
);
}
if (ignoredDependencies.includes(dep)) {
throw new Error(
`The dependency ${dep} was found in ignoredDependencies and custom configuration. Please refer Rules section in configuration.md`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine for now, but once #36 lands, we should create a shortend url for it and actually show a url here for people to lookup

);
}
policies.custom[dep] = {
upgradeBudget: policy.upgradeBudget,
effectiveReleaseDate:
Expand All @@ -52,7 +59,14 @@ async function main(cli, { policyDetails, setupProjectFn }) {
let result;
let processed = false;
try {
result = await processPolicies(projectPaths, setupProjectFn, spinner, currentDate, policies);
result = await processPolicies(
projectPaths,
setupProjectFn,
spinner,
currentDate,
policies,
ignoredDependencies,
);
if (result.isInSupportWindow === false) {
process.exitCode = 1;
}
Expand Down Expand Up @@ -121,6 +135,11 @@ async function run(
type: 'string',
alias: 'f',
},
ignoredDependencies: {
type: 'string',
alias: 'i',
isMultiple: true,
},
},
}),
{ policyDetails, setupProjectFn },
Expand Down
1 change: 1 addition & 0 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = (commandName = 'supported', packageLink = pkg.homepage) => {
{cyan --csv} outputs csv file in the project path
{cyan --current-date, -c} optional current date to use when calculating support
{cyan --config-file, -f} optional config file to override the default setup
{cyan --ignored-dependencies, -i} optional list of dependencies to be ignored while running the policy, ex: -r 'test','tester'
{bold Examples}
{gray $} {cyan ${commandName} ./path/to/project/}
`;
Expand Down
17 changes: 15 additions & 2 deletions lib/project/multiple-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const { ProgressLogger } = require('../util');
const DEFAULT_SETUP_FILE = './setup-project';

module.exports.processPolicies = processPolicies;
async function processPolicies(projectPaths, setupProjectFn, spinner, today, policies) {
async function processPolicies(
projectPaths,
setupProjectFn,
spinner,
today,
policies,
ignoredDependencies,
) {
const setupProject = setupProjectFn ? setupProjectFn : require(DEFAULT_SETUP_FILE);
let result = {
isInSupportWindow: true,
Expand All @@ -30,9 +37,15 @@ async function processPolicies(projectPaths, setupProjectFn, spinner, today, pol
work.push(
queue.add(async () => {
let { dependenciesToCheck, pkg } = await setupProject(projectPath);
let dependenciesToCheckAfterIgnore = dependenciesToCheck.filter(
dep => !ignoredDependencies.includes(dep.name),
);
progressLogger.updateTotalDepCount(dependenciesToCheck.length);
progressLogger.updateIgnoredDepCount(
dependenciesToCheck.length - dependenciesToCheckAfterIgnore.length,
);
let auditResult = await isInSupportWindow(
dependenciesToCheck,
dependenciesToCheckAfterIgnore,
pkg.name,
{
progressLogger,
Expand Down
11 changes: 9 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ function sortLibraries(a, b) {
}

module.exports.ProgressLogger = class ProgressLogger {
constructor(_spinner = { prefixText: '', text: '' }, _isMultipleProduct) {
constructor(_spinner = { prefixText: '', text: '' }, _isMultipleProduct = false) {
this.spinner = _spinner;
this.totalPackages = 0; // start from `1` as we have node policy tested all the time
this.processedCount = 0;
this.semVerLogged = false;
this.isMultipleProduct = _isMultipleProduct;
this.ignoredPackages = 0;
}

getLoggerPrefixText(name, isSupported, isExpiringSoon) {
Expand All @@ -123,6 +124,10 @@ module.exports.ProgressLogger = class ProgressLogger {
this.totalPackages += count;
}

updateIgnoredDepCount(count) {
this.ignoredPackages = count;
}

updateSpinner(name, isSupported, isExpiringSoon) {
if (name && !this.isMultipleProduct) {
if (!this.spinner.prefixText) {
Expand All @@ -141,7 +146,9 @@ module.exports.ProgressLogger = class ProgressLogger {
}
this.spinner.text = `Total Dependencies: ${this.totalPackages}, Verified: ${
this.processedCount
}, Remaining: ${this.totalPackages - this.processedCount}`;
}, Remaining: ${this.totalPackages - this.processedCount - this.ignoredPackages}, Ignored: ${
this.ignoredPackages
}`;
if (this.totalPackages > this.processedCount) {
this.processedCount++;
}
Expand Down
72 changes: 62 additions & 10 deletions tests/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,48 @@ describe('CLI', function () {
});
});

describe(`--ignore-dependencies`, function () {
it('check console log', async function () {
const child = await runSupportedCmd([
`${__dirname}/fixtures/supported-project`,
`--ignored-dependencies @stefanpenner/a`,
`-i rsvp`,
]);

expect(child).to.exitGracefully();
expect(child.stderr).to.includes(`Ignored: 2`);
SparshithNR marked this conversation as resolved.
Show resolved Hide resolved
expect(child.stderr).to.includes('✓ SemVer Policy');
expect(child.stdout).to.includes('Congrats!');
});

it('check if verbose do not incude the entry', async function () {
const child = await runSupportedCmd([
`${__dirname}/fixtures/supported-project`,
`--ignored-dependencies @stefanpenner/a`,
`-i rsvp`,
'--verbose',
]);

expect(child).to.exitGracefully();
expect(child.stderr).to.includes(`Ignored: 2`);
expect(child.stderr).to.includes('✓ SemVer Policy');
expect(child.stdout).to.includes('Congrats!');
expect(child.stdout).not.include(
'@stefanpenner/a 1.0.3 2.0.0 major',
SparshithNR marked this conversation as resolved.
Show resolved Hide resolved
);
});

it('make unsupported to supported project using ignoreDependency config', async function () {
const child = await runSupportedCmd([
`${__dirname}/fixtures/unsupported-project`,
`-f ${__dirname}/fixtures/unsupported-project/config-ignore-dep.json`,
]);

expect(child).to.exitGracefully();
expect(child.stderr).to.includes('✓ SemVer Policy');
});
});

describe('Filter options like --unsupported/expiring/supported', function () {
it('works against a unsupported project with --unsupported option', async function () {
const child = await runSupportedCmd([
Expand Down Expand Up @@ -364,6 +406,7 @@ describe('CLI', function () {
});
});
});

describe('--config-file', function () {
it('make unsupported to supported project using effectiveReleaseDate', async function () {
const child = await runSupportedCmd([
Expand All @@ -374,6 +417,7 @@ describe('CLI', function () {
expect(child).to.exitGracefully();
expect(child.stderr).to.includes('✓ SemVer Policy');
});

it('make unsupported to supported project using upgradeBudget', async function () {
const child = await runSupportedCmd([
`${__dirname}/fixtures/unsupported-project`,
Expand All @@ -385,16 +429,24 @@ describe('CLI', function () {
});

it('alert user when there is conflicting custom config', async function () {
try {
await runSupportedCmd([
`${__dirname}/fixtures/unsupported-project`,
`-f ${__dirname}/fixtures/unsupported-project/config-conflict.json`,
]);
} catch (e) {
expect(e).includes(
`The dependency es6-promise was found multiple times in the config file. Please refer Rules section in configuration.md`,
);
}
const child = await runSupportedCmd([
`${__dirname}/fixtures/unsupported-project`,
`-f ${__dirname}/fixtures/unsupported-project/config-conflict.json`,
]);
expect(child.stderr).includes(
`The dependency es6-promise was found multiple times in the config file. Please refer Rules section in configuration.md`,
);
});

it('alert user when there is conflict in custom config and ignoredDependency', async function () {
const child = await runSupportedCmd([
`${__dirname}/fixtures/unsupported-project`,
`-f ${__dirname}/fixtures/unsupported-project/config.json`,
`-i es6-promise`,
]);
expect(child.stderr).includes(
SparshithNR marked this conversation as resolved.
Show resolved Hide resolved
`The dependency es6-promise was found in ignoredDependencies and custom configuration. Please refer Rules section in configuration.md`,
);
});
});
});
5 changes: 5 additions & 0 deletions tests/fixtures/unsupported-project/config-ignore-dep.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"primary": {
"ignoredDependencies": ["es6-promise", "@stefanpenner/a", "rsvp"]
}
}