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

feat: migrate cli scripts and their test cases #14

Merged
merged 5 commits into from
Nov 8, 2021
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
18 changes: 14 additions & 4 deletions src/bin/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ const getSortedRules = require('../lib/sort-rules');
const flattenRulesDiff = require('../lib/flatten-rules-diff');
const stringifyRuleConfig = require('../lib/stringify-rule-config');

(async function () {

const files = [argv._[0], argv._[1]];
const collectedRules = getFilesToCompare(files).map(compareConfigs);
const collectedRules = await Promise.all(getFilesToCompare(files).map(compareConfigs));

const rulesCount = collectedRules.reduce(
(prev, curr) => {
Expand Down Expand Up @@ -66,13 +68,14 @@ function getFilesToCompare(allFiles) {
return filesToCompare;
}

function compareConfigs(currentFiles) {
async function compareConfigs(currentFiles) {
const ruleFinders = await Promise.all(currentFiles.slice(0, 2).map(getRuleFinder));
return {
config1: path.basename(currentFiles[0]),
config2: path.basename(currentFiles[1]),
rules: rulesDifference(
getRuleFinder(currentFiles[0]),
getRuleFinder(currentFiles[1])
ruleFinders[0],
ruleFinders[1]
)
};
}
Expand All @@ -94,3 +97,10 @@ function rulesDifference(a, b) {
)
);
}

process.exit(0);

})().catch(/* istanbul ignore next */(e) => {
console.error(e);
process.exit(1);
});
9 changes: 8 additions & 1 deletion src/bin/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ const getRuleURI = require('eslint-rule-documentation');
const getRuleFinder = require('../lib/rule-finder');
const cli = require('../lib/cli-util');

(async function () {

const specifiedFile = argv._[0];
const finderOptions = {
omitCore: !argv.core,
includeDeprecated: argv.include === 'deprecated',
ext: argv.ext
};
const ruleFinder = getRuleFinder(specifiedFile, finderOptions);
const ruleFinder = await getRuleFinder(specifiedFile, finderOptions);
const errorOut = argv.error && !argv.n;
let processExitCode = 0;

Expand Down Expand Up @@ -76,3 +78,8 @@ if (!argv.c && !argv.p && !argv.a && !argv.u && !argv.d) {
cli.write();
}
process.exit(processExitCode);

})().catch(/* istanbul ignore next */(e) => {
console.error(e);
process.exit(1);
});
15 changes: 12 additions & 3 deletions test/bin/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const proxyquire = require('proxyquire');
const sinon = require('sinon');

const consoleLog = console.log; // eslint-disable-line no-console
const processExit = process.exit;

const stub = {
'../lib/rule-finder'() {
async '../lib/rule-finder'() {
return {
getCurrentRules() {}, // Noop
getCurrentRulesDetailed() {} // Noop
Expand All @@ -15,6 +16,8 @@ const stub = {
'../lib/object-diff': sinon.stub().returns([{'test-rule': {config1: 'foo-config', config2: 'bar-config'}}])
};

let exitStatus;

describe('diff', () => {
beforeEach(() => {
process.argv = process.argv.slice(0, 2);
Expand All @@ -24,18 +27,23 @@ describe('diff', () => {
consoleLog(...args);
}
});
exitStatus = new Promise(resolve => {
process.exit = resolve;
});
});

afterEach(() => {
console.log.restore(); // eslint-disable-line no-console
process.exit = processExit;
// purge yargs cache
delete require.cache[require.resolve('yargs')];
});

it('logs diff', () => {
it('logs diff', async () => {
process.argv[2] = './foo';
process.argv[3] = './bar';
proxyquire('../../src/bin/diff', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(
console.log.calledWith( // eslint-disable-line no-console
sinon.match(
Expand All @@ -45,11 +53,12 @@ describe('diff', () => {
);
});

it('logs diff verbosely', () => {
it('logs diff verbosely', async () => {
process.argv[2] = '--verbose';
process.argv[3] = './foo';
process.argv[4] = './bar';
proxyquire('../../src/bin/diff', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(
console.log.calledWith( // eslint-disable-line no-console
sinon.match(
Expand Down
93 changes: 50 additions & 43 deletions test/bin/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let exitStatus;
describe('bin', () => {
beforeEach(() => {
stub = {
'../lib/rule-finder'() {
async '../lib/rule-finder'() {
return {
getCurrentRules,
getPluginRules,
Expand All @@ -34,10 +34,9 @@ describe('bin', () => {
}
consoleLog(...args);
};
exitStatus = null;
process.exit = status => {
exitStatus = status;
};
exitStatus = new Promise(resolve => {
process.exit = resolve;
});
process.argv = process.argv.slice(0, 2);
});

Expand All @@ -48,7 +47,7 @@ describe('bin', () => {
delete require.cache[require.resolve('yargs')];
});

it('no option', () => {
it('no option', async () => {
let callCount = 0;
console.log = (...args) => { // eslint-disable-line no-console
callCount += 1;
Expand All @@ -60,122 +59,125 @@ describe('bin', () => {
consoleLog(...args);
};
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.equal(callCount, 3); // eslint-disable-line no-console
});

it('option -c|--current', () => {
it('option -c|--current', async () => {
process.argv[2] = '-c';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getCurrentRules.called);
assert.equal(exitStatus, 0);
});

it('option -p|--plugin', () => {
it('option -p|--plugin', async () => {
process.argv[2] = '-p';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getPluginRules.called);
assert.equal(exitStatus, 0);
});

it('option -a|--all-available', () => {
it('option -a|--all-available', async () => {
process.argv[2] = '-a';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getAllAvailableRules.called);
process.argv[2] = '--all-available';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getAllAvailableRules.called);
assert.equal(exitStatus, 0);
});

it('option -a along with --ext', () => {
it('option -a along with --ext', async () => {
process.argv[2] = '-a';
process.argv[3] = '--ext .json';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getAllAvailableRules.called);
assert.equal(exitStatus, 0);
});

it('option -a along with multi --ext', () => {
it('option -a along with multi --ext', async () => {
process.argv[2] = '-a';
process.argv[3] = '--ext .js';
process.argv[4] = '--ext .json';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getAllAvailableRules.called);
assert.equal(exitStatus, 0);
});

it('option -u|--unused', () => {
it('option -u|--unused', async () => {
process.argv[2] = '-u';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 1);
assert.ok(getUnusedRules.called);
assert.equal(exitStatus, 1);
});

it('options -u|--unused and no unused rules found', () => {
it('options -u|--unused and no unused rules found', async () => {
getUnusedRules.returns([]);
process.argv[2] = '-u';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getUnusedRules.called);
assert.equal(exitStatus, 0);
});

it('option -u|--unused along with -n', () => {
it('option -u|--unused along with -n', async () => {
process.argv[2] = '-u';
process.argv[3] = '-n';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getUnusedRules.called);
assert.equal(exitStatus, 0);
});

it('option -u|--unused along with --no-error', () => {
it('option -u|--unused along with --no-error', async () => {
process.argv[2] = '-u';
process.argv[3] = '--no-error';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getUnusedRules.called);
assert.equal(exitStatus, 0);
});

it('option -d|--deprecated', () => {
it('option -d|--deprecated', async () => {
process.argv[2] = '-d';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 1);
assert.ok(getDeprecatedRules.called);
assert.equal(exitStatus, 1);
});

it('options -d|--deprecated and no deprecated rules found', () => {
it('options -d|--deprecated and no deprecated rules found', async () => {
getDeprecatedRules.returns([]);
process.argv[2] = '-d';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getDeprecatedRules.called);
assert.equal(exitStatus, 0);
});

it('option -d|--deprecated along with -n', () => {
it('option -d|--deprecated along with -n', async () => {
process.argv[2] = '-d';
process.argv[3] = '-n';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getDeprecatedRules.called);
assert.equal(exitStatus, 0);
});

it('option -d|--deprecated along with --no-error', () => {
it('option -d|--deprecated along with --no-error', async () => {
process.argv[2] = '-d';
process.argv[3] = '--no-error';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getDeprecatedRules.called);
assert.equal(exitStatus, 0);
});

it('logs verbosely', () => {
it('logs verbosely', async () => {
process.argv[2] = '-c';
process.argv[3] = '-v';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(getCurrentRules.called);
});

it('logs core rules', () => {
it('logs core rules', async () => {
stub = {
'../lib/rule-finder'(specifiedFile, options) {
async '../lib/rule-finder'(specifiedFile, options) {
return {
getCurrentRules() {
assert(!options.omitCore);
Expand All @@ -186,11 +188,12 @@ describe('bin', () => {
};
process.argv[2] = '-c';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
});

it('does not log core rules with --no-core', () => {
it('does not log core rules with --no-core', async () => {
stub = {
'../lib/rule-finder'(specifiedFile, options) {
async '../lib/rule-finder'(specifiedFile, options) {
return {
getCurrentRules() {
assert(options.omitCore);
Expand All @@ -202,11 +205,12 @@ describe('bin', () => {
process.argv[2] = '-c';
process.argv[3] = '--no-core';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
});

it('does not include deprecated rules by default', () => {
it('does not include deprecated rules by default', async () => {
stub = {
'../lib/rule-finder'(specifiedFile, options) {
async '../lib/rule-finder'(specifiedFile, options) {
return {
getAllAvailableRules() {
assert(!options.includeDeprecated);
Expand All @@ -217,11 +221,12 @@ describe('bin', () => {
};
process.argv[2] = '-a';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
});

it('includes deprecated rules with --include deprecated', () => {
it('includes deprecated rules with --include deprecated', async () => {
stub = {
'../lib/rule-finder'(specifiedFile, options) {
async '../lib/rule-finder'(specifiedFile, options) {
return {
getAllAvailableRules() {
assert(options.includeDeprecated);
Expand All @@ -233,11 +238,12 @@ describe('bin', () => {
process.argv[2] = '-a';
process.argv[3] = '--include=deprecated';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
});

it('includes deprecated rules with -i deprecated', () => {
it('includes deprecated rules with -i deprecated', async () => {
stub = {
'../lib/rule-finder'(specifiedFile, options) {
async '../lib/rule-finder'(specifiedFile, options) {
return {
getAllAvailableRules() {
assert(options.includeDeprecated);
Expand All @@ -250,5 +256,6 @@ describe('bin', () => {
process.argv[3] = '-i';
process.argv[4] = 'deprecated';
proxyquire('../../src/bin/find', stub);
assert.strictEqual(await exitStatus, 0);
});
});