Skip to content

Commit 1e4653b

Browse files
committed
test: split test-fs-watch-ignore-*
Split and simplify the tests into individual files. Refs: nodejs#61433
1 parent 5d39030 commit 1e4653b

17 files changed

+624
-701
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as common from '../common/index.mjs';
2+
import { skipIfNoWatch } from '../common/watch.js';
3+
4+
skipIfNoWatch();
5+
6+
// if (common.isSunOS)
7+
// common.skip('`fs.watch()` is not reliable on SunOS.');
8+
9+
const assert = await import('node:assert');
10+
const path = await import('node:path');
11+
const tmpdir = await import('../common/tmpdir.js');
12+
const { watch } = await import('node:fs/promises');
13+
const { writeFileSync } = await import('node:fs');
14+
15+
tmpdir.refresh();
16+
17+
const testDir = tmpdir.resolve();
18+
const keepFile = 'visible.txt';
19+
const ignoreFile = '.hidden';
20+
const keepFilePath = path.join(testDir, keepFile);
21+
const ignoreFilePath = path.join(testDir, ignoreFile);
22+
23+
const watcher = watch(testDir, {
24+
ignore: (filename) => filename.startsWith('.'),
25+
});
26+
27+
// Do the write with a delay to ensure that the OS is ready to notify us. See
28+
// https://github.com/nodejs/node/issues/52601.
29+
setTimeout(() => {
30+
writeFileSync(ignoreFilePath, 'ignored');
31+
writeFileSync(keepFilePath, 'content');
32+
}, common.platformTimeout(100));
33+
34+
for await (const { filename } of watcher) {
35+
assert.notStrictEqual(filename, ignoreFile);
36+
37+
if (filename === keepFile) {
38+
break;
39+
}
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as common from '../common/index.mjs';
2+
import { skipIfNoWatch } from '../common/watch.js';
3+
4+
skipIfNoWatch();
5+
6+
// if (common.isSunOS)
7+
// common.skip('`fs.watch()` is not reliable on SunOS.');
8+
9+
const assert = await import('node:assert');
10+
const path = await import('node:path');
11+
const tmpdir = await import('../common/tmpdir.js');
12+
const { watch } = await import('node:fs/promises');
13+
const { writeFileSync } = await import('node:fs');
14+
15+
tmpdir.refresh();
16+
17+
const testDir = tmpdir.resolve();
18+
const keepFile = 'keep.txt';
19+
const ignoreFile = 'ignore.log';
20+
const keepFilePath = path.join(testDir, keepFile);
21+
const ignoreFilePath = path.join(testDir, ignoreFile);
22+
23+
const watcher = watch(testDir, { ignore: '*.log' });
24+
25+
// Do the write with a delay to ensure that the OS is ready to notify us. See
26+
// https://github.com/nodejs/node/issues/52601.
27+
setTimeout(() => {
28+
writeFileSync(ignoreFilePath, 'ignored');
29+
writeFileSync(keepFilePath, 'content');
30+
}, common.platformTimeout(100));
31+
32+
for await (const { filename } of watcher) {
33+
assert.notStrictEqual(filename, ignoreFile);
34+
35+
if (filename === keepFile) {
36+
break;
37+
}
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import '../common/index.mjs';
2+
import { skipIfNoWatch } from '../common/watch.js';
3+
4+
skipIfNoWatch();
5+
6+
const assert = await import('node:assert');
7+
const { watch } = await import('node:fs/promises');
8+
9+
await assert.rejects(
10+
async () => {
11+
const watcher = watch('.', { ignore: 123 });
12+
// eslint-disable-next-line no-unused-vars
13+
for await (const _ of watcher) {
14+
// Will throw before yielding
15+
}
16+
},
17+
{
18+
code: 'ERR_INVALID_ARG_TYPE',
19+
name: 'TypeError',
20+
}
21+
);
22+
23+
await assert.rejects(
24+
async () => {
25+
const watcher = watch('.', { ignore: '' });
26+
// eslint-disable-next-line no-unused-vars
27+
for await (const _ of watcher) {
28+
// Will throw before yielding
29+
}
30+
},
31+
{
32+
code: 'ERR_INVALID_ARG_VALUE',
33+
name: 'TypeError',
34+
}
35+
);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as common from '../common/index.mjs';
2+
import { skipIfNoWatch } from '../common/watch.js';
3+
4+
skipIfNoWatch();
5+
6+
// if (common.isSunOS)
7+
// common.skip('`fs.watch()` is not reliable on SunOS.');
8+
9+
const assert = await import('node:assert');
10+
const path = await import('node:path');
11+
const tmpdir = await import('../common/tmpdir.js');
12+
const { watch } = await import('node:fs/promises');
13+
const { writeFileSync } = await import('node:fs');
14+
15+
tmpdir.refresh();
16+
17+
const testDir = tmpdir.resolve();
18+
const keepFile = 'keep.txt';
19+
const ignoreLog = 'debug.log';
20+
const ignoreTmp = 'temp.tmp';
21+
const keepFilePath = path.join(testDir, keepFile);
22+
const ignoreLogPath = path.join(testDir, ignoreLog);
23+
const ignoreTmpPath = path.join(testDir, ignoreTmp);
24+
25+
const watcher = watch(testDir, {
26+
ignore: [
27+
'*.log',
28+
/\.tmp$/,
29+
],
30+
});
31+
32+
// Do the write with a delay to ensure that the OS is ready to notify us. See
33+
// https://github.com/nodejs/node/issues/52601.
34+
setTimeout(() => {
35+
writeFileSync(ignoreLogPath, 'ignored');
36+
writeFileSync(ignoreTmpPath, 'ignored');
37+
writeFileSync(keepFilePath, 'content');
38+
}, common.platformTimeout(100));
39+
40+
for await (const { filename } of watcher) {
41+
assert.notStrictEqual(filename, ignoreLog);
42+
assert.notStrictEqual(filename, ignoreTmp);
43+
44+
if (filename === keepFile) {
45+
break;
46+
}
47+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as common from '../common/index.mjs';
2+
import { skipIfNoWatch } from '../common/watch.js';
3+
4+
skipIfNoWatch();
5+
6+
// if (common.isSunOS)
7+
// common.skip('`fs.watch()` is not reliable on SunOS.');
8+
9+
const assert = await import('node:assert');
10+
const path = await import('node:path');
11+
const tmpdir = await import('../common/tmpdir.js');
12+
const { watch } = await import('node:fs/promises');
13+
const { writeFileSync } = await import('node:fs');
14+
15+
tmpdir.refresh();
16+
17+
const testDir = tmpdir.resolve();
18+
const keepFile = 'keep.txt';
19+
const ignoreFile = 'ignore.tmp';
20+
const keepFilePath = path.join(testDir, keepFile);
21+
const ignoreFilePath = path.join(testDir, ignoreFile);
22+
23+
const watcher = watch(testDir, { ignore: /\.tmp$/ });
24+
25+
// Do the write with a delay to ensure that the OS is ready to notify us. See
26+
// https://github.com/nodejs/node/issues/52601.
27+
setTimeout(() => {
28+
writeFileSync(ignoreFilePath, 'ignored');
29+
writeFileSync(keepFilePath, 'content');
30+
}, common.platformTimeout(100));
31+
32+
for await (const { filename } of watcher) {
33+
assert.notStrictEqual(filename, ignoreFile);
34+
35+
if (filename === keepFile) {
36+
break;
37+
}
38+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { skipIfNoWatch } = require('../common/watch.js');
5+
6+
skipIfNoWatch();
7+
8+
// if (common.isSunOS)
9+
// common.skip('`fs.watch()` is not reliable on SunOS.');
10+
11+
const assert = require('assert');
12+
const path = require('path');
13+
const fs = require('fs');
14+
15+
const tmpdir = require('../common/tmpdir');
16+
17+
tmpdir.refresh();
18+
19+
const testFileName = 'visible.txt';
20+
const testFilePath = path.join(tmpdir.path, testFileName);
21+
const ignoredFileName = '.hidden';
22+
const ignoredFilePath = path.join(tmpdir.path, ignoredFileName);
23+
24+
const watcher = fs.watch(tmpdir.path, {
25+
ignore: (filename) => filename.startsWith('.'),
26+
});
27+
28+
watcher.on('change', common.mustCallAtLeast((event, filename) => {
29+
assert.notStrictEqual(filename, ignoredFileName);
30+
31+
if (filename === testFileName) {
32+
watcher.close();
33+
}
34+
}, 1));
35+
36+
// Do the write with a delay to ensure that the OS is ready to notify us. See
37+
// https://github.com/nodejs/node/issues/52601.
38+
setTimeout(() => {
39+
fs.writeFileSync(ignoredFilePath, 'ignored');
40+
fs.writeFileSync(testFilePath, 'content');
41+
}, common.platformTimeout(100));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { skipIfNoWatch } = require('../common/watch.js');
5+
6+
skipIfNoWatch();
7+
8+
// if (common.isSunOS)
9+
// common.skip('`fs.watch()` is not reliable on SunOS.');
10+
11+
const assert = require('assert');
12+
const path = require('path');
13+
const fs = require('fs');
14+
15+
const tmpdir = require('../common/tmpdir');
16+
17+
tmpdir.refresh();
18+
19+
const testFileName = 'file.txt';
20+
const testFilePath = path.join(tmpdir.path, testFileName);
21+
const ignoredFileName = 'file.log';
22+
const ignoredFilePath = path.join(tmpdir.path, ignoredFileName);
23+
24+
const watcher = fs.watch(tmpdir.path, {
25+
ignore: '*.log',
26+
});
27+
28+
watcher.on('change', common.mustCallAtLeast((event, filename) => {
29+
assert.notStrictEqual(filename, ignoredFileName);
30+
31+
if (filename === testFileName) {
32+
watcher.close();
33+
}
34+
}, 1));
35+
36+
// Do the write with a delay to ensure that the OS is ready to notify us. See
37+
// https://github.com/nodejs/node/issues/52601.
38+
setTimeout(() => {
39+
fs.writeFileSync(ignoredFilePath, 'ignored');
40+
fs.writeFileSync(testFilePath, 'content');
41+
}, common.platformTimeout(100));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { skipIfNoWatch } = require('../common/watch.js');
5+
6+
skipIfNoWatch();
7+
8+
const assert = require('assert');
9+
const fs = require('fs');
10+
11+
assert.throws(
12+
() => fs.watch('.', { ignore: 123 }),
13+
{
14+
code: 'ERR_INVALID_ARG_TYPE',
15+
name: 'TypeError',
16+
}
17+
);
18+
19+
assert.throws(
20+
() => fs.watch('.', { ignore: '' }),
21+
{
22+
code: 'ERR_INVALID_ARG_VALUE',
23+
name: 'TypeError',
24+
}
25+
);
26+
27+
assert.throws(
28+
() => fs.watch('.', { ignore: [123] }),
29+
{
30+
code: 'ERR_INVALID_ARG_TYPE',
31+
name: 'TypeError',
32+
}
33+
);
34+
35+
assert.throws(
36+
() => fs.watch('.', { ignore: [''] }),
37+
{
38+
code: 'ERR_INVALID_ARG_VALUE',
39+
name: 'TypeError',
40+
}
41+
);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { skipIfNoWatch } = require('../common/watch.js');
5+
6+
skipIfNoWatch();
7+
8+
// if (common.isSunOS)
9+
// common.skip('`fs.watch()` is not reliable on SunOS.');
10+
11+
const assert = require('assert');
12+
const path = require('path');
13+
const fs = require('fs');
14+
15+
const tmpdir = require('../common/tmpdir');
16+
17+
tmpdir.refresh();
18+
19+
const testFileName = 'keep.txt';
20+
const testFilePath = path.join(tmpdir.path, testFileName);
21+
const ignoredLogName = 'debug.log';
22+
const ignoredLogPath = path.join(tmpdir.path, ignoredLogName);
23+
const ignoredTmpName = 'temp.tmp';
24+
const ignoredTmpPath = path.join(tmpdir.path, ignoredTmpName);
25+
const ignoredHiddenName = '.secret';
26+
const ignoredHiddenPath = path.join(tmpdir.path, ignoredHiddenName);
27+
28+
const watcher = fs.watch(tmpdir.path, {
29+
ignore: [
30+
'*.log',
31+
/\.tmp$/,
32+
(filename) => filename.startsWith('.'),
33+
],
34+
});
35+
36+
watcher.on('change', common.mustCallAtLeast((event, filename) => {
37+
assert.notStrictEqual(filename, ignoredLogName);
38+
assert.notStrictEqual(filename, ignoredTmpName);
39+
assert.notStrictEqual(filename, ignoredHiddenName);
40+
41+
if (filename === testFileName) {
42+
watcher.close();
43+
}
44+
}, 1));
45+
46+
// Do the write with a delay to ensure that the OS is ready to notify us. See
47+
// https://github.com/nodejs/node/issues/52601.
48+
setTimeout(() => {
49+
fs.writeFileSync(ignoredLogPath, 'ignored');
50+
fs.writeFileSync(ignoredTmpPath, 'ignored');
51+
fs.writeFileSync(ignoredHiddenPath, 'ignored');
52+
fs.writeFileSync(testFilePath, 'content');
53+
}, common.platformTimeout(100));

0 commit comments

Comments
 (0)