Skip to content

Commit

Permalink
Guard list against invalid string defaults (#607)
Browse files Browse the repository at this point in the history
* Guard list against invalid string defaults

* Uses Math.max based on review feedback
  • Loading branch information
Froren authored and SBoudrias committed Nov 2, 2017
1 parent 651a569 commit ce4e858
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/prompts/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function Prompt() {
if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) {
this.selected = def;
} else if (!_.isNumber(def) && def != null) {
this.selected = this.opt.choices.pluck('value').indexOf(def);
let index = this.opt.choices.pluck('value').indexOf(def);
this.selected = Math.max(index, 0);
}

// Make sure no default is set (so it won't be printed)
Expand Down
12 changes: 12 additions & 0 deletions test/specs/prompts/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ describe('`list` prompt', function () {
this.rl.emit('line');
});

it('shouldn\'t allow an invalid string default to change position', function (done) {
this.fixture.default = 'babar';
var list = new List(this.fixture, this.rl);

list.run().then(function (answer) {
expect(answer).to.equal('foo');
done();
});

this.rl.emit('line');
});

it('shouldn\'t allow an invalid index as default', function (done) {
this.fixture.default = 4;
var list = new List(this.fixture, this.rl);
Expand Down

0 comments on commit ce4e858

Please sign in to comment.