Skip to content

Commit

Permalink
avoid reassigning the original contents to a through stream
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Apr 26, 2016
1 parent 896f68d commit e2a9104
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ File.prototype.clone = function(opt) {
var contents;
if (this.isStream()) {
contents = this.contents.pipe(new Stream.PassThrough());
this.contents = this.contents.pipe(new Stream.PassThrough());
} else if (this.isBuffer()) {
contents = opt.contents ? cloneBuffer(this.contents) : this.contents;
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
"replace-ext": "0.0.1"
},
"devDependencies": {
"async": "^2.0.0-rc.3",
"buffer-equal": "0.0.1",
"eslint": "^1.7.3",
"eslint-config-gulp": "^2.0.0",
"event-stream": "^3.1.0",
"github-changes": "^1.0.1",
"istanbul": "^0.3.0",
"istanbul-coveralls": "^1.0.1",
"jscs": "^2.3.5",
"jscs-preset-gulp": "^1.0.0",
"lodash.templatesettings": "^3.1.0",
"mississippi": "^1.2.0",
"mocha": "^2.0.0",
"rimraf": "^2.2.5",
"should": "^7.0.0"
Expand Down
58 changes: 47 additions & 11 deletions test/File.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
var Stream = require('stream');
var fs = require('fs');
var path = require('path');
var es = require('event-stream');
var from = require('mississippi').from;
var to = require('mississippi').to;
var finished = require('mississippi').finished;
var through = require('mississippi').through;
var async = require('async');
var File = require('../');

var should = require('should');
require('mocha');

function allFinished() {
var cb = Array.prototype.pop.call(arguments);
async.map(arguments, finished, cb);
}

function streamNoop(data, enc, cb) {
cb();
}

describe('File', function() {
describe('isVinyl()', function() {
it('should return true on a vinyl object', function(done) {
Expand Down Expand Up @@ -249,7 +262,8 @@ describe('File', function() {
});

it('should copy all attributes over with Stream', function(done) {
var contents = new Stream.PassThrough();
var input = ['test'];
var contents = from(input);
var options = {
cwd: '/',
base: '/test/',
Expand All @@ -259,27 +273,49 @@ describe('File', function() {
var file = new File(options);
var file2 = file.clone();

contents.write(new Buffer('wa'));

process.nextTick(function() {
contents.write(new Buffer('dup'));
contents.end();
});

file2.should.not.equal(file, 'refs should be different');
file2.cwd.should.equal(file.cwd);
file2.base.should.equal(file.base);
file2.path.should.equal(file.path);
file2.contents.should.not.equal(file.contents, 'stream ref should not be the same');
file.contents.pipe(es.wait(function(err, data) {
file2.contents.pipe(es.wait(function(err, data2) {
file.contents.pipe(through(function(err, data) {
file2.contents.pipe(through(function(err, data2) {
data2.should.not.equal(data, 'stream contents ref should not be the same');
data2.should.eql(data, 'stream contents should be the same');
}));
}));
done();
});

it('should allows a cloned file with streaming content to be drained', function(done) {
var input = [];
// Makes an array that represents a file with size ~234K
for (var x = 0; x <= 20000; x++) {
input.push('hello world\n');
}

var contents = from(input);

var options = {
cwd: '/',
base: '/test/',
path: '/test/test.coffee',
contents: contents,
};
var file = new File(options);
var file2 = file.clone();

file2.should.not.equal(file, 'refs should be different');
file2.cwd.should.equal(file.cwd);
file2.base.should.equal(file.base);
file2.path.should.equal(file.path);
file2.contents.should.not.equal(file.contents, 'stream ref should not be the same');

file2.contents.pipe(to(streamNoop));

allFinished(file.contents, file2.contents, done);
});

it('should copy all attributes over with null', function(done) {
var options = {
cwd: '/',
Expand Down

0 comments on commit e2a9104

Please sign in to comment.