main.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*global describe, it*/
  2. "use strict";
  3. var gulp = require('gulp'),
  4. should = require('should'),
  5. through = require('through2'),
  6. plumber = require('gulp-plumber'),
  7. gutil = require('gulp-util'),
  8. join = require('path').join,
  9. fs = require('fs'),
  10. notify = require('../');
  11. var mockGenerator = function (tester) {
  12. tester = tester || function () { };
  13. return function (opts, callback) {
  14. tester(opts);
  15. callback();
  16. };
  17. };
  18. var originalLogger = notify.logger();
  19. describe('gulp output stream', function() {
  20. beforeEach(function () {
  21. notify.logLevel(0);
  22. notify.logger(originalLogger);
  23. });
  24. describe('notify()', function() {
  25. it('should return a stream', function(done) {
  26. var stream = notify({
  27. notifier: mockGenerator()
  28. });
  29. should.exist(stream);
  30. should.exist(stream.on);
  31. should.exist(stream.pipe);
  32. done();
  33. });
  34. it('should allow setting of own reporter', function(done) {
  35. var notifier = notify.withReporter(mockGenerator);
  36. var stream = notifier();
  37. should.exist(stream);
  38. should.exist(stream.on);
  39. should.exist(stream.pipe);
  40. done();
  41. });
  42. it('should set message, title and have default icon of Gulp logo', function(done) {
  43. var testString = "this is a test";
  44. var expectedIcon = join(__dirname, '..', 'assets', 'gulp.png');
  45. var expectedFile = new gutil.File({
  46. path: "test/fixtures/1.txt",
  47. cwd: "test/",
  48. base: "test/fixtures/",
  49. contents: fs.createReadStream("test/fixtures/1.txt")
  50. });
  51. var mockedNotify = notify.withReporter(mockGenerator(function (opts) {
  52. opts.should.have.property('title');
  53. opts.should.have.property('message');
  54. opts.should.have.property('icon');
  55. String(opts.icon).should.equal(expectedIcon);
  56. String(opts.message).should.equal(testString);
  57. done();
  58. }.bind(this)));
  59. var outstream = mockedNotify({
  60. message: testString
  61. });
  62. outstream.write(expectedFile);
  63. });
  64. it('should be able to override default icon', function(done) {
  65. var testString = "this is a test";
  66. var expectedIcon = "testIcon";
  67. var expectedFile = new gutil.File({
  68. path: "test/fixtures/1.txt",
  69. cwd: "test/",
  70. base: "test/fixtures/",
  71. contents: fs.createReadStream("test/fixtures/1.txt")
  72. });
  73. var mockedNotify = notify.withReporter(mockGenerator(function (opts) {
  74. opts.should.have.property('title');
  75. opts.should.have.property('message');
  76. opts.should.have.property('icon');
  77. String(opts.icon).should.equal(expectedIcon);
  78. String(opts.message).should.equal(testString);
  79. done();
  80. }.bind(this)));
  81. var outstream = mockedNotify({
  82. message: testString,
  83. icon: expectedIcon
  84. });
  85. outstream.write(expectedFile);
  86. });
  87. it('should call notifier with extra options untouched', function(done) {
  88. var testString = "this is a test";
  89. var testIcon = "face-cool";
  90. var mockedNotify = notify.withReporter(mockGenerator(function (opts) {
  91. should.exist(opts);
  92. should.exist(opts.icon);
  93. should.exist(opts.message);
  94. String(opts.icon).should.equal(testIcon);
  95. String(opts.message).should.equal(testString);
  96. }));
  97. var instream = gulp.src(join(__dirname, "./fixtures/*.txt")),
  98. outstream = mockedNotify({
  99. message: testString,
  100. icon: testIcon
  101. });
  102. outstream.on('data', function(file) {
  103. should.exist(file);
  104. should.exist(file.path);
  105. should.exist(file.contents);
  106. });
  107. outstream.on('end', function() {
  108. done();
  109. });
  110. instream.pipe(outstream);
  111. });
  112. it('should emit error when sub-module returns error and emitError is true', function(done) {
  113. var mockedNotify = notify.withReporter(function (options, callback) {
  114. callback(new Error(testString));
  115. });
  116. var testString = "testString",
  117. instream = gulp.src(join(__dirname, "./fixtures/*.txt")),
  118. outstream = mockedNotify({
  119. message: testString,
  120. emitError: true
  121. });
  122. outstream.on('error', function (error) {
  123. should.exist(error);
  124. should.exist(error.message);
  125. String(error.message).should.equal(testString);
  126. done();
  127. });
  128. instream.pipe(outstream);
  129. });
  130. it('should pass on files', function(done) {
  131. var mockedNotify = notify.withReporter(mockGenerator());
  132. var
  133. testSuffix = "tester",
  134. srcFile = join(__dirname, "./fixtures/*"),
  135. instream = gulp.src(srcFile),
  136. outstream = mockedNotify();
  137. var numFilesBefore = 0,
  138. numFilesAfter = 0;
  139. instream
  140. .pipe(through.obj(function (file, enc, cb) {
  141. numFilesBefore++;
  142. this.push(file);
  143. cb();
  144. }, function (cb) {
  145. numFilesBefore.should.equal(3);
  146. cb();
  147. }))
  148. .pipe(outstream)
  149. .pipe(through.obj(function (file, enc, cb) {
  150. numFilesAfter++;
  151. this.push(file);
  152. cb();
  153. }, function (callback) {
  154. numFilesAfter.should.equal(3);
  155. done();
  156. }));
  157. });
  158. it('should pass on files even on error in notifier (with use of plumber)', function(done) {
  159. var
  160. testString = "tester",
  161. srcFile = join(__dirname, "./fixtures/*"),
  162. instream = gulp.src(srcFile),
  163. outstream = notify({
  164. notifier: function (options, callback) {
  165. callback(new Error(testString));
  166. }
  167. });
  168. var numFilesBefore = 0,
  169. numFilesAfter = 0;
  170. instream
  171. .pipe(plumber())
  172. .pipe(through.obj(function (file, enc, cb) {
  173. numFilesBefore++;
  174. this.push(file);
  175. cb();
  176. }, function (cb) {
  177. numFilesBefore.should.equal(3);
  178. cb();
  179. }))
  180. .pipe(outstream)
  181. .on('error', function (error) {
  182. error.message.should.equal(testString);
  183. })
  184. .pipe(through.obj(function (file, enc, cb) {
  185. numFilesAfter++;
  186. this.push(file);
  187. cb();
  188. }, function (callback) {
  189. numFilesAfter.should.equal(3);
  190. done();
  191. }));
  192. });
  193. it('should emit error when sub-module throws exception/error and emitError flag is true', function(done) {
  194. var testString = "some exception",
  195. instream = gulp.src(join(__dirname, "./fixtures/*.txt")),
  196. outstream = notify({
  197. message: testString,
  198. notifier: function (options, callback) {
  199. throw new Error(testString);
  200. },
  201. emitError: true
  202. });
  203. outstream.on('error', function (error) {
  204. should.exist(error);
  205. should.exist(error.message);
  206. String(error.message).should.equal(testString);
  207. done();
  208. });
  209. instream.pipe(outstream);
  210. });
  211. it('should not emit error when sub-module throws exception/error if emitError flag is false (default)', function(done) {
  212. notify.logLevel(1);
  213. notify.logger(function () {
  214. should.exist(true);
  215. done();
  216. });
  217. var testString = "some exception",
  218. expectedFile = new gutil.File({
  219. path: "test/fixtures/1.txt",
  220. cwd: "test/",
  221. base: "test/fixtures/",
  222. contents: fs.createReadStream("test/fixtures/1.txt")
  223. }),
  224. outstream = notify({
  225. message: testString,
  226. notifier: function (options, callback) {
  227. throw new Error(testString);
  228. }
  229. });
  230. outstream.on('error', function (error) {
  231. should.not.exist(error);
  232. should.not.exist(error.message);
  233. String(error.message).should.not.equal(testString);
  234. done();
  235. });
  236. outstream.on('end', function () {
  237. done();
  238. });
  239. outstream.write(expectedFile);
  240. outstream.write(null);
  241. outstream.end();
  242. });
  243. it('should default to notifying file path and default title', function(done) {
  244. var srcFile = join(__dirname, "./fixtures/1.txt");
  245. var instream = gulp.src(srcFile),
  246. outstream = notify({
  247. notifier: mockGenerator(function (opts) {
  248. should.exist(opts);
  249. should.exist(opts.title);
  250. should.exist(opts.message);
  251. String(opts.message).should.equal(srcFile);
  252. String(opts.title).should.equal("Gulp notification");
  253. })
  254. });
  255. outstream.on('data', function(file) {
  256. should.exist(file);
  257. should.exist(file.path);
  258. should.exist(file.contents);
  259. });
  260. outstream.on('end', function() {
  261. done();
  262. });
  263. instream.pipe(outstream);
  264. });
  265. it('should take function with file as argument, as message or title', function(done) {
  266. var
  267. testSuffix = "tester",
  268. srcFile = join(__dirname, "./fixtures/1.txt"),
  269. instream = gulp.src(srcFile),
  270. outstream = notify({
  271. notifier: mockGenerator(function (opts) {
  272. should.exist(opts);
  273. should.exist(opts.title);
  274. should.exist(opts.message);
  275. String(opts.message).should.equal(srcFile + testSuffix);
  276. String(opts.title).should.equal(srcFile + testSuffix);
  277. }),
  278. message: function (file) {
  279. String(file.path).should.equal(srcFile);
  280. return file.path + testSuffix;
  281. },
  282. title: function (file) {
  283. String(file.path).should.equal(srcFile);
  284. return file.path + testSuffix;
  285. }
  286. });
  287. outstream.on('data', function(file) {
  288. should.exist(file);
  289. should.exist(file.path);
  290. should.exist(file.contents);
  291. });
  292. outstream.on('end', function() {
  293. done();
  294. });
  295. instream.pipe(outstream);
  296. });
  297. it('should notify on all files per default', function(done) {
  298. var
  299. testSuffix = "tester",
  300. srcFile = join(__dirname, "./fixtures/*"),
  301. instream = gulp.src(srcFile),
  302. numFunctionCalls = 0,
  303. outstream = notify({
  304. notifier: mockGenerator(function (opts) {
  305. should.exist(opts);
  306. should.exist(opts.title);
  307. should.exist(opts.message);
  308. numFunctionCalls++;
  309. })
  310. });
  311. outstream.on('data', function(file) {
  312. should.exist(file);
  313. should.exist(file.path);
  314. should.exist(file.contents);
  315. });
  316. outstream.on('end', function() {
  317. numFunctionCalls.should.equal(3);
  318. done();
  319. });
  320. instream.pipe(outstream)
  321. });
  322. it('should handle streamed files', function (done) {
  323. var expectedFile = new gutil.File({
  324. path: "test/fixtures/1.txt",
  325. cwd: "test/",
  326. base: "test/fixtures/",
  327. contents: fs.createReadStream("test/fixtures/1.txt")
  328. });
  329. var testString = "testString";
  330. var outstream = notify({
  331. message: testString,
  332. notifier: mockGenerator(function (opts) {
  333. should.exist(opts);
  334. should.exist(opts.title);
  335. should.exist(opts.message);
  336. String(opts.message).should.equal(testString);
  337. })
  338. });
  339. outstream.on('error', function (err) {
  340. should.not.exist(err);
  341. });
  342. outstream.on('data', function(file) {
  343. should.exist(file);
  344. should.exist(file.isStream());
  345. should.exist(file.path);
  346. should.exist(file.contents);
  347. done();
  348. });
  349. outstream.write(expectedFile);
  350. });
  351. it('should support lodash template for titles and messages', function (done) {
  352. var expectedFile = new gutil.File({
  353. path: "test/fixtures/1.txt",
  354. cwd: "test/",
  355. base: "test/fixtures/",
  356. contents: fs.createReadStream("test/fixtures/1.txt")
  357. });
  358. var testString = "Template: <%= file.relative %>";
  359. var expectedString = "Template: 1.txt";
  360. var outstream = notify({
  361. message: testString,
  362. title: testString,
  363. notifier: mockGenerator(function (opts) {
  364. should.exist(opts);
  365. should.exist(opts.title);
  366. should.exist(opts.message);
  367. String(opts.message).should.equal(expectedString);
  368. String(opts.title).should.equal(expectedString);
  369. })
  370. });
  371. outstream.on('error', function (err) {
  372. should.not.exist(err);
  373. });
  374. outstream.on('data', function(file) {
  375. should.exist(file);
  376. should.exist(file.path);
  377. should.exist(file.contents);
  378. done();
  379. });
  380. outstream.write(expectedFile);
  381. });
  382. });
  383. describe('notify.onLast', function() {
  384. it('should only notify on the last file, if onLast flag is activated', function(done) {
  385. var
  386. testSuffix = "tester",
  387. srcFile = join(__dirname, "./fixtures/*"),
  388. instream = gulp.src(srcFile),
  389. numFunctionCalls = 0,
  390. outstream = notify({
  391. onLast: true,
  392. notifier: mockGenerator(function (opts) {
  393. should.exist(opts);
  394. should.exist(opts.title);
  395. should.exist(opts.message);
  396. numFunctionCalls++;
  397. })
  398. });
  399. outstream.on('data', function(file) {
  400. should.exist(file);
  401. should.exist(file.path);
  402. should.exist(file.contents);
  403. });
  404. outstream.on('end', function() {
  405. numFunctionCalls.should.equal(1);
  406. done();
  407. });
  408. instream.pipe(outstream);
  409. });
  410. it('should stream all files even if onLast is activated', function(done) {
  411. var
  412. testSuffix = "tester",
  413. srcFile = join(__dirname, "./fixtures/*"),
  414. instream = gulp.src(srcFile),
  415. outstream = notify({
  416. onLast: true,
  417. notifier: mockGenerator()
  418. });
  419. var numFilesBefore = 0,
  420. numFilesAfter = 0;
  421. instream
  422. .pipe(through.obj(function (file, enc, cb) {
  423. numFilesBefore++;
  424. this.push(file);
  425. cb();
  426. }, function (cb) {
  427. numFilesBefore.should.equal(3);
  428. cb();
  429. }))
  430. .pipe(outstream)
  431. .pipe(through.obj(function (file, enc, cb) {
  432. numFilesAfter++;
  433. this.push(file);
  434. cb();
  435. }, function (callback) {
  436. numFilesAfter.should.equal(3);
  437. done();
  438. }));
  439. });
  440. it('should support lodash template for titles and messages when onLast', function (done) {
  441. var
  442. testSuffix = "tester",
  443. srcFile = join(__dirname, "./fixtures/*"),
  444. instream = gulp.src(srcFile),
  445. numFunctionCalls = 0,
  446. outstream = notify({
  447. onLast: true,
  448. message: 'Template: <%= file.relative %>',
  449. notifier: mockGenerator(function (opts) {
  450. should.exist(opts);
  451. should.exist(opts.title);
  452. should.exist(opts.message);
  453. opts.message.should.startWith('Template:')
  454. opts.message.should.endWith('.txt')
  455. numFunctionCalls++;
  456. })
  457. });
  458. outstream.on('data', function(file) {
  459. should.exist(file);
  460. should.exist(file.path);
  461. should.exist(file.contents);
  462. });
  463. outstream.on('end', function() {
  464. numFunctionCalls.should.equal(1);
  465. done();
  466. });
  467. instream.pipe(outstream);
  468. });
  469. });
  470. describe('notify.logger()', function() {
  471. it('should allow setting of logLevel', function(done) {
  472. var notifier = notify.withReporter(mockGenerator);
  473. should.exist(notify.logLevel);
  474. should.exist(notify.logger);
  475. done();
  476. });
  477. it('should log error on log level 1', function(done) {
  478. var srcFile = join(__dirname, "./fixtures/1.txt");
  479. notify.logLevel(1);
  480. notify.logger(function (options) {
  481. should.exist(true);
  482. done();
  483. });
  484. var notifier = notify.withReporter(mockGenerator);
  485. gulp.src(srcFile)
  486. .pipe(through.obj(function (file, enc, cb) {
  487. this.emit("error", new gutil.PluginError("testPlugin", "foo"));
  488. cb();
  489. }))
  490. .on("error", notifier.onError())
  491. });
  492. it('should not log on log level 0', function(done) {
  493. var srcFile = join(__dirname, "./fixtures/1.txt");
  494. var hasBeenCalled = false;
  495. notify.logLevel(0);
  496. notify.logger(function () {
  497. hasBeenCalled = true;
  498. should.not.exist(arguments);
  499. });
  500. var notifier = notify.withReporter(mockGenerator(function () {
  501. should(hasBeenCalled).equal(false);
  502. done();
  503. }));
  504. gulp.src(srcFile)
  505. .pipe(through.obj(function (file, enc, cb) {
  506. this.emit("error", new gutil.PluginError("testPlugin", "foo"));
  507. cb();
  508. }))
  509. .on("error", notifier.onError())
  510. });
  511. });
  512. describe('notify.onError()', function() {
  513. it('should have defined onError function on object', function (done) {
  514. should.exist(notify.onError);
  515. done();
  516. });
  517. it('should have onError event withReporter', function(done) {
  518. var notifier = notify.withReporter(mockGenerator);
  519. should.exist(notifier.onError);
  520. done();
  521. });
  522. it('should call end on stream', function (done) {
  523. var onError = notify.onError({
  524. notifier: mockGenerator(function (opts) { })
  525. });
  526. var stream = through.obj(function (file, enc, cb) {
  527. this.emit('error', 'error');
  528. cb();
  529. });
  530. stream.on('error', onError).on('end', done);
  531. stream.write({});
  532. });
  533. it('should be limited by notifying on error if th onError-option is passed', function (done) {
  534. var
  535. testMessage = "tester",
  536. srcFile = join(__dirname, "./fixtures/*"),
  537. onError = notify.onError({
  538. notifier: mockGenerator(function (opts) {
  539. should.exist(opts);
  540. should.exist(opts.title);
  541. should.exist(opts.message);
  542. String(opts.message).should.equal(testMessage);
  543. String(opts.title).should.equal("Error running Gulp");
  544. done();
  545. })
  546. });
  547. gulp.src(srcFile)
  548. .pipe(through.obj(function (file, enc, cb) {
  549. this.emit("error", new gutil.PluginError("testPlugin", testMessage));
  550. cb();
  551. }))
  552. .on("error", onError)
  553. });
  554. it('should have onError options on withReporter sugar', function (done) {
  555. var
  556. testMessage = "tester",
  557. srcFile = join(__dirname, "./fixtures/*");
  558. var custom = notify.withReporter(mockGenerator(function (opts) {
  559. should.exist(opts);
  560. should.exist(opts.title);
  561. should.exist(opts.message);
  562. String(opts.message).should.equal(testMessage);
  563. String(opts.title).should.equal("Error running Gulp");
  564. done();
  565. }));
  566. gulp.src(srcFile)
  567. .pipe(through.obj(function (file, enc, cb) {
  568. this.emit("error", new gutil.PluginError("testPlugin", testMessage));
  569. cb();
  570. }))
  571. .on("error", custom.onError())
  572. });
  573. it('should have default Gulp-error logo onError', function (done) {
  574. var expectedIcon = join(__dirname, '..', 'assets', 'gulp-error.png');
  575. var
  576. testMessage = "tester",
  577. srcFile = join(__dirname, "./fixtures/*");
  578. var custom = notify.withReporter(mockGenerator(function (opts) {
  579. should.exist(opts);
  580. should.exist(opts.title);
  581. should.exist(opts.message);
  582. should.exist(opts.icon);
  583. String(opts.message).should.equal(testMessage);
  584. String(opts.icon).should.equal(expectedIcon);
  585. String(opts.title).should.equal("Error running Gulp");
  586. done();
  587. }));
  588. gulp.src(srcFile)
  589. .pipe(through.obj(function (file, enc, cb) {
  590. this.emit("error", new gutil.PluginError("testPlugin", testMessage));
  591. cb();
  592. }))
  593. .on("error", custom.onError())
  594. });
  595. it('should be able to override default icon onError', function (done) {
  596. var expectedIcon = "testIcon";
  597. var testMessage = "tester";
  598. var custom = notify.withReporter(mockGenerator(function (opts) {
  599. should.exist(opts);
  600. should.exist(opts.title);
  601. should.exist(opts.message);
  602. should.exist(opts.icon);
  603. String(opts.message).should.equal(testMessage);
  604. String(opts.icon).should.equal(expectedIcon);
  605. String(opts.title).should.equal("Error running Gulp");
  606. done();
  607. }));
  608. custom.onError({
  609. icon: expectedIcon,
  610. })(new gutil.PluginError("testPlugin", testMessage));
  611. });
  612. it('should have Frog sound per default onError', function (done) {
  613. var expectedIcon = "testIcon";
  614. var testMessage = "tester";
  615. var custom = notify.withReporter(mockGenerator(function (opts) {
  616. should.exist(opts);
  617. should.exist(opts.sound);
  618. String(opts.sound).should.equal('Frog');
  619. done();
  620. }));
  621. custom.onError({
  622. icon: expectedIcon,
  623. })(new gutil.PluginError("testPlugin", testMessage));
  624. });
  625. it('should support lodash template for titles and messages on onError', function (done) {
  626. var testString = "Template: <%= error.message %>";
  627. var expectedString = "Template: test";
  628. var srcFile = join(__dirname, "./fixtures/*");
  629. var onError = notify.onError({
  630. message: testString,
  631. title: testString,
  632. notifier: mockGenerator(function (opts) {
  633. should.exist(opts);
  634. should.exist(opts.title);
  635. should.exist(opts.message);
  636. String(opts.message).should.equal(expectedString);
  637. String(opts.title).should.equal(expectedString);
  638. done();
  639. })
  640. });
  641. gulp.src(srcFile)
  642. .pipe(through.obj(function (file, enc, cb) {
  643. this.emit("error", new gutil.PluginError("testPlugin", "test"));
  644. cb();
  645. }))
  646. .on('error', onError);
  647. });
  648. });
  649. });