diff --git a/index.js b/index.js index 97f5c75..928dade 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var isStream = require('./lib/isStream'); var isNull = require('./lib/isNull'); var inspectStream = require('./lib/inspectStream'); var cloneBuffer = require('./lib/cloneBuffer'); +var Stream = require('stream'); function File(file) { if (!file) file = {}; @@ -43,8 +44,14 @@ File.prototype.isDirectory = function() { }; File.prototype.clone = function() { - var clonedContents = this.isBuffer() ? cloneBuffer(this.contents) : this.contents; var clonedStat = this.stat ? cloneStats(this.stat) : null; + var clonedContents = null; + if(this.isBuffer()) { + clonedContents = cloneBuffer(this.contents); + } else if(this.isStream()) { + clonedContents = this.contents.pipe(new Stream.PassThrough()); + this.contents = this.contents.pipe(new Stream.PassThrough()); + } return new File({ cwd: this.cwd, diff --git a/test/File.js b/test/File.js index 1f2a83a..13b90d7 100644 --- a/test/File.js +++ b/test/File.js @@ -1,6 +1,7 @@ var File = require('../'); var Stream = require('stream'); var fs = require('fs'); +var es = require('event-stream'); var should = require('should'); require('mocha'); @@ -196,20 +197,64 @@ describe('File', function() { }); it('should copy all attributes over with Stream', function(done) { + var contents = new Stream.PassThrough(); var options = { cwd: "/", base: "/test/", path: "/test/test.coffee", - contents: new Stream() + contents: contents }; var file = new File(options); var file2 = file.clone(); + contents.write(Buffer('wa')); + + process.nextTick(function() { + contents.write(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); + file.contents.pipe(es.wait(function(err, data) { + file2.contents.pipe(es.wait(function(err, data2) { + data2.should.equal(data, 'stream contents should be the same'); + })); + })); + file2.contents.should.not.equal(file.contents, 'stream ref should not be the same'); + done(); + }); + + it('should copy all attributes over with Stream', function(done) { + var contents = new Stream.PassThrough(); + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: contents + }; + var file = new File(options); + var file2 = file.clone(); + + contents.write(Buffer('wa')); + + process.nextTick(function() { + contents.write(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.equal(file.contents, 'stream ref should be the same'); + file2.contents.pipe(es.wait(function(err, data) { + file.contents.pipe(es.wait(function(err, data2) { + data2.should.equal(data, 'stream contents should be the same'); + })); + })); + file2.contents.should.not.equal(file.contents, 'stream ref should not be the same'); done(); });