test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. var gulpHelp = require('./index.js'),
  2. noop = require('./lib/noop'),
  3. /* jshint unused: true */
  4. should = require('should'),
  5. /* jshint unused: false */
  6. sinon = require('sinon');
  7. /* jshint expr: true */
  8. describe('help', function () {
  9. var gulp, originalTaskFn;
  10. beforeEach(function () {
  11. gulp = null;
  12. originalTaskFn = null;
  13. });
  14. it('should have help task with help text', function () {
  15. gulp = sinon.stub({task: noop, tasks: {help: {}}});
  16. originalTaskFn = gulp.task;
  17. gulpHelp(gulp);
  18. should(originalTaskFn.calledTwice).ok;
  19. should(originalTaskFn.calledWith('default', ['help'])).ok;
  20. should(gulp.tasks.help.help.message).eql('Display this help text.');
  21. });
  22. it('should have a custom help text if passed', function () {
  23. gulp = sinon.stub({task: noop, tasks: {help: {}}});
  24. gulpHelp(gulp, {description: 'help text.'});
  25. should(gulp.tasks.help.help.message).eql('help text.');
  26. });
  27. it('should create an alias if passed', function () {
  28. gulp = sinon.stub({task: noop, tasks: {help: {}}});
  29. originalTaskFn = gulp.task;
  30. gulpHelp(gulp, {aliases: ['h', '?']});
  31. should(gulp.tasks.help.help.message).eql('Display this help text.');
  32. should(gulp.tasks.help.help.aliases).eql('Aliases: h, ?');
  33. should(originalTaskFn.calledWith('h', ['help'])).ok;
  34. should(originalTaskFn.calledWith('?', ['help'])).ok;
  35. });
  36. it('should create an options object if passed', function () {
  37. gulp = sinon.stub({task: noop, tasks: {help: {}}});
  38. originalTaskFn = gulp.task;
  39. gulpHelp(gulp, {aliases: ['h', '?'], options: {opt: 'val'}});
  40. should(gulp.tasks.help.help.options).eql({opt: 'val'});
  41. should(originalTaskFn.calledWith('h', ['help'])).ok;
  42. should(originalTaskFn.calledWith('?', ['help'])).ok;
  43. });
  44. describe('should support old task definitions', function () {
  45. beforeEach(function () {
  46. gulp = sinon.stub({task: noop, tasks: {help: {}, oldStyle: {}}});
  47. originalTaskFn = gulp.task;
  48. gulpHelp(gulp);
  49. should(originalTaskFn.calledTwice).ok;
  50. });
  51. it('with nothing', function () {
  52. gulp.task('oldStyle');
  53. should(originalTaskFn.calledThrice).ok;
  54. should(originalTaskFn.calledWith('oldStyle')).ok;
  55. should(gulp.tasks.oldStyle.help).ok;
  56. should(gulp.tasks.oldStyle.help.message).eql('');
  57. });
  58. it('with nothing null', function () {
  59. gulp.task('oldStyle', null);
  60. should(originalTaskFn.calledThrice).ok;
  61. should(originalTaskFn.calledWith('oldStyle')).ok;
  62. should(gulp.tasks.oldStyle.help).ok;
  63. should(gulp.tasks.oldStyle.help.message).eql('');
  64. should(gulp.tasks.oldStyle.help.depsMessage).eql('');
  65. });
  66. it('with func', function () {
  67. gulp.task('oldStyle', noop);
  68. should(originalTaskFn.calledThrice).ok;
  69. should(originalTaskFn.calledWith('oldStyle', noop)).ok;
  70. should(gulp.tasks.oldStyle.help).ok;
  71. should(gulp.tasks.oldStyle.help.message).eql('');
  72. should(gulp.tasks.oldStyle.help.depsMessage).eql('');
  73. });
  74. it('with deps and func', function () {
  75. gulp.task('oldStyle', ['dep'], noop);
  76. should(originalTaskFn.calledThrice).ok;
  77. should(originalTaskFn.calledWith('oldStyle', ['dep'], noop)).ok;
  78. should(gulp.tasks.oldStyle.help).ok;
  79. should(gulp.tasks.oldStyle.help.message).eql('');
  80. should(gulp.tasks.oldStyle.help.depsMessage).eql('[dep]');
  81. });
  82. it('with deps and no func', function () {
  83. gulp.task('oldStyle', ['dep']);
  84. should(originalTaskFn.calledThrice).ok;
  85. should(originalTaskFn.calledWith('oldStyle', ['dep'])).ok;
  86. should(gulp.tasks.oldStyle.help).ok;
  87. should(gulp.tasks.oldStyle.help.message).eql('');
  88. should(gulp.tasks.oldStyle.help.depsMessage).eql('[dep]');
  89. });
  90. });
  91. describe('should support new task definitions', function () {
  92. beforeEach(function () {
  93. gulp = sinon.stub({task: noop, tasks: {help: {}, newStyle: {}}});
  94. originalTaskFn = gulp.task;
  95. gulpHelp(gulp);
  96. should(originalTaskFn.calledTwice).ok;
  97. });
  98. it('with help text and no nothing', function () {
  99. gulp.task('newStyle', 'help text here');
  100. should(originalTaskFn.callCount).eql(3);
  101. should(originalTaskFn.calledWith('newStyle')).ok;
  102. should(gulp.tasks.newStyle.help).ok;
  103. should(gulp.tasks.newStyle.help.message).eql('help text here');
  104. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  105. });
  106. it('with help text and no deps', function () {
  107. gulp.task('newStyle', 'help text here', noop);
  108. should(originalTaskFn.callCount).eql(3);
  109. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  110. should(gulp.tasks.newStyle.help).ok;
  111. should(gulp.tasks.newStyle.help.message).eql('help text here');
  112. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  113. });
  114. it('with help text and deps', function () {
  115. gulp.task('newStyle', 'help text here', ['dep'], noop);
  116. should(originalTaskFn.callCount).eql(3);
  117. should(originalTaskFn.calledWith('newStyle', ['dep'], noop)).ok;
  118. should(gulp.tasks.newStyle.help).ok;
  119. should(gulp.tasks.newStyle.help.message).eql('help text here');
  120. should(gulp.tasks.newStyle.help.depsMessage).eql('[dep]');
  121. });
  122. it('with help text', function () {
  123. gulp.task('newStyle', true);
  124. should(originalTaskFn.callCount).eql(3);
  125. should(originalTaskFn.calledWith('newStyle')).ok;
  126. should(gulp.tasks.newStyle.help).ok;
  127. should(gulp.tasks.newStyle.help.message).eql('');
  128. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  129. });
  130. it('with help text null', function () {
  131. gulp.task('newStyle', null);
  132. should(originalTaskFn.callCount).eql(3);
  133. should(originalTaskFn.calledWith('newStyle')).ok;
  134. should(gulp.tasks.newStyle.help).ok;
  135. should(gulp.tasks.newStyle.help.message).eql('');
  136. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  137. });
  138. it('with disabled help text and no deps', function () {
  139. gulp.task('newStyle', false, noop);
  140. should(originalTaskFn.callCount).eql(3);
  141. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  142. should(gulp.tasks.newStyle.help).eql(undefined);
  143. });
  144. it('with disabled help text and deps', function () {
  145. gulp.task('newStyle', false, ['dep'], noop);
  146. should(originalTaskFn.callCount).eql(3);
  147. should(originalTaskFn.calledWith('newStyle', ['dep'], noop)).ok;
  148. should(gulp.tasks.newStyle.help).eql(undefined);
  149. });
  150. it('with aliases', function () {
  151. gulp.task('newStyle', 'description.', ['dep'], noop, {aliases: ['new-style', 'nstyle']});
  152. should(originalTaskFn.callCount).eql(5);
  153. should(originalTaskFn.calledWith('newStyle', ['dep'], noop)).ok;
  154. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  155. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  156. should(gulp.tasks.newStyle.help).ok;
  157. should(gulp.tasks.newStyle.help.message).eql('description.');
  158. should(gulp.tasks.newStyle.help.aliases).eql('Aliases: new-style, nstyle');
  159. should(gulp.tasks.newStyle.help.depsMessage).eql('[dep]');
  160. });
  161. it('with aliases no help', function () {
  162. gulp.task('newStyle', ['dep'], noop, {aliases: ['new-style', 'nstyle']});
  163. should(originalTaskFn.callCount).eql(5);
  164. should(originalTaskFn.calledWith('newStyle', ['dep'], noop)).ok;
  165. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  166. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  167. should(gulp.tasks.newStyle.help).ok;
  168. should(gulp.tasks.newStyle.help.message).eql('');
  169. should(gulp.tasks.newStyle.help.aliases).eql('Aliases: new-style, nstyle');
  170. should(gulp.tasks.newStyle.help.depsMessage).eql('[dep]');
  171. });
  172. it('with aliases no deps', function () {
  173. gulp.task('newStyle', 'description.', noop, {aliases: ['new-style', 'nstyle']});
  174. should(originalTaskFn.callCount).eql(5);
  175. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  176. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  177. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  178. should(gulp.tasks.newStyle.help).ok;
  179. should(gulp.tasks.newStyle.help.message).eql('description.');
  180. should(gulp.tasks.newStyle.help.aliases).eql('Aliases: new-style, nstyle');
  181. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  182. });
  183. it('with aliases, disabled help and no deps', function () {
  184. gulp.task('newStyle', false, noop, {aliases: ['new-style', 'nstyle']});
  185. should(originalTaskFn.callCount).eql(5);
  186. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  187. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  188. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  189. should(gulp.tasks.newStyle.help).eql(undefined);
  190. });
  191. it('with aliases no help no deps', function () {
  192. gulp.task('newStyle', noop, {aliases: ['new-style', 'nstyle']});
  193. should(originalTaskFn.callCount).eql(5);
  194. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  195. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  196. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  197. should(gulp.tasks.newStyle.help).ok;
  198. should(gulp.tasks.newStyle.help.message).eql('');
  199. should(gulp.tasks.newStyle.help.aliases).eql('Aliases: new-style, nstyle');
  200. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  201. });
  202. it('with options', function () {
  203. gulp.task('newStyle', 'description.', ['dep'], noop, {options: {key: 'val', key2: 'val2'}});
  204. should(originalTaskFn.callCount).eql(3);
  205. should(originalTaskFn.calledWith('newStyle', ['dep'], noop)).ok;
  206. should(gulp.tasks.newStyle.help).ok;
  207. should(gulp.tasks.newStyle.help.options).eql({key: 'val', key2: 'val2'});
  208. should(gulp.tasks.newStyle.help.message).eql('description.');
  209. should(gulp.tasks.newStyle.help.depsMessage).eql('[dep]');
  210. });
  211. it('with options no help', function () {
  212. gulp.task('newStyle', ['dep'], noop, {options: {key: 'val', key2: 'val2'}});
  213. should(originalTaskFn.callCount).eql(3);
  214. should(originalTaskFn.calledWith('newStyle', ['dep'], noop)).ok;
  215. should(gulp.tasks.newStyle.help).ok;
  216. should(gulp.tasks.newStyle.help.options).eql({key: 'val', key2: 'val2'});
  217. should(gulp.tasks.newStyle.help.message).eql('');
  218. should(gulp.tasks.newStyle.help.depsMessage).eql('[dep]');
  219. });
  220. it('with options no deps', function () {
  221. gulp.task('newStyle', 'description.', noop, {options: {key: 'val', key2: 'val2'}});
  222. should(originalTaskFn.callCount).eql(3);
  223. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  224. should(gulp.tasks.newStyle.help).ok;
  225. should(gulp.tasks.newStyle.help.options).eql({key: 'val', key2: 'val2'});
  226. should(gulp.tasks.newStyle.help.message).eql('description.');
  227. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  228. });
  229. it('with options, disabled help and no deps', function () {
  230. gulp.task('newStyle', false, noop, {options: {key: 'val', key2: 'val2'}});
  231. should(originalTaskFn.callCount).eql(3);
  232. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  233. should(gulp.tasks.newStyle.help).eql(undefined);
  234. });
  235. it('with options, no help no deps', function () {
  236. gulp.task('newStyle', noop, {options: {key: 'val', key2: 'val2'}});
  237. should(originalTaskFn.callCount).eql(3);
  238. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  239. should(gulp.tasks.newStyle.help).ok;
  240. should(gulp.tasks.newStyle.help.options).eql({key: 'val', key2: 'val2'});
  241. should(gulp.tasks.newStyle.help.message).eql('');
  242. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  243. });
  244. it('with options and aliases', function () {
  245. gulp.task('newStyle', 'description.', ['dep'], noop, {
  246. aliases: ['new-style', 'nstyle'],
  247. options: {key: 'val', key2: 'val2'}
  248. });
  249. should(originalTaskFn.callCount).eql(5);
  250. should(originalTaskFn.calledWith('newStyle', ['dep'], noop)).ok;
  251. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  252. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  253. should(gulp.tasks.newStyle.help).ok;
  254. should(gulp.tasks.newStyle.help.options).eql({key: 'val', key2: 'val2'});
  255. should(gulp.tasks.newStyle.help.message).eql('description.');
  256. should(gulp.tasks.newStyle.help.aliases).eql('Aliases: new-style, nstyle');
  257. should(gulp.tasks.newStyle.help.depsMessage).eql('[dep]');
  258. });
  259. it('with options and aliases no help', function () {
  260. gulp.task('newStyle', ['dep'], noop, {
  261. aliases: ['new-style', 'nstyle'],
  262. options: {key: 'val', key2: 'val2'}
  263. });
  264. should(originalTaskFn.callCount).eql(5);
  265. should(originalTaskFn.calledWith('newStyle', ['dep'], noop)).ok;
  266. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  267. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  268. should(gulp.tasks.newStyle.help).ok;
  269. should(gulp.tasks.newStyle.help.options).eql({key: 'val', key2: 'val2'});
  270. should(gulp.tasks.newStyle.help.message).eql('');
  271. should(gulp.tasks.newStyle.help.aliases).eql('Aliases: new-style, nstyle');
  272. should(gulp.tasks.newStyle.help.depsMessage).eql('[dep]');
  273. });
  274. it('with options and aliases no deps', function () {
  275. gulp.task('newStyle', 'description.', noop, {
  276. aliases: ['new-style', 'nstyle'],
  277. options: {key: 'val', key2: 'val2'}
  278. });
  279. should(originalTaskFn.callCount).eql(5);
  280. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  281. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  282. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  283. should(gulp.tasks.newStyle.help).ok;
  284. should(gulp.tasks.newStyle.help.options).eql({key: 'val', key2: 'val2'});
  285. should(gulp.tasks.newStyle.help.message).eql('description.');
  286. should(gulp.tasks.newStyle.help.aliases).eql('Aliases: new-style, nstyle');
  287. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  288. });
  289. it('with options and aliases, disabled help and no deps', function () {
  290. gulp.task('newStyle', false, noop, {aliases: ['new-style', 'nstyle'], options: {key: 'val', key2: 'val2'}});
  291. should(originalTaskFn.callCount).eql(5);
  292. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  293. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  294. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  295. should(gulp.tasks.newStyle.help).eql(undefined);
  296. });
  297. it('with options and aliases, no help no deps', function () {
  298. gulp.task('newStyle', noop, {aliases: ['new-style', 'nstyle'], options: {key: 'val', key2: 'val2'}});
  299. should(originalTaskFn.callCount).eql(5);
  300. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  301. should(originalTaskFn.calledWith('new-style', ['newStyle'], noop)).ok;
  302. should(originalTaskFn.calledWith('nstyle', ['newStyle'], noop)).ok;
  303. should(gulp.tasks.newStyle.help).ok;
  304. should(gulp.tasks.newStyle.help.options).eql({key: 'val', key2: 'val2'});
  305. should(gulp.tasks.newStyle.help.message).eql('');
  306. should(gulp.tasks.newStyle.help.aliases).eql('Aliases: new-style, nstyle');
  307. should(gulp.tasks.newStyle.help.depsMessage).eql('');
  308. });
  309. it('and replace help message of a task being rewritten', function () {
  310. gulp.task('newStyle', noop);
  311. gulp.task('newStyle', false, noop);
  312. should(originalTaskFn.callCount).eql(4);
  313. should(originalTaskFn.calledWith('newStyle', noop)).ok;
  314. should(gulp.tasks.newStyle.help).eql(undefined);
  315. });
  316. it('with two deps', function () {
  317. gulp.task('newStyle', 'help text here', ['dep', 'dep-2'], noop);
  318. should(originalTaskFn.callCount).eql(3);
  319. should(originalTaskFn.calledWith('newStyle', ['dep', 'dep-2'], noop)).ok;
  320. should(gulp.tasks.newStyle.help).ok;
  321. should(gulp.tasks.newStyle.help.depsMessage).eql('[dep, dep-2]');
  322. });
  323. });
  324. describe('should throw error', function () {
  325. function shouldThrowGulpPluginError(func) {
  326. (function () {
  327. func();
  328. }).should.throw(/^gulp-help: Unexpected arg types/);
  329. }
  330. beforeEach(function () {
  331. gulp = sinon.stub({task: noop, tasks: {help: {}, aTask: {}}});
  332. originalTaskFn = gulp.task;
  333. gulpHelp(gulp);
  334. should(originalTaskFn.calledTwice).ok;
  335. });
  336. it('with no args given', function () {
  337. shouldThrowGulpPluginError(function () {
  338. gulp.task();
  339. });
  340. });
  341. it('null name', function () {
  342. shouldThrowGulpPluginError(function () {
  343. gulp.task(null);
  344. });
  345. });
  346. });
  347. });