From 795f143b5d775cc8425e1529254797e65d19f60c Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Tue, 19 Jun 2018 23:49:49 -0700 Subject: [PATCH] test(coverage): fix coverage issues These issues are pointed out by the latest version of nyc. Fixing these issues brings us to 100% coverage (as of nyc@v12.0.2). Fixes include: * ignore coverage for a condition we don't hit during testing * branch coverage for when we read stdin for commands with non-boolean options (e.g., head, tail) * branch coverage for plugin-behavior with config files lacking a plugin attribute Issue #134 --- src/config.js | 2 +- src/shx.js | 1 + test/specs/cli.js | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/config.js b/src/config.js index b98b827..41fe85c 100644 --- a/src/config.js +++ b/src/config.js @@ -42,7 +42,7 @@ export const shouldReadStdin = (args) => { }); let requiredNumArgs = cmdInfo ? cmdInfo.minArgs : -1; - // If a non-boolean option is passed in, incrememnt the required argument + // If a non-boolean option is passed in, increment the required argument // count (this is the case for `-n` for `head` and `tail`) if (parsedArgs.n && (cmd === 'head' || cmd === 'tail')) { requiredNumArgs++; diff --git a/src/shx.js b/src/shx.js index 7e08f93..dcb178e 100644 --- a/src/shx.js +++ b/src/shx.js @@ -102,6 +102,7 @@ export function shx(argv) { if (typeof code === 'number') { return code; } else if (shell.error()) { + /* istanbul ignore next */ return EXIT_CODES.CMD_FAILED; } diff --git a/test/specs/cli.js b/test/specs/cli.js index db8d5b6..526d1cb 100644 --- a/test/specs/cli.js +++ b/test/specs/cli.js @@ -153,9 +153,10 @@ describe('cli', () => { shouldReadStdin(['grep', 'a.*z']).should.equal(true); }); - it('reads stdin if only options are given', () => { + it('reads stdin if not enough arguments are given', () => { process.stdin.isTTY = undefined; shouldReadStdin(['head', '-n', '1']).should.equal(true); + shouldReadStdin(['tail', '-n', '1']).should.equal(true); shouldReadStdin(['grep', '-i', 'a.*z']).should.equal(true); }); @@ -216,6 +217,20 @@ describe('cli', () => { }).should.throw(Error); }); + it('does not load any plugins if config file lacks plugin section', () => { + const config = { + notplugins: [ // the plugins attribute is mandatory for loading plugins + 'shelljs-plugin-open', + ], + }; + shell.ShellString(JSON.stringify(config)).to(CONFIG_FILE); + + const output = cli('help'); + output.stderr.should.equal(''); // Runs successfully + output.stdout.should.match(/Usage/); // make sure help is printed + output.stdout.should.not.match(/- open/); // should *not* load the plugin + }); + it('defends against malicious config files', () => { const notValidJSON = ` var shell = require('shelljs');