Skip to content

Commit

Permalink
Fix: Avoid setting a position in custom write stream (fixes #202) (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
doowb authored and phated committed Aug 15, 2016
1 parent e8dc007 commit 7b2a84c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,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);
Expand Down
28 changes: 28 additions & 0 deletions test/dest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand All @@ -33,10 +34,13 @@ var inputBase = testConstants.inputBase;
var outputBase = testConstants.outputBase;
var inputPath = testConstants.inputPath;
var outputPath = testConstants.outputPath;
var inputPathLarge = testConstants.inputPathLarge;
var outputPathLarge = testConstants.outputPathLarge;
var outputRenamePath = testConstants.outputRenamePath;
var inputDirpath = testConstants.inputDirpath;
var outputDirpath = testConstants.outputDirpath;
var contents = testConstants.contents;
var contentsLarge = testConstants.contentsLarge;

var clean = cleanup([outputBase]);

Expand Down Expand Up @@ -304,6 +308,30 @@ describe('.dest()', function() {
], done);
});

it('writes large streaming files to the right folder', function(done) {
var file = new File({
base: inputBase,
path: inputPathLarge,
contents: string(contentsLarge),
});

function assert(files) {
var outputContents = fs.readFileSync(outputPathLarge, 'utf8');

expect(files.length).toEqual(1);
expect(files).toInclude(file);
expect(files[0].base).toEqual(outputBase, 'base should have changed');
expect(files[0].path).toEqual(outputPathLarge, 'path should have changed');
expect(outputContents).toEqual(contentsLarge);
};

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,
Expand Down
18 changes: 18 additions & 0 deletions test/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,13 +36,17 @@ 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;
var outputPathLarge = testConstants.outputPathLarge;
var outputDirpath = testConstants.outputDirpath;
var outputNestedPath = testConstants.outputNestedPath;
var outputNestedDirpath = testConstants.outputNestedDirpath;
var contents = testConstants.contents;
var contentsLarge = testConstants.contentsLarge;

var clean = cleanup([outputBase]);

Expand Down Expand Up @@ -1453,6 +1458,19 @@ describe('createWriteStream', function() {
], assert);
});

it('accepts just a file path and writes a large file to it', function(done) {
function assert(err) {
var outputContents = fs.readFileSync(outputPathLarge, 'utf8');
expect(outputContents).toEqual(contentsLarge);
done(err);
}

pipe([
string(contentsLarge),
createWriteStream(outputPathLarge),
], assert);
});

it('accepts flag option', function(done) {
// Write 12 stars then 12345 because the length of expected is 12
fs.writeFileSync(outputPath, '************12345');
Expand Down
11 changes: 11 additions & 0 deletions test/utils/test-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var outputBase = path.join(__dirname, '..', outputRelative);
// Used for file tests
var inputPath = path.join(inputBase, './test.txt');
var outputPath = path.join(outputBase, './test.txt');
var inputPathLarge = path.join(inputBase, './test-large.txt');
var outputPathLarge = path.join(outputBase, './test-large.txt');
// Used for directory tests
var inputDirpath = path.join(inputBase, './foo');
var outputDirpath = path.join(outputBase, './foo');
Expand All @@ -37,6 +39,12 @@ var symlinkNestedFirst = path.join(outputBase, './test-multi-layer-symlink');
var symlinkNestedSecond = path.join(outputBase, './foo/baz-link.txt');
// Used for contents of files
var contents = 'Hello World!';
var contentsLarge = '';
var idx = 0;
while (idx++ < 2000) {
contentsLarge += '[' + idx + '] Hello World!\n';
}


module.exports = {
inputRelative: inputRelative,
Expand All @@ -45,6 +53,8 @@ module.exports = {
outputBase: outputBase,
inputPath: inputPath,
outputPath: outputPath,
inputPathLarge: inputPathLarge,
outputPathLarge: outputPathLarge,
inputDirpath: inputDirpath,
outputDirpath: outputDirpath,
inputNestedPath: inputNestedPath,
Expand All @@ -63,4 +73,5 @@ module.exports = {
symlinkNestedFirst: symlinkNestedFirst,
symlinkNestedSecond: symlinkNestedSecond,
contents: contents,
contentsLarge: contentsLarge,
};
15 changes: 15 additions & 0 deletions test/utils/test-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@ var miss = require('mississippi');
var expect = require('expect');

var to = miss.to;
var from = miss.from;
var through = miss.through;

function string(str) {
return from(function(size, next) {
if (str.length <= 0) {
next(null, null);
return;
}

var chunk = str.slice(0, size);
str = str.slice(size);
next(null, chunk);
});
}

function rename(filepath) {
return through.obj(function(file, enc, cb) {
file.path = filepath;
Expand Down Expand Up @@ -46,6 +60,7 @@ function slowCount(value) {
}

module.exports = {
string: string,
rename: rename,
includes: includes,
count: count,
Expand Down

0 comments on commit 7b2a84c

Please sign in to comment.