123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- 'use strict';
- var expect = require('expect.js');
- var promptly = require('../index');
- var async = require('async');
- // Mock stdout
- var stdout = '';
- var oldWrite = process.stdout.write;
- process.stdout.write = function (data) {
- stdout += data;
- return oldWrite.apply(process.stdout, arguments);
- };
- // Function to send a line to stdin
- function sendLine(line) {
- setImmediate(function () {
- process.stdin.emit('data', line + '\n');
- });
- }
- beforeEach(function () {
- stdout = '';
- });
- describe('prompt()', function () {
- it('should prompt the user', function (next) {
- promptly.prompt('something: ', function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('yeaa');
- expect(stdout).to.contain('something: ');
- next();
- });
- sendLine('yeaa');
- });
- it('should keep asking if no value is passed and no default was defined', function (next) {
- promptly.prompt('something: ', function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('yeaa');
- expect(stdout).to.contain('something: ');
- expect(stdout.indexOf('something')).to.not.be(stdout.lastIndexOf('something'));
- next();
- });
- sendLine('');
- sendLine('yeaa');
- });
- it('should assume default value if nothing is passed', function (next) {
- promptly.prompt('something: ', { 'default': 'myValue' }, function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('myValue');
- expect(stdout).to.contain('something: ');
- next();
- });
- sendLine('');
- });
- it('should trim the user input if trim is enabled', function (next) {
- promptly.prompt('something: ', { trim: true }, function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('yeaa');
- expect(stdout).to.contain('something: ');
- next();
- });
- sendLine(' yeaa ');
- });
- it('should call validator after trimming', function (next) {
- function validator(value) {
- if (value !== 'yeaa') {
- throw new Error('bla');
- }
- return value;
- }
- promptly.prompt('something: ', { validator: validator, retry: false }, function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('yeaa');
- expect(stdout).to.contain('something: ');
- next();
- });
- sendLine(' yeaa ');
- });
- it('should assume values from the validator', function (next) {
- function validator() { return 'bla'; }
- promptly.prompt('something: ', { validator: validator }, function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('bla');
- expect(stdout).to.contain('something: ');
- next();
- });
- sendLine(' yeaa ');
- });
- it('should automatically retry if a validator fails by default', function (next) {
- function validator(value) {
- if (value !== 'yeaa') {
- throw new Error('bla');
- }
- return value;
- }
- promptly.prompt('something: ', { validator: validator, retry: true }, function (err, value) {
- expect(stdout).to.contain('something: ');
- expect(stdout.indexOf('something')).to.not.be(stdout.lastIndexOf('something'));
- expect(stdout).to.contain('bla');
- expect(value).to.equal('yeaa');
- next();
- });
- sendLine('wtf');
- sendLine('yeaa');
- });
- it('should give error if the validator fails and retry is false', function (next) {
- function validator() { throw new Error('bla'); }
- promptly.prompt('something: ', { validator: validator, retry: false }, function (err) {
- expect(err).to.be.an(Error);
- expect(err.message).to.be('bla');
- expect(stdout).to.contain('something: ');
- next();
- });
- sendLine(' yeaa ');
- });
- it('should give retry ability on error', function (next) {
- var times = 0;
- function validator(value) {
- if (value !== 'yeaa') {
- throw new Error('bla');
- }
- return value;
- }
- promptly.prompt('something: ', { validator: validator, retry: false }, function (err, value) {
- times++;
- if (times === 1) {
- expect(err).to.be.an(Error);
- err.retry();
- return sendLine('yeaa');
- }
- expect(value).to.equal('yeaa');
- expect(stdout).to.contain('something: ');
- expect(stdout.indexOf('something')).to.not.be(stdout.lastIndexOf('something'));
- next();
- });
- sendLine('wtf');
- });
- it('should write input to stdout by default', function (next) {
- promptly.prompt('something: ', function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('yeaa');
- expect(stdout).to.contain('something: ');
- expect(stdout).to.contain(value);
- next();
- });
- sendLine('yeaa');
- });
- it('should write input to stdout if silent is false', function (next) {
- promptly.prompt('something: ', { silent: true }, function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('yeaa');
- expect(stdout).to.contain('something: ');
- expect(stdout).to.not.contain(value);
- next();
- });
- sendLine('yeaa');
- });
- it('should prompt the user (using promise)', function (next) {
- promptly.prompt('something: ')
- .then(function (value) {
- expect(value).to.be('yeaa');
- expect(stdout).to.contain('something: ');
- next();
- })
- .catch(function () {
- expect().fail();
- next();
- });
- sendLine('yeaa');
- });
- });
- describe('choose()', function () {
- it('should keep asking on invalid choice', function (next) {
- promptly.choose('apple or orange? ', ['apple', 'orange'], function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('orange');
- expect(stdout).to.contain('apple or orange? ');
- expect(stdout.indexOf('apple or orange')).to.not.be(stdout.lastIndexOf('apple or orange'));
- expect(stdout).to.contain('Invalid choice');
- next();
- });
- sendLine('bleh');
- sendLine('orange');
- });
- it('should error on invalid choice if retry is disabled', function (next) {
- promptly.choose('apple or orange? ', ['apple', 'orange'], { retry: false }, function (err) {
- expect(err).to.be.an(Error);
- expect(err.message).to.contain('choice');
- expect(stdout).to.contain('apple or orange? ');
- next();
- });
- sendLine('bleh');
- });
- it('should be ok on valid choice', function (next) {
- promptly.choose('apple or orange? ', ['apple', 'orange'], function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be('apple');
- expect(stdout).to.contain('apple or orange? ');
- next();
- });
- sendLine('apple');
- });
- it('should not use strict comparison when matching against valid choices', function (next) {
- promptly.choose('choices: ', [1, 2, 3], function (err, value) {
- expect(err).to.be(null);
- expect(typeof value).to.equal('number');
- expect(value).to.be(1);
- expect(stdout).to.contain('choices: ');
- next();
- });
- sendLine('1');
- });
- it('should work using promise', function (next) {
- promptly.choose('apple or orange? ', ['apple', 'orange'])
- .then(function (value) {
- expect(value).to.be('orange');
- expect(stdout).to.contain('apple or orange? ');
- next();
- })
- .catch(function () {
- expect().fail();
- next();
- });
- sendLine('orange');
- });
- });
- describe('confirm()', function () {
- it('should be ok on valid choice and coerce to boolean values', function (next) {
- async.forEachSeries(['yes', 'Y', 'y', '1'], function (truthy, next) {
- promptly.confirm('test yes? ', { retry: false }, function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be(true);
- expect(stdout).to.contain('test yes? ');
- next();
- });
- sendLine(truthy);
- }, function () {
- async.forEachSeries(['no', 'N', 'n', '0'], function (truthy, next) {
- promptly.confirm('test no? ', function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be(false);
- expect(stdout).to.contain('test no? ');
- next();
- });
- sendLine(truthy);
- }, next);
- });
- });
- it('should keep asking on invalid choice', function (next) {
- promptly.confirm('yes or no? ', function (err, value) {
- expect(err).to.be(null);
- expect(value).to.be(true);
- expect(stdout).to.contain('yes or no? ');
- expect(stdout.indexOf('yes or no')).to.not.be(stdout.lastIndexOf('yes or no'));
- next();
- });
- sendLine('bleh');
- sendLine('y');
- });
- it('should error on invalid choice if retry is disabled', function (next) {
- promptly.confirm('yes or no? ', { retry: false }, function (err) {
- expect(err).to.be.an(Error);
- expect(err.message).to.not.contain('Invalid choice');
- expect(stdout).to.contain('yes or no? ');
- next();
- });
- sendLine('bleh');
- });
- it('should work using promise', function (next) {
- promptly.confirm('yes or no? ')
- .then(function (value) {
- expect(stdout).to.contain('yes or no? ');
- expect(value).to.be(true);
- next();
- })
- .catch(function () {
- expect().fail();
- next();
- });
- sendLine('y');
- });
- });
- describe('password()', function () {
- it('should prompt the user silently', function (next) {
- promptly.password('something: ', function (err, value) {
- expect(value).to.be('yeaa');
- expect(stdout).to.contain('something: ');
- expect(stdout).to.not.contain('yeaa');
- next();
- });
- sendLine('yeaa');
- });
- it('should not trim by default', function (next) {
- promptly.password('something: ', function (err, value) {
- expect(value).to.be(' yeaa ');
- expect(stdout).to.contain('something: ');
- expect(stdout).to.not.contain(' yeaa ');
- next();
- });
- sendLine(' yeaa ');
- });
- it('show allow empty passwords by default', function (next) {
- promptly.password('something: ', function (err, value) {
- expect(value).to.be('');
- expect(stdout).to.contain('something: ');
- next();
- });
- sendLine('');
- });
- it('should prompt the user silently using promise', function (next) {
- promptly.password('something: ')
- .then(function (value) {
- expect(value).to.be('yeaa');
- expect(stdout).to.contain('something: ');
- expect(stdout).to.not.contain('yeaa');
- next();
- })
- .catch(function () {
- expect().fail();
- next();
- });
- sendLine('yeaa');
- });
- });
|