Skip to content

Commit a5c5786

Browse files
cjihrigMylesBorins
authored andcommitted
test: refactor test-fs-write-file-sync.js
This commit reduces shared state by introducing scopes and block scoped variables. It also makes the monkey patching used by the test more robust. PR-URL: #24834 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent a5c8af7 commit a5c5786

File tree

1 file changed

+58
-62
lines changed

1 file changed

+58
-62
lines changed

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

+58-62
Original file line numberDiff line numberDiff line change
@@ -21,87 +21,83 @@
2121

2222
'use strict';
2323
const common = require('../common');
24-
const assert = require('assert');
25-
const path = require('path');
26-
const fs = require('fs');
27-
let openCount = 0;
28-
let mode;
29-
let content;
3024

3125
if (!common.isMainThread)
3226
common.skip('process.umask is not available in Workers');
3327

34-
// Need to hijack fs.open/close to make sure that things
35-
// get closed once they're opened.
36-
fs._openSync = fs.openSync;
37-
fs.openSync = openSync;
38-
fs._closeSync = fs.closeSync;
39-
fs.closeSync = closeSync;
40-
41-
// Reset the umask for testing
42-
process.umask(0o000);
28+
const assert = require('assert');
29+
const path = require('path');
30+
const fs = require('fs');
4331

4432
// On Windows chmod is only able to manipulate read-only bit. Test if creating
4533
// the file in read-only mode works.
46-
if (common.isWindows) {
47-
mode = 0o444;
48-
} else {
49-
mode = 0o755;
50-
}
34+
const mode = common.isWindows ? 0o444 : 0o755;
35+
36+
// Reset the umask for testing
37+
process.umask(0o000);
5138

5239
const tmpdir = require('../common/tmpdir');
5340
tmpdir.refresh();
5441

5542
// Test writeFileSync
56-
const file1 = path.join(tmpdir.path, 'testWriteFileSync.txt');
57-
58-
fs.writeFileSync(file1, '123', { mode });
43+
{
44+
const file = path.join(tmpdir.path, 'testWriteFileSync.txt');
5945

60-
content = fs.readFileSync(file1, { encoding: 'utf8' });
61-
assert.strictEqual(content, '123');
62-
63-
assert.strictEqual(fs.statSync(file1).mode & 0o777, mode);
46+
fs.writeFileSync(file, '123', { mode });
47+
const content = fs.readFileSync(file, { encoding: 'utf8' });
48+
assert.strictEqual(content, '123');
49+
assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
50+
}
6451

6552
// Test appendFileSync
66-
const file2 = path.join(tmpdir.path, 'testAppendFileSync.txt');
67-
68-
fs.appendFileSync(file2, 'abc', { mode });
69-
70-
content = fs.readFileSync(file2, { encoding: 'utf8' });
71-
assert.strictEqual(content, 'abc');
72-
73-
assert.strictEqual(fs.statSync(file2).mode & mode, mode);
74-
75-
// Test writeFileSync with file descriptor
76-
const file3 = path.join(tmpdir.path, 'testWriteFileSyncFd.txt');
53+
{
54+
const file = path.join(tmpdir.path, 'testAppendFileSync.txt');
7755

78-
const fd = fs.openSync(file3, 'w+', mode);
79-
fs.writeFileSync(fd, '123');
80-
fs.closeSync(fd);
81-
82-
content = fs.readFileSync(file3, { encoding: 'utf8' });
83-
assert.strictEqual(content, '123');
84-
85-
assert.strictEqual(fs.statSync(file3).mode & 0o777, mode);
86-
87-
// Verify that all opened files were closed.
88-
assert.strictEqual(openCount, 0);
89-
90-
function openSync() {
91-
openCount++;
92-
return fs._openSync.apply(fs, arguments);
56+
fs.appendFileSync(file, 'abc', { mode });
57+
const content = fs.readFileSync(file, { encoding: 'utf8' });
58+
assert.strictEqual(content, 'abc');
59+
assert.strictEqual(fs.statSync(file).mode & mode, mode);
9360
}
9461

95-
function closeSync() {
96-
openCount--;
97-
return fs._closeSync.apply(fs, arguments);
62+
// Test writeFileSync with file descriptor
63+
{
64+
// Need to hijack fs.open/close to make sure that things
65+
// get closed once they're opened.
66+
const _openSync = fs.openSync;
67+
const _closeSync = fs.closeSync;
68+
let openCount = 0;
69+
70+
fs.openSync = (...args) => {
71+
openCount++;
72+
return _openSync(...args);
73+
};
74+
75+
fs.closeSync = (...args) => {
76+
openCount--;
77+
return _closeSync(...args);
78+
};
79+
80+
const file = path.join(tmpdir.path, 'testWriteFileSyncFd.txt');
81+
const fd = fs.openSync(file, 'w+', mode);
82+
83+
fs.writeFileSync(fd, '123');
84+
fs.closeSync(fd);
85+
const content = fs.readFileSync(file, { encoding: 'utf8' });
86+
assert.strictEqual(content, '123');
87+
assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
88+
89+
// Verify that all opened files were closed.
90+
assert.strictEqual(openCount, 0);
91+
fs.openSync = _openSync;
92+
fs.closeSync = _closeSync;
9893
}
9994

10095
// Test writeFileSync with flags
101-
const file4 = path.join(tmpdir.path, 'testWriteFileSyncFlags.txt');
102-
103-
fs.writeFileSync(file4, 'hello ', { encoding: 'utf8', flag: 'a' });
104-
fs.writeFileSync(file4, 'world!', { encoding: 'utf8', flag: 'a' });
96+
{
97+
const file = path.join(tmpdir.path, 'testWriteFileSyncFlags.txt');
10598

106-
content = fs.readFileSync(file4, { encoding: 'utf8' });
107-
assert.strictEqual(content, 'hello world!');
99+
fs.writeFileSync(file, 'hello ', { encoding: 'utf8', flag: 'a' });
100+
fs.writeFileSync(file, 'world!', { encoding: 'utf8', flag: 'a' });
101+
const content = fs.readFileSync(file, { encoding: 'utf8' });
102+
assert.strictEqual(content, 'hello world!');
103+
}

0 commit comments

Comments
 (0)