Skip to content

Commit

Permalink
test: refactor test-fs-write-sync
Browse files Browse the repository at this point in the history
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 targos committed Jul 20, 2019
1 parent d4113f9 commit e2adfb7
Showing 1 changed file with 17 additions and 39 deletions.
56 changes: 17 additions & 39 deletions test/parallel/test-fs-write-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 e2adfb7

Please sign in to comment.