123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771 |
- /*global describe, it*/
- "use strict";
- var gulp = require('gulp'),
- should = require('should'),
- through = require('through2'),
- plumber = require('gulp-plumber'),
- PluginError = require('plugin-error'),
- Vinyl = require('vinyl'),
- join = require('path').join,
- fs = require('fs'),
- notify = require('../');
- var mockGenerator = function (tester) {
- tester = tester || function () { };
- return function (opts, callback) {
- tester(opts);
- callback();
- };
- };
- var originalLogger = notify.logger();
- describe('gulp output stream', function() {
- beforeEach(function () {
- notify.logLevel(0);
- notify.logger(originalLogger);
- });
- describe('notify()', function() {
- it('should return a stream', function(done) {
- var stream = notify({
- notifier: mockGenerator()
- });
- should.exist(stream);
- should.exist(stream.on);
- should.exist(stream.pipe);
- done();
- });
- it('should allow setting of own reporter', function(done) {
- var notifier = notify.withReporter(mockGenerator);
- var stream = notifier();
- should.exist(stream);
- should.exist(stream.on);
- should.exist(stream.pipe);
- done();
- });
- it('should set message, title and have default icon of Gulp logo', function(done) {
- var testString = "this is a test";
- var expectedIcon = join(__dirname, '..', 'assets', 'gulp.png');
- var expectedFile = new Vinyl({
- path: "test/fixtures/1.txt",
- cwd: "test/",
- base: "test/fixtures/",
- contents: fs.createReadStream("test/fixtures/1.txt")
- });
- var mockedNotify = notify.withReporter(mockGenerator(function (opts) {
- opts.should.have.property('title');
- opts.should.have.property('message');
- opts.should.have.property('icon');
- String(opts.icon).should.equal(expectedIcon);
- String(opts.message).should.equal(testString);
- done();
- }.bind(this)));
- var outstream = mockedNotify({
- message: testString
- });
- outstream.write(expectedFile);
- });
- it('should be able to override default icon', function(done) {
- var testString = "this is a test";
- var expectedIcon = "testIcon";
- var expectedFile = new Vinyl({
- path: "test/fixtures/1.txt",
- cwd: "test/",
- base: "test/fixtures/",
- contents: fs.createReadStream("test/fixtures/1.txt")
- });
- var mockedNotify = notify.withReporter(mockGenerator(function (opts) {
- opts.should.have.property('title');
- opts.should.have.property('message');
- opts.should.have.property('icon');
- String(opts.icon).should.equal(expectedIcon);
- String(opts.message).should.equal(testString);
- done();
- }.bind(this)));
- var outstream = mockedNotify({
- message: testString,
- icon: expectedIcon
- });
- outstream.write(expectedFile);
- });
- it('should call notifier with extra options untouched', function(done) {
- var testString = "this is a test";
- var testIcon = "face-cool";
- var mockedNotify = notify.withReporter(mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.icon);
- should.exist(opts.message);
- String(opts.icon).should.equal(testIcon);
- String(opts.message).should.equal(testString);
- }));
- var instream = gulp.src(join(__dirname, "./fixtures/*.txt")),
- outstream = mockedNotify({
- message: testString,
- icon: testIcon
- });
- outstream.on('data', function(file) {
- should.exist(file);
- should.exist(file.path);
- should.exist(file.contents);
- });
- outstream.on('end', function() {
- done();
- });
- instream.pipe(outstream);
- });
- it('should emit error when sub-module returns error and emitError is true', function(done) {
- var mockedNotify = notify.withReporter(function (options, callback) {
- callback(new Error(testString));
- });
- var testString = "testString",
- instream = gulp.src(join(__dirname, "./fixtures/*.txt")),
- outstream = mockedNotify({
- message: testString,
- emitError: true
- });
- outstream.on('error', function (error) {
- should.exist(error);
- should.exist(error.message);
- String(error.message).should.equal(testString);
- done();
- });
- instream.pipe(outstream);
- });
- it('should pass on files', function(done) {
- var mockedNotify = notify.withReporter(mockGenerator());
- var
- testSuffix = "tester",
- srcFile = join(__dirname, "./fixtures/*"),
- instream = gulp.src(srcFile),
- outstream = mockedNotify();
- var numFilesBefore = 0,
- numFilesAfter = 0;
- instream
- .pipe(through.obj(function (file, enc, cb) {
- numFilesBefore++;
- this.push(file);
- cb();
- }, function (cb) {
- numFilesBefore.should.equal(3);
- cb();
- }))
- .pipe(outstream)
- .pipe(through.obj(function (file, enc, cb) {
- numFilesAfter++;
- this.push(file);
- cb();
- }, function (callback) {
- numFilesAfter.should.equal(3);
- done();
- }));
- });
- it('should pass on files even on error in notifier (with use of plumber)', function(done) {
- var
- testString = "tester",
- srcFile = join(__dirname, "./fixtures/*"),
- instream = gulp.src(srcFile),
- outstream = notify({
- notifier: function (options, callback) {
- callback(new Error(testString));
- }
- });
- var numFilesBefore = 0,
- numFilesAfter = 0;
- instream
- .pipe(plumber())
- .pipe(through.obj(function (file, enc, cb) {
- numFilesBefore++;
- this.push(file);
- cb();
- }, function (cb) {
- numFilesBefore.should.equal(3);
- cb();
- }))
- .pipe(outstream)
- .on('error', function (error) {
- error.message.should.equal(testString);
- })
- .pipe(through.obj(function (file, enc, cb) {
- numFilesAfter++;
- this.push(file);
- cb();
- }, function (callback) {
- numFilesAfter.should.equal(3);
- done();
- }));
- });
- it('should emit error when sub-module throws exception/error and emitError flag is true', function(done) {
- var testString = "some exception",
- instream = gulp.src(join(__dirname, "./fixtures/*.txt")),
- outstream = notify({
- message: testString,
- notifier: function (options, callback) {
- throw new Error(testString);
- },
- emitError: true
- });
- outstream.on('error', function (error) {
- should.exist(error);
- should.exist(error.message);
- String(error.message).should.equal(testString);
- done();
- });
- instream.pipe(outstream);
- });
- it('should not emit error when sub-module throws exception/error if emitError flag is false (default)', function(done) {
- notify.logLevel(1);
- notify.logger(function () {
- should.exist(true);
- done();
- });
- var testString = "some exception",
- expectedFile = new Vinyl({
- path: "test/fixtures/1.txt",
- cwd: "test/",
- base: "test/fixtures/",
- contents: fs.createReadStream("test/fixtures/1.txt")
- }),
- outstream = notify({
- message: testString,
- notifier: function (options, callback) {
- throw new Error(testString);
- }
- });
- outstream.on('error', function (error) {
- should.not.exist(error);
- should.not.exist(error.message);
- String(error.message).should.not.equal(testString);
- done();
- });
- outstream.on('end', function () {
- done();
- });
- outstream.write(expectedFile);
- outstream.write(null);
- outstream.end();
- });
- it('should default to notifying file path and default title', function(done) {
- var srcFile = join(__dirname, "./fixtures/1.txt");
- var instream = gulp.src(srcFile),
- outstream = notify({
- notifier: mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- String(opts.message).should.equal(srcFile);
- String(opts.title).should.equal("Gulp notification");
- })
- });
- outstream.on('data', function(file) {
- should.exist(file);
- should.exist(file.path);
- should.exist(file.contents);
- });
- outstream.on('end', function() {
- done();
- });
- instream.pipe(outstream);
- });
- it('should take function with file as argument, as message or title', function(done) {
- var
- testSuffix = "tester",
- srcFile = join(__dirname, "./fixtures/1.txt"),
- instream = gulp.src(srcFile),
- outstream = notify({
- notifier: mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- String(opts.message).should.equal(srcFile + testSuffix);
- String(opts.title).should.equal(srcFile + testSuffix);
- }),
- message: function (file) {
- String(file.path).should.equal(srcFile);
- return file.path + testSuffix;
- },
- title: function (file) {
- String(file.path).should.equal(srcFile);
- return file.path + testSuffix;
- }
- });
- outstream.on('data', function(file) {
- should.exist(file);
- should.exist(file.path);
- should.exist(file.contents);
- });
- outstream.on('end', function() {
- done();
- });
- instream.pipe(outstream);
- });
- it('should notify on all files per default', function(done) {
- var
- testSuffix = "tester",
- srcFile = join(__dirname, "./fixtures/*"),
- instream = gulp.src(srcFile),
- numFunctionCalls = 0,
- outstream = notify({
- notifier: mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- numFunctionCalls++;
- })
- });
- outstream.on('data', function(file) {
- should.exist(file);
- should.exist(file.path);
- should.exist(file.contents);
- });
- outstream.on('end', function() {
- numFunctionCalls.should.equal(3);
- done();
- });
- instream.pipe(outstream)
- });
- it('should handle streamed files', function (done) {
- var expectedFile = new Vinyl({
- path: "test/fixtures/1.txt",
- cwd: "test/",
- base: "test/fixtures/",
- contents: fs.createReadStream("test/fixtures/1.txt")
- });
- var testString = "testString";
- var outstream = notify({
- message: testString,
- notifier: mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- String(opts.message).should.equal(testString);
- })
- });
- outstream.on('error', function (err) {
- should.not.exist(err);
- });
- outstream.on('data', function(file) {
- should.exist(file);
- should.exist(file.isStream());
- should.exist(file.path);
- should.exist(file.contents);
- done();
- });
- outstream.write(expectedFile);
- });
- it('should support lodash template for titles and messages', function (done) {
- var expectedFile = new Vinyl({
- path: "test/fixtures/1.txt",
- cwd: "test/",
- base: "test/fixtures/",
- contents: fs.createReadStream("test/fixtures/1.txt")
- });
- var testString = "Template: <%= file.relative %>";
- var expectedString = "Template: 1.txt";
- var outstream = notify({
- message: testString,
- title: testString,
- notifier: mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- String(opts.message).should.equal(expectedString);
- String(opts.title).should.equal(expectedString);
- })
- });
- outstream.on('error', function (err) {
- should.not.exist(err);
- });
- outstream.on('data', function(file) {
- should.exist(file);
- should.exist(file.path);
- should.exist(file.contents);
- done();
- });
- outstream.write(expectedFile);
- });
- });
- describe('notify.onLast', function() {
- it('should only notify on the last file, if onLast flag is activated', function(done) {
- var
- testSuffix = "tester",
- srcFile = join(__dirname, "./fixtures/*"),
- instream = gulp.src(srcFile),
- numFunctionCalls = 0,
- outstream = notify({
- onLast: true,
- notifier: mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- numFunctionCalls++;
- })
- });
- outstream.on('data', function(file) {
- should.exist(file);
- should.exist(file.path);
- should.exist(file.contents);
- });
- outstream.on('end', function() {
- numFunctionCalls.should.equal(1);
- done();
- });
- instream.pipe(outstream);
- });
- it('should stream all files even if onLast is activated', function(done) {
- var
- testSuffix = "tester",
- srcFile = join(__dirname, "./fixtures/*"),
- instream = gulp.src(srcFile),
- outstream = notify({
- onLast: true,
- notifier: mockGenerator()
- });
- var numFilesBefore = 0,
- numFilesAfter = 0;
- instream
- .pipe(through.obj(function (file, enc, cb) {
- numFilesBefore++;
- this.push(file);
- cb();
- }, function (cb) {
- numFilesBefore.should.equal(3);
- cb();
- }))
- .pipe(outstream)
- .pipe(through.obj(function (file, enc, cb) {
- numFilesAfter++;
- this.push(file);
- cb();
- }, function (callback) {
- numFilesAfter.should.equal(3);
- done();
- }));
- });
- it('should support lodash template for titles and messages when onLast', function (done) {
- var
- testSuffix = "tester",
- srcFile = join(__dirname, "./fixtures/*"),
- instream = gulp.src(srcFile),
- numFunctionCalls = 0,
- outstream = notify({
- onLast: true,
- message: 'Template: <%= file.relative %>',
- notifier: mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- opts.message.should.startWith('Template:')
- opts.message.should.endWith('.txt')
- numFunctionCalls++;
- })
- });
- outstream.on('data', function(file) {
- should.exist(file);
- should.exist(file.path);
- should.exist(file.contents);
- });
- outstream.on('end', function() {
- numFunctionCalls.should.equal(1);
- done();
- });
- instream.pipe(outstream);
- });
- });
- describe('notify.logger()', function() {
- it('should allow setting of logLevel', function(done) {
- var notifier = notify.withReporter(mockGenerator);
- should.exist(notify.logLevel);
- should.exist(notify.logger);
- done();
- });
- it('should log error on log level 1', function(done) {
- var srcFile = join(__dirname, "./fixtures/1.txt");
- notify.logLevel(1);
- notify.logger(function (options) {
- should.exist(true);
- done();
- });
- var notifier = notify.withReporter(mockGenerator);
- gulp.src(srcFile)
- .pipe(through.obj(function (file, enc, cb) {
- this.emit("error", new PluginError("testPlugin", "foo"));
- cb();
- }))
- .on("error", notifier.onError())
- });
- it('should not log on log level 0', function(done) {
- var srcFile = join(__dirname, "./fixtures/1.txt");
- var hasBeenCalled = false;
- notify.logLevel(0);
- notify.logger(function () {
- hasBeenCalled = true;
- should.not.exist(arguments);
- });
- var notifier = notify.withReporter(mockGenerator(function () {
- should(hasBeenCalled).equal(false);
- done();
- }));
- gulp.src(srcFile)
- .pipe(through.obj(function (file, enc, cb) {
- this.emit("error", new PluginError("testPlugin", "foo"));
- cb();
- }))
- .on("error", notifier.onError())
- });
- });
- describe('notify.onError()', function() {
- it('should have defined onError function on object', function (done) {
- should.exist(notify.onError);
- done();
- });
- it('should have onError event withReporter', function(done) {
- var notifier = notify.withReporter(mockGenerator);
- should.exist(notifier.onError);
- done();
- });
- it('should call end on stream', function (done) {
- var onError = notify.onError({
- notifier: mockGenerator(function (opts) { })
- });
- var stream = through.obj(function (file, enc, cb) {
- this.emit('error', 'error');
- cb();
- });
- stream.on('error', onError).on('end', done);
- stream.write({});
- });
- it('should be limited by notifying on error if th onError-option is passed', function (done) {
- var
- testMessage = "tester",
- srcFile = join(__dirname, "./fixtures/*"),
- onError = notify.onError({
- notifier: mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- String(opts.message).should.equal(testMessage);
- String(opts.title).should.equal("Error running Gulp");
- done();
- })
- });
- gulp.src(srcFile)
- .pipe(through.obj(function (file, enc, cb) {
- this.emit("error", new PluginError("testPlugin", testMessage));
- cb();
- }))
- .on("error", onError)
- });
- it('should have onError options on withReporter sugar', function (done) {
- var
- testMessage = "tester",
- srcFile = join(__dirname, "./fixtures/*");
- var custom = notify.withReporter(mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- String(opts.message).should.equal(testMessage);
- String(opts.title).should.equal("Error running Gulp");
- done();
- }));
- gulp.src(srcFile)
- .pipe(through.obj(function (file, enc, cb) {
- this.emit("error", new PluginError("testPlugin", testMessage));
- cb();
- }))
- .on("error", custom.onError())
- });
- it('should have default Gulp-error logo onError', function (done) {
- var expectedIcon = join(__dirname, '..', 'assets', 'gulp-error.png');
- var
- testMessage = "tester",
- srcFile = join(__dirname, "./fixtures/*");
- var custom = notify.withReporter(mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- should.exist(opts.icon);
- String(opts.message).should.equal(testMessage);
- String(opts.icon).should.equal(expectedIcon);
- String(opts.title).should.equal("Error running Gulp");
- done();
- }));
- gulp.src(srcFile)
- .pipe(through.obj(function (file, enc, cb) {
- this.emit("error", new PluginError("testPlugin", testMessage));
- cb();
- }))
- .on("error", custom.onError())
- });
- it('should be able to override default icon onError', function (done) {
- var expectedIcon = "testIcon";
- var testMessage = "tester";
- var custom = notify.withReporter(mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- should.exist(opts.icon);
- String(opts.message).should.equal(testMessage);
- String(opts.icon).should.equal(expectedIcon);
- String(opts.title).should.equal("Error running Gulp");
- done();
- }));
- custom.onError({
- icon: expectedIcon,
- })(new PluginError("testPlugin", testMessage));
- });
- it('should have Frog sound per default onError', function (done) {
- var expectedIcon = "testIcon";
- var testMessage = "tester";
- var custom = notify.withReporter(mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.sound);
- String(opts.sound).should.equal('Frog');
- done();
- }));
- custom.onError({
- icon: expectedIcon,
- })(new PluginError("testPlugin", testMessage));
- });
- it('should support lodash template for titles and messages on onError', function (done) {
- var testString = "Template: <%= error.message %>";
- var expectedString = "Template: test";
- var srcFile = join(__dirname, "./fixtures/*");
- var onError = notify.onError({
- message: testString,
- title: testString,
- notifier: mockGenerator(function (opts) {
- should.exist(opts);
- should.exist(opts.title);
- should.exist(opts.message);
- String(opts.message).should.equal(expectedString);
- String(opts.title).should.equal(expectedString);
- done();
- })
- });
- gulp.src(srcFile)
- .pipe(through.obj(function (file, enc, cb) {
- this.emit("error", new PluginError("testPlugin", "test"));
- cb();
- }))
- .on('error', onError);
- });
- });
- });
|