Skip to content

Commit ce9e3cf

Browse files
Trottaddaleax
authored andcommitted
test: refactor test/sequential/test-fs-watch.js
* add block scoping * rename block-scoped identifiers (e.g., filenameTwo -> filename) * use common.mustCall() instead of exit handler * use common.mustNotCall() as appropriate * order modules per test writing guide Backport-PR-URL: #14575 Backport-Reviewed-By: Anna Henningsen <[email protected]> PR-URL: #14534 Reviewed-By: Colin Ihrig <[email protected]>
1 parent 32b30d5 commit ce9e3cf

File tree

1 file changed

+100
-105
lines changed

1 file changed

+100
-105
lines changed

test/sequential/test-fs-watch.js

+100-105
Original file line numberDiff line numberDiff line change
@@ -21,126 +21,121 @@
2121

2222
'use strict';
2323
const common = require('../common');
24+
2425
const assert = require('assert');
25-
const path = require('path');
2626
const fs = require('fs');
27+
const path = require('path');
2728

2829
const expectFilePath = common.isWindows ||
2930
common.isLinux ||
3031
common.isOSX ||
3132
common.isAIX;
3233

33-
let watchSeenOne = 0;
34-
let watchSeenTwo = 0;
35-
let watchSeenThree = 0;
36-
3734
const testDir = common.tmpDir;
3835

39-
const filenameOne = 'watch.txt';
40-
const filepathOne = path.join(testDir, filenameOne);
41-
42-
const filenameTwo = 'hasOwnProperty';
43-
const filepathTwo = filenameTwo;
44-
const filepathTwoAbs = path.join(testDir, filenameTwo);
45-
46-
process.on('exit', function() {
47-
assert.ok(watchSeenOne > 0);
48-
assert.ok(watchSeenTwo > 0);
49-
assert.ok(watchSeenThree > 0);
50-
});
51-
5236
common.refreshTmpDir();
5337

54-
fs.writeFileSync(filepathOne, 'hello');
55-
56-
assert.doesNotThrow(
57-
function() {
58-
const watcher = fs.watch(filepathOne);
59-
watcher.on('change', function(event, filename) {
60-
assert.strictEqual(event, 'change');
61-
62-
if (expectFilePath) {
63-
assert.strictEqual(filename, 'watch.txt');
64-
}
65-
watcher.close();
66-
++watchSeenOne;
67-
});
68-
}
69-
);
70-
71-
setImmediate(function() {
72-
fs.writeFileSync(filepathOne, 'world');
73-
});
74-
75-
76-
process.chdir(testDir);
77-
78-
fs.writeFileSync(filepathTwoAbs, 'howdy');
79-
80-
assert.doesNotThrow(
81-
function() {
82-
const watcher = fs.watch(filepathTwo, function(event, filename) {
83-
assert.strictEqual(event, 'change');
84-
85-
if (expectFilePath) {
86-
assert.strictEqual(filename, 'hasOwnProperty');
87-
}
88-
watcher.close();
89-
++watchSeenTwo;
90-
});
91-
}
92-
);
93-
94-
setImmediate(function() {
95-
fs.writeFileSync(filepathTwoAbs, 'pardner');
96-
});
97-
98-
const filenameThree = 'newfile.txt';
99-
const testsubdir = fs.mkdtempSync(testDir + path.sep);
100-
const filepathThree = path.join(testsubdir, filenameThree);
101-
102-
assert.doesNotThrow(
103-
function() {
104-
const watcher = fs.watch(testsubdir, function(event, filename) {
105-
const renameEv = common.isSunOS || common.isAIX ? 'change' : 'rename';
106-
assert.strictEqual(event, renameEv);
107-
if (expectFilePath) {
108-
assert.strictEqual(filename, 'newfile.txt');
109-
} else {
110-
assert.strictEqual(filename, null);
111-
}
112-
watcher.close();
113-
++watchSeenThree;
114-
});
115-
}
116-
);
117-
118-
setImmediate(function() {
119-
const fd = fs.openSync(filepathThree, 'w');
120-
fs.closeSync(fd);
121-
});
38+
{
39+
const filepath = path.join(testDir, 'watch.txt');
40+
41+
fs.writeFileSync(filepath, 'hello');
42+
43+
assert.doesNotThrow(
44+
function() {
45+
const watcher = fs.watch(filepath);
46+
watcher.on('change', common.mustCall(function(event, filename) {
47+
assert.strictEqual(event, 'change');
48+
49+
if (expectFilePath) {
50+
assert.strictEqual(filename, 'watch.txt');
51+
}
52+
watcher.close();
53+
}));
54+
}
55+
);
56+
57+
setImmediate(function() {
58+
fs.writeFileSync(filepath, 'world');
59+
});
60+
}
61+
62+
{
63+
const filepathAbs = path.join(testDir, 'hasOwnProperty');
64+
65+
process.chdir(testDir);
66+
67+
fs.writeFileSync(filepathAbs, 'howdy');
68+
69+
assert.doesNotThrow(
70+
function() {
71+
const watcher =
72+
fs.watch('hasOwnProperty', common.mustCall(function(event, filename) {
73+
assert.strictEqual(event, 'change');
74+
75+
if (expectFilePath) {
76+
assert.strictEqual(filename, 'hasOwnProperty');
77+
}
78+
watcher.close();
79+
}));
80+
}
81+
);
82+
83+
setImmediate(function() {
84+
fs.writeFileSync(filepathAbs, 'pardner');
85+
});
86+
}
87+
88+
{
89+
const testsubdir = fs.mkdtempSync(testDir + path.sep);
90+
const filepath = path.join(testsubdir, 'newfile.txt');
91+
92+
assert.doesNotThrow(
93+
function() {
94+
const watcher =
95+
fs.watch(testsubdir, common.mustCall(function(event, filename) {
96+
const renameEv = common.isSunOS || common.isAIX ? 'change' : 'rename';
97+
assert.strictEqual(event, renameEv);
98+
if (expectFilePath) {
99+
assert.strictEqual(filename, 'newfile.txt');
100+
} else {
101+
assert.strictEqual(filename, null);
102+
}
103+
watcher.close();
104+
}));
105+
}
106+
);
107+
108+
setImmediate(function() {
109+
const fd = fs.openSync(filepath, 'w');
110+
fs.closeSync(fd);
111+
});
112+
113+
}
122114

123115
// https://github.com/joyent/node/issues/2293 - non-persistent watcher should
124116
// not block the event loop
125-
fs.watch(__filename, {persistent: false}, function() {
126-
assert(0);
127-
});
117+
{
118+
fs.watch(__filename, { persistent: false }, common.mustNotCall());
119+
}
128120

129121
// whitebox test to ensure that wrapped FSEvent is safe
130122
// https://github.com/joyent/node/issues/6690
131-
let oldhandle;
132-
assert.throws(function() {
133-
const w = fs.watch(__filename, common.mustNotCall());
134-
oldhandle = w._handle;
135-
w._handle = { close: w._handle.close };
136-
w.close();
137-
}, /^TypeError: Illegal invocation$/);
138-
oldhandle.close(); // clean up
139-
140-
assert.throws(function() {
141-
const w = fs.watchFile(__filename, {persistent: false}, common.mustNotCall());
142-
oldhandle = w._handle;
143-
w._handle = { stop: w._handle.stop };
144-
w.stop();
145-
}, /^TypeError: Illegal invocation$/);
146-
oldhandle.stop(); // clean up
123+
{
124+
let oldhandle;
125+
assert.throws(function() {
126+
const w = fs.watch(__filename, common.mustNotCall());
127+
oldhandle = w._handle;
128+
w._handle = { close: w._handle.close };
129+
w.close();
130+
}, /^TypeError: Illegal invocation$/);
131+
oldhandle.close(); // clean up
132+
133+
assert.throws(function() {
134+
const w = fs.watchFile(__filename, { persistent: false },
135+
common.mustNotCall());
136+
oldhandle = w._handle;
137+
w._handle = { stop: w._handle.stop };
138+
w.stop();
139+
}, /^TypeError: Illegal invocation$/);
140+
oldhandle.stop(); // clean up
141+
}

0 commit comments

Comments
 (0)