You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
6.6 KiB

7 years ago
  1. var Hash = require('hashish');
  2. var optimist = require('../index');
  3. var test = require('tap').test;
  4. test('usageFail', function (t) {
  5. var r = checkUsage(function () {
  6. return optimist('-x 10 -z 20'.split(' '))
  7. .usage('Usage: $0 -x NUM -y NUM')
  8. .demand(['x','y'])
  9. .argv;
  10. });
  11. t.same(
  12. r.result,
  13. { x : 10, z : 20, _ : [], $0 : './usage' }
  14. );
  15. t.same(
  16. r.errors.join('\n').split(/\n+/),
  17. [
  18. 'Usage: ./usage -x NUM -y NUM',
  19. 'Options:',
  20. ' -x [required]',
  21. ' -y [required]',
  22. 'Missing required arguments: y',
  23. ]
  24. );
  25. t.same(r.logs, []);
  26. t.ok(r.exit);
  27. t.end();
  28. });
  29. test('usagePass', function (t) {
  30. var r = checkUsage(function () {
  31. return optimist('-x 10 -y 20'.split(' '))
  32. .usage('Usage: $0 -x NUM -y NUM')
  33. .demand(['x','y'])
  34. .argv;
  35. });
  36. t.same(r, {
  37. result : { x : 10, y : 20, _ : [], $0 : './usage' },
  38. errors : [],
  39. logs : [],
  40. exit : false,
  41. });
  42. t.end();
  43. });
  44. test('checkPass', function (t) {
  45. var r = checkUsage(function () {
  46. return optimist('-x 10 -y 20'.split(' '))
  47. .usage('Usage: $0 -x NUM -y NUM')
  48. .check(function (argv) {
  49. if (!('x' in argv)) throw 'You forgot about -x';
  50. if (!('y' in argv)) throw 'You forgot about -y';
  51. })
  52. .argv;
  53. });
  54. t.same(r, {
  55. result : { x : 10, y : 20, _ : [], $0 : './usage' },
  56. errors : [],
  57. logs : [],
  58. exit : false,
  59. });
  60. t.end();
  61. });
  62. test('checkFail', function (t) {
  63. var r = checkUsage(function () {
  64. return optimist('-x 10 -z 20'.split(' '))
  65. .usage('Usage: $0 -x NUM -y NUM')
  66. .check(function (argv) {
  67. if (!('x' in argv)) throw 'You forgot about -x';
  68. if (!('y' in argv)) throw 'You forgot about -y';
  69. })
  70. .argv;
  71. });
  72. t.same(
  73. r.result,
  74. { x : 10, z : 20, _ : [], $0 : './usage' }
  75. );
  76. t.same(
  77. r.errors.join('\n').split(/\n+/),
  78. [
  79. 'Usage: ./usage -x NUM -y NUM',
  80. 'You forgot about -y'
  81. ]
  82. );
  83. t.same(r.logs, []);
  84. t.ok(r.exit);
  85. t.end();
  86. });
  87. test('checkCondPass', function (t) {
  88. function checker (argv) {
  89. return 'x' in argv && 'y' in argv;
  90. }
  91. var r = checkUsage(function () {
  92. return optimist('-x 10 -y 20'.split(' '))
  93. .usage('Usage: $0 -x NUM -y NUM')
  94. .check(checker)
  95. .argv;
  96. });
  97. t.same(r, {
  98. result : { x : 10, y : 20, _ : [], $0 : './usage' },
  99. errors : [],
  100. logs : [],
  101. exit : false,
  102. });
  103. t.end();
  104. });
  105. test('checkCondFail', function (t) {
  106. function checker (argv) {
  107. return 'x' in argv && 'y' in argv;
  108. }
  109. var r = checkUsage(function () {
  110. return optimist('-x 10 -z 20'.split(' '))
  111. .usage('Usage: $0 -x NUM -y NUM')
  112. .check(checker)
  113. .argv;
  114. });
  115. t.same(
  116. r.result,
  117. { x : 10, z : 20, _ : [], $0 : './usage' }
  118. );
  119. t.same(
  120. r.errors.join('\n').split(/\n+/).join('\n'),
  121. 'Usage: ./usage -x NUM -y NUM\n'
  122. + 'Argument check failed: ' + checker.toString()
  123. );
  124. t.same(r.logs, []);
  125. t.ok(r.exit);
  126. t.end();
  127. });
  128. test('countPass', function (t) {
  129. var r = checkUsage(function () {
  130. return optimist('1 2 3 --moo'.split(' '))
  131. .usage('Usage: $0 [x] [y] [z] {OPTIONS}')
  132. .demand(3)
  133. .argv;
  134. });
  135. t.same(r, {
  136. result : { _ : [ '1', '2', '3' ], moo : true, $0 : './usage' },
  137. errors : [],
  138. logs : [],
  139. exit : false,
  140. });
  141. t.end();
  142. });
  143. test('countFail', function (t) {
  144. var r = checkUsage(function () {
  145. return optimist('1 2 --moo'.split(' '))
  146. .usage('Usage: $0 [x] [y] [z] {OPTIONS}')
  147. .demand(3)
  148. .argv;
  149. });
  150. t.same(
  151. r.result,
  152. { _ : [ '1', '2' ], moo : true, $0 : './usage' }
  153. );
  154. t.same(
  155. r.errors.join('\n').split(/\n+/),
  156. [
  157. 'Usage: ./usage [x] [y] [z] {OPTIONS}',
  158. 'Not enough non-option arguments: got 2, need at least 3',
  159. ]
  160. );
  161. t.same(r.logs, []);
  162. t.ok(r.exit);
  163. t.end();
  164. });
  165. test('defaultSingles', function (t) {
  166. var r = checkUsage(function () {
  167. return optimist('--foo 50 --baz 70 --powsy'.split(' '))
  168. .default('foo', 5)
  169. .default('bar', 6)
  170. .default('baz', 7)
  171. .argv
  172. ;
  173. });
  174. t.same(r.result, {
  175. foo : '50',
  176. bar : 6,
  177. baz : '70',
  178. powsy : true,
  179. _ : [],
  180. $0 : './usage',
  181. });
  182. t.end();
  183. });
  184. test('defaultAliases', function (t) {
  185. var r = checkUsage(function () {
  186. return optimist('')
  187. .alias('f', 'foo')
  188. .default('f', 5)
  189. .argv
  190. ;
  191. });
  192. t.same(r.result, {
  193. f : '5',
  194. foo : '5',
  195. _ : [],
  196. $0 : './usage',
  197. });
  198. t.end();
  199. });
  200. test('defaultHash', function (t) {
  201. var r = checkUsage(function () {
  202. return optimist('--foo 50 --baz 70'.split(' '))
  203. .default({ foo : 10, bar : 20, quux : 30 })
  204. .argv
  205. ;
  206. });
  207. t.same(r.result, {
  208. _ : [],
  209. $0 : './usage',
  210. foo : 50,
  211. baz : 70,
  212. bar : 20,
  213. quux : 30,
  214. });
  215. t.end();
  216. });
  217. test('rebase', function (t) {
  218. t.equal(
  219. optimist.rebase('/home/substack', '/home/substack/foo/bar/baz'),
  220. './foo/bar/baz'
  221. );
  222. t.equal(
  223. optimist.rebase('/home/substack/foo/bar/baz', '/home/substack'),
  224. '../../..'
  225. );
  226. t.equal(
  227. optimist.rebase('/home/substack/foo', '/home/substack/pow/zoom.txt'),
  228. '../pow/zoom.txt'
  229. );
  230. t.end();
  231. });
  232. function checkUsage (f) {
  233. var exit = false;
  234. process._exit = process.exit;
  235. process._env = process.env;
  236. process._argv = process.argv;
  237. process.exit = function (t) { exit = true };
  238. process.env = Hash.merge(process.env, { _ : 'node' });
  239. process.argv = [ './usage' ];
  240. var errors = [];
  241. var logs = [];
  242. console._error = console.error;
  243. console.error = function (msg) { errors.push(msg) };
  244. console._log = console.log;
  245. console.log = function (msg) { logs.push(msg) };
  246. var result = f();
  247. process.exit = process._exit;
  248. process.env = process._env;
  249. process.argv = process._argv;
  250. console.error = console._error;
  251. console.log = console._log;
  252. return {
  253. errors : errors,
  254. logs : logs,
  255. exit : exit,
  256. result : result,
  257. };
  258. };