|
| 1 | +var fs = require('fs'); |
| 2 | +var File = require('vinyl'); |
| 3 | +var buffer = require('..'); |
| 4 | +var through = require('through2'); |
| 5 | +var assert = require('assert'); |
| 6 | +var Stream = require('stream'); |
| 7 | + |
| 8 | +describe('gulp-buffer', function() { |
| 9 | + |
| 10 | + describe('with null contents', function() { |
| 11 | + it('should let null files pass through', function(done) { |
| 12 | + var b = buffer(); |
| 13 | + var n = 0; |
| 14 | + var _transform = function(file, enc, callback) { |
| 15 | + assert.equal(file.contents, null); |
| 16 | + n++; |
| 17 | + callback(); |
| 18 | + } |
| 19 | + var _flush = function(callback) { |
| 20 | + assert.equal(n, 1); |
| 21 | + done(); |
| 22 | + callback(); |
| 23 | + } |
| 24 | + var t = through.obj(_transform, _flush); |
| 25 | + b.pipe(t); |
| 26 | + b.end(new File({ |
| 27 | + contents: null |
| 28 | + })); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('with buffer contents', function() { |
| 33 | + it('should let buffer files pass through', function(done) { |
| 34 | + var b = buffer(); |
| 35 | + var n = 0; |
| 36 | + var c = new Buffer("data"); |
| 37 | + var _transform = function(file, enc, callback) { |
| 38 | + assert.equal(file.contents, c); |
| 39 | + n++; |
| 40 | + callback(); |
| 41 | + } |
| 42 | + var _flush = function(callback) { |
| 43 | + assert.equal(n, 1); |
| 44 | + done(); |
| 45 | + callback(); |
| 46 | + } |
| 47 | + var t = through.obj(_transform, _flush); |
| 48 | + b.pipe(t); |
| 49 | + b.end(new File({ |
| 50 | + contents: c |
| 51 | + })); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('with stream contents', function() { |
| 56 | + it('should transform stream to buffer', function(done) { |
| 57 | + var b = buffer(); |
| 58 | + var n = 0; |
| 59 | + var c = Readable(); |
| 60 | + var _transform = function(file, enc, callback) { |
| 61 | + assert.equal(file.isBuffer(), true); |
| 62 | + assert.equal(file.contents.toString(), "data"); |
| 63 | + n++; |
| 64 | + callback(); |
| 65 | + } |
| 66 | + var _flush = function(callback) { |
| 67 | + assert.equal(n, 1); |
| 68 | + done(); |
| 69 | + callback(); |
| 70 | + } |
| 71 | + var t = through.obj(_transform, _flush); |
| 72 | + b.pipe(t); |
| 73 | + b.end(new File({ |
| 74 | + contents: c |
| 75 | + })); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + |
| 80 | +}); |
| 81 | + |
| 82 | +function Readable(cb){ |
| 83 | + var readable = new Stream.Readable(); |
| 84 | + readable._read = function() { |
| 85 | + this.push(new Buffer('data')); |
| 86 | + this.push(null); // no more data |
| 87 | + }; |
| 88 | + if (cb) readable.on('end', cb); |
| 89 | + return readable; |
| 90 | +} |
| 91 | + |
| 92 | + |
0 commit comments