Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor test-fs-write-sync #28371

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}