Skip to content

Commit

Permalink
test(coverage): fix coverage issues
Browse files Browse the repository at this point in the history
These issues are pointed out by the latest version of nyc. Fixing these
issues brings us to 100% coverage (as of [email protected]).

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
  • Loading branch information
nfischer committed Jun 20, 2018
1 parent c0d7b8b commit 795f143
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
1 change: 1 addition & 0 deletions src/shx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
17 changes: 16 additions & 1 deletion test/specs/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 795f143

Please sign in to comment.