Skip to content

Commit

Permalink
fix(autocomplete): Handle error when no argument is given to autocomp…
Browse files Browse the repository at this point in the history
…lete helper (#4)

* Handle error when no argument is given to autocomplete helper
* check if allowed shell are input
  • Loading branch information
kinok authored and mattallty committed Mar 4, 2017
1 parent 3066c2c commit fee73ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const path = require('path');
const Autocomplete = require('./autocomplete');
const createLogger = require('./logger').createLogger;
const tabtabComplete = require('tabtab/src/complete');
const WrongNumberOfArgumentError = require('./error/wrong-num-of-arg');

class Program extends GetterSetter {

Expand All @@ -22,6 +23,7 @@ class Program extends GetterSetter {
this.bin = this.makeGetterSetter('bin');
this._bin = path.basename(process.argv[1]);
this._autocomplete = new Autocomplete(this);
this._supportedShell = ['bash', 'zsh', 'fish'];
this.logger(createLogger());
this._defaultCommand = null;
}
Expand Down Expand Up @@ -210,7 +212,12 @@ class Program extends GetterSetter {
return this._autocomplete.listen({"_" : args._});
}
const complete = new tabtabComplete({name: this.bin()});
this.logger().info(complete.script(this.bin(), this.bin(), argv[1].toLowerCase()));

if (typeof argv[1] !== "string" || this._supportedShell.indexOf(argv[1]) === -1) {
this.fatalError(new WrongNumberOfArgumentError(`Shell must be given (${this._supportedShell.join('/')})`, {}, this));
} else {
this.logger().info(complete.script(this.bin(), this.bin(), argv[1].toLowerCase()));
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/arg-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,21 @@ describe("Argument validation", function() {
should(this.action.callCount).eql(1);
});

it(`should throw WrongNumberOfArgumentError when no argument is given to completion`, function() {
program.parse(makeArgv(['completion']));
should(this.fatalError.callCount).eql(1);
should(this.fatalError.calledWith(sinon.match.instanceOf(WrongNumberOfArgumentError))).be.ok();
});

it(`should throw WrongNumberOfArgumentError when wrong argument is given to completion`, function() {
program.parse(makeArgv(['completion', 'nosh']));
should(this.fatalError.callCount).eql(1);
should(this.fatalError.calledWith(sinon.match.instanceOf(WrongNumberOfArgumentError))).be.ok();
});

it(`should not throw any error when passing an handled argument to completion`, function() {
program.parse(makeArgv(['completion', 'zsh']));
should(this.fatalError.callCount).eql(0);
});

});

0 comments on commit fee73ab

Please sign in to comment.