Skip to content

Commit c6c0947

Browse files
anonrigRafaelGSS
authored andcommitted
test: split parallel fs-watch-recursive tests
PR-URL: #45865 Fixes: #45535 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent f7dba5b commit c6c0947

10 files changed

+457
-285
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { setTimeout } = require('timers/promises');
5+
6+
if (common.isIBMi)
7+
common.skip('IBMi does not support `fs.watch()`');
8+
9+
// fs-watch on folders have limited capability in AIX.
10+
// The testcase makes use of folder watching, and causes
11+
// hang. This behavior is documented. Skip this for AIX.
12+
13+
if (common.isAIX)
14+
common.skip('folder watch capability is limited in AIX.');
15+
16+
const assert = require('assert');
17+
const path = require('path');
18+
const fs = require('fs');
19+
20+
const tmpdir = require('../common/tmpdir');
21+
const testDir = tmpdir.path;
22+
tmpdir.refresh();
23+
24+
(async () => {
25+
// Add a file to subfolder of a watching folder
26+
27+
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
28+
const testDirectory = path.join(rootDirectory, 'test-4');
29+
fs.mkdirSync(testDirectory);
30+
31+
const file = 'folder-5';
32+
const filePath = path.join(testDirectory, file);
33+
fs.mkdirSync(filePath);
34+
35+
const subfolderPath = path.join(filePath, 'subfolder-6');
36+
fs.mkdirSync(subfolderPath);
37+
38+
const childrenFile = 'file-7.txt';
39+
const childrenAbsolutePath = path.join(subfolderPath, childrenFile);
40+
const relativePath = path.join(file, path.basename(subfolderPath), childrenFile);
41+
42+
const watcher = fs.watch(testDirectory, { recursive: true });
43+
let watcherClosed = false;
44+
watcher.on('change', function(event, filename) {
45+
assert.strictEqual(event, 'rename');
46+
47+
if (filename === relativePath) {
48+
watcher.close();
49+
watcherClosed = true;
50+
}
51+
});
52+
53+
await setTimeout(common.platformTimeout(100));
54+
fs.writeFileSync(childrenAbsolutePath, 'world');
55+
56+
process.once('exit', function() {
57+
assert(watcherClosed, 'watcher Object was not closed');
58+
});
59+
})().then(common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { setTimeout } = require('timers/promises');
5+
6+
if (common.isIBMi)
7+
common.skip('IBMi does not support `fs.watch()`');
8+
9+
// fs-watch on folders have limited capability in AIX.
10+
// The testcase makes use of folder watching, and causes
11+
// hang. This behavior is documented. Skip this for AIX.
12+
13+
if (common.isAIX)
14+
common.skip('folder watch capability is limited in AIX.');
15+
16+
const assert = require('assert');
17+
const path = require('path');
18+
const fs = require('fs');
19+
20+
const tmpdir = require('../common/tmpdir');
21+
const testDir = tmpdir.path;
22+
tmpdir.refresh();
23+
24+
(async () => {
25+
// Add a file to newly created folder to already watching folder
26+
27+
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
28+
const testDirectory = path.join(rootDirectory, 'test-3');
29+
fs.mkdirSync(testDirectory);
30+
31+
const filePath = path.join(testDirectory, 'folder-3');
32+
33+
const childrenFile = 'file-4.txt';
34+
const childrenAbsolutePath = path.join(filePath, childrenFile);
35+
const childrenRelativePath = path.join(path.basename(filePath), childrenFile);
36+
37+
const watcher = fs.watch(testDirectory, { recursive: true });
38+
let watcherClosed = false;
39+
watcher.on('change', function(event, filename) {
40+
assert.strictEqual(event, 'rename');
41+
assert.ok(filename === path.basename(filePath) || filename === childrenRelativePath);
42+
43+
if (filename === childrenRelativePath) {
44+
watcher.close();
45+
watcherClosed = true;
46+
}
47+
});
48+
49+
await setTimeout(common.platformTimeout(100));
50+
fs.mkdirSync(filePath);
51+
await setTimeout(common.platformTimeout(100));
52+
fs.writeFileSync(childrenAbsolutePath, 'world');
53+
54+
process.once('exit', function() {
55+
assert(watcherClosed, 'watcher Object was not closed');
56+
});
57+
})().then(common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { setTimeout } = require('timers/promises');
5+
6+
if (common.isIBMi)
7+
common.skip('IBMi does not support `fs.watch()`');
8+
9+
// fs-watch on folders have limited capability in AIX.
10+
// The testcase makes use of folder watching, and causes
11+
// hang. This behavior is documented. Skip this for AIX.
12+
13+
if (common.isAIX)
14+
common.skip('folder watch capability is limited in AIX.');
15+
16+
const assert = require('assert');
17+
const path = require('path');
18+
const fs = require('fs');
19+
const { pathToFileURL } = require('url');
20+
21+
const tmpdir = require('../common/tmpdir');
22+
const testDir = tmpdir.path;
23+
tmpdir.refresh();
24+
25+
(async () => {
26+
// Add a file to already watching folder, and use URL as the path
27+
28+
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
29+
const testDirectory = path.join(rootDirectory, 'test-5');
30+
fs.mkdirSync(testDirectory);
31+
32+
const filePath = path.join(testDirectory, 'file-8.txt');
33+
const url = pathToFileURL(testDirectory);
34+
35+
const watcher = fs.watch(url, { recursive: true });
36+
let watcherClosed = false;
37+
watcher.on('change', function(event, filename) {
38+
assert.strictEqual(event, 'rename');
39+
40+
if (filename === path.basename(filePath)) {
41+
watcher.close();
42+
watcherClosed = true;
43+
}
44+
});
45+
46+
await setTimeout(common.platformTimeout(100));
47+
fs.writeFileSync(filePath, 'world');
48+
49+
process.on('exit', function() {
50+
assert(watcherClosed, 'watcher Object was not closed');
51+
});
52+
})().then(common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { setTimeout } = require('timers/promises');
5+
6+
if (common.isIBMi)
7+
common.skip('IBMi does not support `fs.watch()`');
8+
9+
// fs-watch on folders have limited capability in AIX.
10+
// The testcase makes use of folder watching, and causes
11+
// hang. This behavior is documented. Skip this for AIX.
12+
13+
if (common.isAIX)
14+
common.skip('folder watch capability is limited in AIX.');
15+
16+
const assert = require('assert');
17+
const path = require('path');
18+
const fs = require('fs');
19+
20+
const tmpdir = require('../common/tmpdir');
21+
const testDir = tmpdir.path;
22+
tmpdir.refresh();
23+
24+
(async () => {
25+
// Add a file to already watching folder
26+
27+
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
28+
const testDirectory = path.join(rootDirectory, 'test-1');
29+
fs.mkdirSync(testDirectory);
30+
31+
const testFile = path.join(testDirectory, 'file-1.txt');
32+
33+
const watcher = fs.watch(testDirectory, { recursive: true });
34+
let watcherClosed = false;
35+
watcher.on('change', function(event, filename) {
36+
assert.strictEqual(event, 'rename');
37+
38+
if (filename === path.basename(testFile)) {
39+
watcher.close();
40+
watcherClosed = true;
41+
}
42+
});
43+
44+
await setTimeout(common.platformTimeout(100));
45+
fs.writeFileSync(testFile, 'world');
46+
47+
process.once('exit', function() {
48+
assert(watcherClosed, 'watcher Object was not closed');
49+
});
50+
})().then(common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { setTimeout } = require('timers/promises');
5+
6+
if (common.isIBMi)
7+
common.skip('IBMi does not support `fs.watch()`');
8+
9+
// fs-watch on folders have limited capability in AIX.
10+
// The testcase makes use of folder watching, and causes
11+
// hang. This behavior is documented. Skip this for AIX.
12+
13+
if (common.isAIX)
14+
common.skip('folder watch capability is limited in AIX.');
15+
16+
const assert = require('assert');
17+
const path = require('path');
18+
const fs = require('fs');
19+
20+
const tmpdir = require('../common/tmpdir');
21+
const testDir = tmpdir.path;
22+
tmpdir.refresh();
23+
24+
(async () => {
25+
// Add a folder to already watching folder
26+
27+
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
28+
const testDirectory = path.join(rootDirectory, 'test-2');
29+
fs.mkdirSync(testDirectory);
30+
31+
const testFile = path.join(testDirectory, 'folder-2');
32+
33+
const watcher = fs.watch(testDirectory, { recursive: true });
34+
let watcherClosed = false;
35+
watcher.on('change', function(event, filename) {
36+
assert.strictEqual(event, 'rename');
37+
38+
if (filename === path.basename(testFile)) {
39+
watcher.close();
40+
watcherClosed = true;
41+
}
42+
});
43+
44+
await setTimeout(common.platformTimeout(100));
45+
fs.mkdirSync(testFile);
46+
47+
process.once('exit', function() {
48+
assert(watcherClosed, 'watcher Object was not closed');
49+
});
50+
})().then(common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { setTimeout } = require('timers/promises');
5+
6+
if (common.isIBMi)
7+
common.skip('IBMi does not support `fs.watch()`');
8+
9+
// fs-watch on folders have limited capability in AIX.
10+
// The testcase makes use of folder watching, and causes
11+
// hang. This behavior is documented. Skip this for AIX.
12+
13+
if (common.isAIX)
14+
common.skip('folder watch capability is limited in AIX.');
15+
16+
const assert = require('assert');
17+
const path = require('path');
18+
const fs = require('fs');
19+
20+
const tmpdir = require('../common/tmpdir');
21+
const testDir = tmpdir.path;
22+
tmpdir.refresh();
23+
24+
(async () => {
25+
// Assert recursive watch does not leak handles
26+
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
27+
const testDirectory = path.join(rootDirectory, 'test-7');
28+
const filePath = path.join(testDirectory, 'only-file.txt');
29+
fs.mkdirSync(testDirectory);
30+
31+
let watcherClosed = false;
32+
const watcher = fs.watch(testDirectory, { recursive: true });
33+
watcher.on('change', common.mustCallAtLeast(async (event, filename) => {
34+
await setTimeout(common.platformTimeout(100));
35+
if (filename === path.basename(filePath)) {
36+
watcher.close();
37+
watcherClosed = true;
38+
}
39+
await setTimeout(common.platformTimeout(100));
40+
assert(!process._getActiveHandles().some((handle) => handle.constructor.name === 'StatWatcher'));
41+
}));
42+
43+
process.on('exit', function() {
44+
assert(watcherClosed, 'watcher Object was not closed');
45+
});
46+
await setTimeout(common.platformTimeout(100));
47+
fs.writeFileSync(filePath, 'content');
48+
})().then(common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { setTimeout } = require('timers/promises');
5+
6+
if (common.isIBMi)
7+
common.skip('IBMi does not support `fs.watch()`');
8+
9+
// fs-watch on folders have limited capability in AIX.
10+
// The testcase makes use of folder watching, and causes
11+
// hang. This behavior is documented. Skip this for AIX.
12+
13+
if (common.isAIX)
14+
common.skip('folder watch capability is limited in AIX.');
15+
16+
const assert = require('assert');
17+
const path = require('path');
18+
const fs = require('fs');
19+
20+
const tmpdir = require('../common/tmpdir');
21+
const testDir = tmpdir.path;
22+
tmpdir.refresh();
23+
24+
(async () => {
25+
// Watch a folder and update an already existing file in it.
26+
27+
const rootDirectory = fs.mkdtempSync(testDir + path.sep);
28+
const testDirectory = path.join(rootDirectory, 'test-0');
29+
fs.mkdirSync(testDirectory);
30+
31+
const testFile = path.join(testDirectory, 'file-1.txt');
32+
fs.writeFileSync(testFile, 'hello');
33+
34+
const watcher = fs.watch(testDirectory, { recursive: true });
35+
let watcherClosed = false;
36+
watcher.on('change', common.mustCallAtLeast(function(event, filename) {
37+
// Libuv inconsistenly emits a rename event for the file we are watching
38+
assert.ok(event === 'change' || event === 'rename');
39+
40+
if (filename === path.basename(testFile)) {
41+
watcher.close();
42+
watcherClosed = true;
43+
}
44+
}));
45+
46+
await setTimeout(common.platformTimeout(100));
47+
fs.writeFileSync(testFile, 'hello');
48+
49+
process.once('exit', function() {
50+
assert(watcherClosed, 'watcher Object was not closed');
51+
});
52+
})().then(common.mustCall());

0 commit comments

Comments
 (0)