test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. 'use strict';
  2. var expect = require('expect.js');
  3. var promptly = require('../index');
  4. var async = require('async');
  5. // Mock stdout
  6. var stdout = '';
  7. var oldWrite = process.stdout.write;
  8. process.stdout.write = function (data) {
  9. stdout += data;
  10. return oldWrite.apply(process.stdout, arguments);
  11. };
  12. // Function to send a line to stdin
  13. function sendLine(line) {
  14. setImmediate(function () {
  15. process.stdin.emit('data', line + '\n');
  16. });
  17. }
  18. beforeEach(function () {
  19. stdout = '';
  20. });
  21. describe('prompt()', function () {
  22. it('should prompt the user', function (next) {
  23. promptly.prompt('something: ', function (err, value) {
  24. expect(err).to.be(null);
  25. expect(value).to.be('yeaa');
  26. expect(stdout).to.contain('something: ');
  27. next();
  28. });
  29. sendLine('yeaa');
  30. });
  31. it('should keep asking if no value is passed and no default was defined', function (next) {
  32. promptly.prompt('something: ', function (err, value) {
  33. expect(err).to.be(null);
  34. expect(value).to.be('yeaa');
  35. expect(stdout).to.contain('something: ');
  36. expect(stdout.indexOf('something')).to.not.be(stdout.lastIndexOf('something'));
  37. next();
  38. });
  39. sendLine('');
  40. sendLine('yeaa');
  41. });
  42. it('should assume default value if nothing is passed', function (next) {
  43. promptly.prompt('something: ', { 'default': 'myValue' }, function (err, value) {
  44. expect(err).to.be(null);
  45. expect(value).to.be('myValue');
  46. expect(stdout).to.contain('something: ');
  47. next();
  48. });
  49. sendLine('');
  50. });
  51. it('should trim the user input if trim is enabled', function (next) {
  52. promptly.prompt('something: ', { trim: true }, function (err, value) {
  53. expect(err).to.be(null);
  54. expect(value).to.be('yeaa');
  55. expect(stdout).to.contain('something: ');
  56. next();
  57. });
  58. sendLine(' yeaa ');
  59. });
  60. it('should call validator after trimming', function (next) {
  61. function validator(value) {
  62. if (value !== 'yeaa') {
  63. throw new Error('bla');
  64. }
  65. return value;
  66. }
  67. promptly.prompt('something: ', { validator: validator, retry: false }, function (err, value) {
  68. expect(err).to.be(null);
  69. expect(value).to.be('yeaa');
  70. expect(stdout).to.contain('something: ');
  71. next();
  72. });
  73. sendLine(' yeaa ');
  74. });
  75. it('should assume values from the validator', function (next) {
  76. function validator() { return 'bla'; }
  77. promptly.prompt('something: ', { validator: validator }, function (err, value) {
  78. expect(err).to.be(null);
  79. expect(value).to.be('bla');
  80. expect(stdout).to.contain('something: ');
  81. next();
  82. });
  83. sendLine(' yeaa ');
  84. });
  85. it('should automatically retry if a validator fails by default', function (next) {
  86. function validator(value) {
  87. if (value !== 'yeaa') {
  88. throw new Error('bla');
  89. }
  90. return value;
  91. }
  92. promptly.prompt('something: ', { validator: validator, retry: true }, function (err, value) {
  93. expect(stdout).to.contain('something: ');
  94. expect(stdout.indexOf('something')).to.not.be(stdout.lastIndexOf('something'));
  95. expect(stdout).to.contain('bla');
  96. expect(value).to.equal('yeaa');
  97. next();
  98. });
  99. sendLine('wtf');
  100. sendLine('yeaa');
  101. });
  102. it('should give error if the validator fails and retry is false', function (next) {
  103. function validator() { throw new Error('bla'); }
  104. promptly.prompt('something: ', { validator: validator, retry: false }, function (err) {
  105. expect(err).to.be.an(Error);
  106. expect(err.message).to.be('bla');
  107. expect(stdout).to.contain('something: ');
  108. next();
  109. });
  110. sendLine(' yeaa ');
  111. });
  112. it('should give retry ability on error', function (next) {
  113. var times = 0;
  114. function validator(value) {
  115. if (value !== 'yeaa') {
  116. throw new Error('bla');
  117. }
  118. return value;
  119. }
  120. promptly.prompt('something: ', { validator: validator, retry: false }, function (err, value) {
  121. times++;
  122. if (times === 1) {
  123. expect(err).to.be.an(Error);
  124. err.retry();
  125. return sendLine('yeaa');
  126. }
  127. expect(value).to.equal('yeaa');
  128. expect(stdout).to.contain('something: ');
  129. expect(stdout.indexOf('something')).to.not.be(stdout.lastIndexOf('something'));
  130. next();
  131. });
  132. sendLine('wtf');
  133. });
  134. it('should write input to stdout by default', function (next) {
  135. promptly.prompt('something: ', function (err, value) {
  136. expect(err).to.be(null);
  137. expect(value).to.be('yeaa');
  138. expect(stdout).to.contain('something: ');
  139. expect(stdout).to.contain(value);
  140. next();
  141. });
  142. sendLine('yeaa');
  143. });
  144. it('should write input to stdout if silent is false', function (next) {
  145. promptly.prompt('something: ', { silent: true }, function (err, value) {
  146. expect(err).to.be(null);
  147. expect(value).to.be('yeaa');
  148. expect(stdout).to.contain('something: ');
  149. expect(stdout).to.not.contain(value);
  150. next();
  151. });
  152. sendLine('yeaa');
  153. });
  154. it('should prompt the user (using promise)', function (next) {
  155. promptly.prompt('something: ')
  156. .then(function (value) {
  157. expect(value).to.be('yeaa');
  158. expect(stdout).to.contain('something: ');
  159. next();
  160. })
  161. .catch(function () {
  162. expect().fail();
  163. next();
  164. });
  165. sendLine('yeaa');
  166. });
  167. });
  168. describe('choose()', function () {
  169. it('should keep asking on invalid choice', function (next) {
  170. promptly.choose('apple or orange? ', ['apple', 'orange'], function (err, value) {
  171. expect(err).to.be(null);
  172. expect(value).to.be('orange');
  173. expect(stdout).to.contain('apple or orange? ');
  174. expect(stdout.indexOf('apple or orange')).to.not.be(stdout.lastIndexOf('apple or orange'));
  175. expect(stdout).to.contain('Invalid choice');
  176. next();
  177. });
  178. sendLine('bleh');
  179. sendLine('orange');
  180. });
  181. it('should error on invalid choice if retry is disabled', function (next) {
  182. promptly.choose('apple or orange? ', ['apple', 'orange'], { retry: false }, function (err) {
  183. expect(err).to.be.an(Error);
  184. expect(err.message).to.contain('choice');
  185. expect(stdout).to.contain('apple or orange? ');
  186. next();
  187. });
  188. sendLine('bleh');
  189. });
  190. it('should be ok on valid choice', function (next) {
  191. promptly.choose('apple or orange? ', ['apple', 'orange'], function (err, value) {
  192. expect(err).to.be(null);
  193. expect(value).to.be('apple');
  194. expect(stdout).to.contain('apple or orange? ');
  195. next();
  196. });
  197. sendLine('apple');
  198. });
  199. it('should not use strict comparison when matching against valid choices', function (next) {
  200. promptly.choose('choices: ', [1, 2, 3], function (err, value) {
  201. expect(err).to.be(null);
  202. expect(typeof value).to.equal('number');
  203. expect(value).to.be(1);
  204. expect(stdout).to.contain('choices: ');
  205. next();
  206. });
  207. sendLine('1');
  208. });
  209. it('should work using promise', function (next) {
  210. promptly.choose('apple or orange? ', ['apple', 'orange'])
  211. .then(function (value) {
  212. expect(value).to.be('orange');
  213. expect(stdout).to.contain('apple or orange? ');
  214. next();
  215. })
  216. .catch(function () {
  217. expect().fail();
  218. next();
  219. });
  220. sendLine('orange');
  221. });
  222. });
  223. describe('confirm()', function () {
  224. it('should be ok on valid choice and coerce to boolean values', function (next) {
  225. async.forEachSeries(['yes', 'Y', 'y', '1'], function (truthy, next) {
  226. promptly.confirm('test yes? ', { retry: false }, function (err, value) {
  227. expect(err).to.be(null);
  228. expect(value).to.be(true);
  229. expect(stdout).to.contain('test yes? ');
  230. next();
  231. });
  232. sendLine(truthy);
  233. }, function () {
  234. async.forEachSeries(['no', 'N', 'n', '0'], function (truthy, next) {
  235. promptly.confirm('test no? ', function (err, value) {
  236. expect(err).to.be(null);
  237. expect(value).to.be(false);
  238. expect(stdout).to.contain('test no? ');
  239. next();
  240. });
  241. sendLine(truthy);
  242. }, next);
  243. });
  244. });
  245. it('should keep asking on invalid choice', function (next) {
  246. promptly.confirm('yes or no? ', function (err, value) {
  247. expect(err).to.be(null);
  248. expect(value).to.be(true);
  249. expect(stdout).to.contain('yes or no? ');
  250. expect(stdout.indexOf('yes or no')).to.not.be(stdout.lastIndexOf('yes or no'));
  251. next();
  252. });
  253. sendLine('bleh');
  254. sendLine('y');
  255. });
  256. it('should error on invalid choice if retry is disabled', function (next) {
  257. promptly.confirm('yes or no? ', { retry: false }, function (err) {
  258. expect(err).to.be.an(Error);
  259. expect(err.message).to.not.contain('Invalid choice');
  260. expect(stdout).to.contain('yes or no? ');
  261. next();
  262. });
  263. sendLine('bleh');
  264. });
  265. it('should work using promise', function (next) {
  266. promptly.confirm('yes or no? ')
  267. .then(function (value) {
  268. expect(stdout).to.contain('yes or no? ');
  269. expect(value).to.be(true);
  270. next();
  271. })
  272. .catch(function () {
  273. expect().fail();
  274. next();
  275. });
  276. sendLine('y');
  277. });
  278. });
  279. describe('password()', function () {
  280. it('should prompt the user silently', function (next) {
  281. promptly.password('something: ', function (err, value) {
  282. expect(value).to.be('yeaa');
  283. expect(stdout).to.contain('something: ');
  284. expect(stdout).to.not.contain('yeaa');
  285. next();
  286. });
  287. sendLine('yeaa');
  288. });
  289. it('should not trim by default', function (next) {
  290. promptly.password('something: ', function (err, value) {
  291. expect(value).to.be(' yeaa ');
  292. expect(stdout).to.contain('something: ');
  293. expect(stdout).to.not.contain(' yeaa ');
  294. next();
  295. });
  296. sendLine(' yeaa ');
  297. });
  298. it('show allow empty passwords by default', function (next) {
  299. promptly.password('something: ', function (err, value) {
  300. expect(value).to.be('');
  301. expect(stdout).to.contain('something: ');
  302. next();
  303. });
  304. sendLine('');
  305. });
  306. it('should prompt the user silently using promise', function (next) {
  307. promptly.password('something: ')
  308. .then(function (value) {
  309. expect(value).to.be('yeaa');
  310. expect(stdout).to.contain('something: ');
  311. expect(stdout).to.not.contain('yeaa');
  312. next();
  313. })
  314. .catch(function () {
  315. expect().fail();
  316. next();
  317. });
  318. sendLine('yeaa');
  319. });
  320. });