From 176ab76b07310a20367a75f4b9846d0990fcaf3d Mon Sep 17 00:00:00 2001 From: Brian Woodward Date: Mon, 15 Aug 2016 17:26:42 -0400 Subject: [PATCH] Fix: Avoid setting a position in custom write stream (fixes #202) (#203) --- lib/file-operations.js | 3 +-- test/dest.js | 25 +++++++++++++++++++++++++ test/file-operations.js | 19 +++++++++++++++++++ test/utils/test-streams.js | 22 ++++++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/lib/file-operations.js b/lib/file-operations.js index 3ec563af..e8f5adcd 100644 --- a/lib/file-operations.js +++ b/lib/file-operations.js @@ -349,7 +349,6 @@ function WriteStream(path, options, flush) { this.mode = options.mode || constants.DEFAULT_FILE_MODE; this.flag = options.flag || 'w'; - this.pos = APPEND_MODE_REGEXP.test(this.flag) ? null : 0;; // Used by node's `fs.WriteStream` this.fd = null; @@ -398,7 +397,7 @@ function worker(data, encoding, callback) { return this.once('open', onOpen); } - fs.write(this.fd, data, 0, data.length, this.pos, onWrite); + fs.write(this.fd, data, 0, data.length, null, onWrite); function onOpen() { self._write(data, encoding, callback); diff --git a/test/dest.js b/test/dest.js index d0802f69..fc03aef1 100644 --- a/test/dest.js +++ b/test/dest.js @@ -25,6 +25,7 @@ var count = testStreams.count; var rename = testStreams.rename; var includes = testStreams.includes; var slowCount = testStreams.slowCount; +var string = testStreams.string; function noop() {} @@ -304,6 +305,30 @@ describe('.dest()', function() { ], done); }); + it('writes large streaming files to the right folder', function(done) { + var size = 40000; + + var file = new File({ + base: inputBase, + path: inputPath, + contents: string(size), + }); + + function assert(files) { + var stats = fs.lstatSync(outputPath); + + expect(files.length).toEqual(1); + expect(files).toInclude(file); + expect(stats.size).toEqual(size); + }; + + pipe([ + from.obj([file]), + vfs.dest(outputBase), + concat(assert), + ], done); + }); + it('writes directories to the right folder', function(done) { var file = new File({ base: inputBase, diff --git a/test/file-operations.js b/test/file-operations.js index d4d69f86..e448b802 100644 --- a/test/file-operations.js +++ b/test/file-operations.js @@ -19,6 +19,7 @@ var statMode = require('./utils/stat-mode'); var mockError = require('./utils/mock-error'); var isWindows = require('./utils/is-windows'); var applyUmask = require('./utils/apply-umask'); +var testStreams = require('./utils/test-streams'); var testConstants = require('./utils/test-constants'); var mkdirp = fo.mkdirp; @@ -35,6 +36,8 @@ var createWriteStream = fo.createWriteStream; var pipe = miss.pipe; var from = miss.from; +var string = testStreams.string; + var outputBase = testConstants.outputBase; var inputPath = testConstants.inputPath; var outputPath = testConstants.outputPath; @@ -1453,6 +1456,22 @@ describe('createWriteStream', function() { ], assert); }); + it('accepts just a file path and writes a large file to it', function(done) { + var size = 40000; + + function assert(err) { + var stats = fs.lstatSync(outputPath); + + expect(stats.size).toEqual(size); + done(err); + } + + pipe([ + string(size), + createWriteStream(outputPath), + ], assert); + }); + it('accepts flag option', function(done) { // Write 12 stars then 12345 because the length of expected is 12 fs.writeFileSync(outputPath, '************12345'); diff --git a/test/utils/test-streams.js b/test/utils/test-streams.js index 0c68c2e9..9c84ce9e 100644 --- a/test/utils/test-streams.js +++ b/test/utils/test-streams.js @@ -4,8 +4,29 @@ var miss = require('mississippi'); var expect = require('expect'); var to = miss.to; +var from = miss.from; var through = miss.through; +function string(length) { + return from(function(size, next) { + if (length <= 0) { + next(null, null); + return; + } + + var chunkSize = size <= length ? size : length; + + length -= size; + + var chunk = ''; + for (var x = 0; x < chunkSize; x++) { + chunk += 'a'; + } + + next(null, chunk); + }); +} + function rename(filepath) { return through.obj(function(file, enc, cb) { file.path = filepath; @@ -46,6 +67,7 @@ function slowCount(value) { } module.exports = { + string: string, rename: rename, includes: includes, count: count,