Skip to content
Permalink
Browse files

test: refactor test-fs-write-sync

Refactor the code for the test-fs-write-sync to avoid code repetition
and to make it simpler.

PR-URL: #28371
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information...
gabynr authored and Trott committed Jun 21, 2019
1 parent b06ce0b commit 327c6734cba332c2ac19cf0cdedaca1b918e7010
Showing with 17 additions and 39 deletions.
  1. +17 −39 test/parallel/test-fs-write-sync.js
@@ -29,50 +29,28 @@ const filename = path.join(tmpdir.path, 'write.txt');

tmpdir.refresh();

// fs.writeSync with all parameters provided:
{
const fd = fs.openSync(filename, 'w');
const parameters = [Buffer.from('bár'), 0, Buffer.byteLength('bár')];

let written = fs.writeSync(fd, '');
assert.strictEqual(written, 0);
// The first time fs.writeSync is called with all parameters provided.
// After that, each pop in the cycle removes the final parameter. So:
// - The 2nd time fs.writeSync with a buffer, without the length parameter.
// - The 3rd time fs.writeSync with a buffer, without the offset and length
// parameters.
while (parameters.length > 0) {
const fd = fs.openSync(filename, 'w');

fs.writeSync(fd, 'foo');
let written = fs.writeSync(fd, '');
assert.strictEqual(written, 0);

written = fs.writeSync(fd, Buffer.from('bár'), 0, Buffer.byteLength('bár'));
assert.ok(written > 3);
fs.closeSync(fd);
fs.writeSync(fd, 'foo');

assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
}

// fs.writeSync with a buffer, without the length parameter:
{
const fd = fs.openSync(filename, 'w');

let written = fs.writeSync(fd, '');
assert.strictEqual(written, 0);

fs.writeSync(fd, 'foo');

written = fs.writeSync(fd, Buffer.from('bár'), 0);
assert.ok(written > 3);
fs.closeSync(fd);

assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
}

// fs.writeSync with a buffer, without the offset and length parameters:
{
const fd = fs.openSync(filename, 'w');

let written = fs.writeSync(fd, '');
assert.strictEqual(written, 0);

fs.writeSync(fd, 'foo');
written = fs.writeSync(fd, ...parameters);
assert.ok(written > 3);
fs.closeSync(fd);

written = fs.writeSync(fd, Buffer.from('bár'));
assert.ok(written > 3);
fs.closeSync(fd);
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');

assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
parameters.pop();
}
}

0 comments on commit 327c673

Please sign in to comment.
You can’t perform that action at this time.