Comparing version
{ | ||
"name": "promptly", | ||
"version": "0.2.1", | ||
"version": "1.0.0", | ||
"description": "Simple command line prompting utility", | ||
"main": "index.js", | ||
"dependencies": { | ||
"read": "~1.0.4" | ||
"read": "^1.0.4" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.8.1", | ||
"async": "~0.1.22", | ||
"expect.js": "~0.2.0" | ||
"async": "^1.5.1", | ||
"expect.js": "^0.3.1", | ||
"mocha": "^2.1.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
"test": "mocha -R spec --bail" | ||
}, | ||
@@ -17,0 +17,0 @@ "repository": { |
@@ -1,3 +0,15 @@ | ||
# promptly [](http://travis-ci.org/IndigoUnited/node-promptly.png) | ||
# promptly | ||
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] | ||
[npm-url]:https://npmjs.org/package/promptly | ||
[downloads-image]:http://img.shields.io/npm/dm/promptly.svg | ||
[npm-image]:http://img.shields.io/npm/v/promptly.svg | ||
[travis-url]:https://travis-ci.org/IndigoUnited/node-promptly | ||
[travis-image]:http://img.shields.io/travis/IndigoUnited/node-promptly.svg | ||
[david-dm-url]:https://david-dm.org/IndigoUnited/node-promptly | ||
[david-dm-image]:https://img.shields.io/david/IndigoUnited/node-promptly.svg | ||
[david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-promptly#info=devDependencies | ||
[david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-promptly.svg | ||
Simple command line prompting utility. | ||
@@ -4,0 +16,0 @@ |
143
test/test.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
// Mock stdout | ||
var stdout = ''; | ||
@@ -15,6 +16,15 @@ var oldWrite = process.stdout.write; | ||
// 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) { | ||
stdout = ''; | ||
promptly.prompt('something: ', function (err, value) { | ||
@@ -27,8 +37,6 @@ expect(err).to.be(null); | ||
process.stdin.emit('data', 'yeaa\n'); | ||
sendLine('yeaa'); | ||
}); | ||
it('should keep asking if no value is passed and no default was defined', function (next) { | ||
stdout = ''; | ||
promptly.prompt('something: ', function (err, value) { | ||
@@ -42,9 +50,8 @@ expect(err).to.be(null); | ||
process.stdin.emit('data', '\n'); | ||
process.stdin.emit('data', 'yeaa\n'); | ||
sendLine(''); | ||
sendLine('yeaa'); | ||
}); | ||
it('should assume default value if nothing is passed', function (next) { | ||
stdout = ''; | ||
promptly.prompt('something: ', { 'default': '' }, function (err, value) { | ||
@@ -57,8 +64,6 @@ expect(err).to.be(null); | ||
process.stdin.emit('data', '\n'); | ||
sendLine(''); | ||
}); | ||
it('should trim the user input if trim is enabled', function (next) { | ||
stdout = ''; | ||
promptly.prompt('something: ', { trim: true }, function (err, value) { | ||
@@ -71,9 +76,7 @@ expect(err).to.be(null); | ||
process.stdin.emit('data', ' yeaa \n'); | ||
sendLine(' yeaa '); | ||
}); | ||
it('should call validator after trimming', function (next) { | ||
stdout = ''; | ||
var validator = function (value) { | ||
function validator(value) { | ||
if (value !== 'yeaa') { | ||
@@ -84,3 +87,3 @@ throw new Error('bla'); | ||
return value; | ||
}; | ||
} | ||
@@ -94,10 +97,8 @@ promptly.prompt('something: ', { validator: validator, retry: false }, function (err, value) { | ||
process.stdin.emit('data', ' yeaa \n'); | ||
sendLine(' yeaa '); | ||
}); | ||
it('should assume values from the validator', function (next) { | ||
stdout = ''; | ||
function validator() { return 'bla'; } | ||
var validator = function () { return 'bla'; }; | ||
promptly.prompt('something: ', { validator: validator }, function (err, value) { | ||
@@ -110,9 +111,7 @@ expect(err).to.be(null); | ||
process.stdin.emit('data', ' yeaa \n'); | ||
sendLine(' yeaa '); | ||
}); | ||
it('should automatically retry if a validator fails by default', function (next) { | ||
stdout = ''; | ||
var validator = function (value) { | ||
function validator(value) { | ||
if (value !== 'yeaa') { | ||
@@ -123,3 +122,3 @@ throw new Error('bla'); | ||
return value; | ||
}; | ||
} | ||
@@ -134,11 +133,9 @@ promptly.prompt('something: ', { validator: validator, retry: true }, function (err, value) { | ||
process.stdin.emit('data', 'wtf\n'); | ||
process.stdin.emit('data', 'yeaa\n'); | ||
sendLine('wtf'); | ||
sendLine('yeaa'); | ||
}); | ||
it('should give error if the validator fails and retry is false', function (next) { | ||
stdout = ''; | ||
function validator() { throw new Error('bla'); } | ||
var validator = function () { throw new Error('bla'); }; | ||
promptly.prompt('something: ', { validator: validator, retry: false }, function (err) { | ||
@@ -151,9 +148,9 @@ expect(err).to.be.an(Error); | ||
process.stdin.emit('data', ' yeaa \n'); | ||
sendLine(' yeaa '); | ||
}); | ||
it('should give retry ability on error', function (next) { | ||
stdout = ''; | ||
var times = 0; | ||
var validator = function (value) { | ||
function validator(value) { | ||
if (value !== 'yeaa') { | ||
@@ -164,4 +161,3 @@ throw new Error('bla'); | ||
return value; | ||
}, | ||
times = 0; | ||
} | ||
@@ -174,3 +170,3 @@ promptly.prompt('something: ', { validator: validator, retry: false }, function (err, value) { | ||
err.retry(); | ||
return process.stdin.emit('data', 'yeaa\n'); | ||
return sendLine('yeaa'); | ||
} | ||
@@ -184,5 +180,28 @@ | ||
process.stdin.emit('data', 'wtf\n'); | ||
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'); | ||
}); | ||
}); | ||
@@ -192,4 +211,2 @@ | ||
it('should keep asking on invalid choice', function (next) { | ||
stdout = ''; | ||
promptly.choose('apple or orange: ', ['apple', 'orange'], function (err, value) { | ||
@@ -204,9 +221,7 @@ expect(err).to.be(null); | ||
process.stdin.emit('data', 'bleh\n'); | ||
process.stdin.emit('data', 'orange\n'); | ||
sendLine('bleh'); | ||
sendLine('orange'); | ||
}); | ||
it('should error on invalid choice if retry is disabled', function (next) { | ||
stdout = ''; | ||
promptly.choose('apple or orange: ', ['apple', 'orange'], { retry: false }, function (err) { | ||
@@ -219,8 +234,6 @@ expect(err).to.be.an(Error); | ||
process.stdin.emit('data', 'bleh\n'); | ||
sendLine('bleh'); | ||
}); | ||
it('should be ok on valid choice', function (next) { | ||
stdout = ''; | ||
promptly.choose('apple or orange: ', ['apple', 'orange'], function (err, value) { | ||
@@ -233,8 +246,6 @@ expect(err).to.be(null); | ||
process.stdin.emit('data', 'apple\n'); | ||
sendLine('apple'); | ||
}); | ||
it('should not use strict comparison when matching against valid choices', function (next) { | ||
stdout = ''; | ||
promptly.choose('choices: ', [1, 2, 3], function (err, value) { | ||
@@ -248,3 +259,3 @@ expect(err).to.be(null); | ||
process.stdin.emit('data', '1\n'); | ||
sendLine('1'); | ||
}); | ||
@@ -255,4 +266,2 @@ }); | ||
it('should be ok on valid choice and coerce to boolean values', function (next) { | ||
stdout = ''; | ||
async.forEachSeries(['yes', 'Y', 'y', '1'], function (truthy, next) { | ||
@@ -266,3 +275,3 @@ promptly.confirm('test yes: ', { retry: false }, function (err, value) { | ||
process.stdin.emit('data', truthy + '\n'); | ||
sendLine(truthy); | ||
}, function () { | ||
@@ -277,3 +286,3 @@ async.forEachSeries(['no', 'N', 'n', '0'], function (truthy, next) { | ||
process.stdin.emit('data', truthy + '\n'); | ||
sendLine(truthy); | ||
}, next); | ||
@@ -284,4 +293,2 @@ }); | ||
it('should keep asking on invalid choice', function (next) { | ||
stdout = ''; | ||
promptly.confirm('yes or no: ', function (err, value) { | ||
@@ -295,9 +302,7 @@ expect(err).to.be(null); | ||
process.stdin.emit('data', 'bleh\n'); | ||
process.stdin.emit('data', 'y\n'); | ||
sendLine('bleh'); | ||
sendLine('y'); | ||
}); | ||
it('should error on invalid choice if retry is disabled', function (next) { | ||
stdout = ''; | ||
promptly.confirm('yes or no: ', { retry: false }, function (err) { | ||
@@ -310,3 +315,3 @@ expect(err).to.be.an(Error); | ||
process.stdin.emit('data', 'bleh\n'); | ||
sendLine('bleh'); | ||
}); | ||
@@ -317,4 +322,2 @@ }); | ||
it('should prompt the user silently', function (next) { | ||
stdout = ''; | ||
promptly.password('something: ', function (err, value) { | ||
@@ -328,8 +331,6 @@ expect(value).to.be('yeaa'); | ||
process.stdin.emit('data', 'yeaa\n'); | ||
sendLine('yeaa'); | ||
}); | ||
it('should not trim by default', function (next) { | ||
stdout = ''; | ||
promptly.password('something: ', function (err, value) { | ||
@@ -343,8 +344,6 @@ expect(value).to.be(' yeaa '); | ||
process.stdin.emit('data', ' yeaa \n'); | ||
sendLine(' yeaa '); | ||
}); | ||
it('show allow empty passwords by default', function (next) { | ||
stdout = ''; | ||
promptly.password('something: ', function (err, value) { | ||
@@ -357,4 +356,4 @@ expect(value).to.be(''); | ||
process.stdin.emit('data', '\n'); | ||
sendLine(''); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
22173
3.78%418
2.96%1
-50%165
7.84%Updated