Skip to content

Commit e2adfb7

Browse files
gabynrtargos
authored andcommitted
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 <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent d4113f9 commit e2adfb7

File tree

1 file changed

+17
-39
lines changed

1 file changed

+17
-39
lines changed

test/parallel/test-fs-write-sync.js

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,50 +29,28 @@ const filename = path.join(tmpdir.path, 'write.txt');
2929

3030
tmpdir.refresh();
3131

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

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

39-
fs.writeSync(fd, 'foo');
43+
let written = fs.writeSync(fd, '');
44+
assert.strictEqual(written, 0);
4045

41-
written = fs.writeSync(fd, Buffer.from('bár'), 0, Buffer.byteLength('bár'));
42-
assert.ok(written > 3);
43-
fs.closeSync(fd);
46+
fs.writeSync(fd, 'foo');
4447

45-
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
46-
}
47-
48-
// fs.writeSync with a buffer, without the length parameter:
49-
{
50-
const fd = fs.openSync(filename, 'w');
51-
52-
let written = fs.writeSync(fd, '');
53-
assert.strictEqual(written, 0);
54-
55-
fs.writeSync(fd, 'foo');
56-
57-
written = fs.writeSync(fd, Buffer.from('bár'), 0);
58-
assert.ok(written > 3);
59-
fs.closeSync(fd);
60-
61-
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
62-
}
63-
64-
// fs.writeSync with a buffer, without the offset and length parameters:
65-
{
66-
const fd = fs.openSync(filename, 'w');
67-
68-
let written = fs.writeSync(fd, '');
69-
assert.strictEqual(written, 0);
70-
71-
fs.writeSync(fd, 'foo');
48+
written = fs.writeSync(fd, ...parameters);
49+
assert.ok(written > 3);
50+
fs.closeSync(fd);
7251

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

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

0 commit comments

Comments
 (0)