test: split test-fs-watch-ignore-* · nodejs/node@8c25489 · GitHub
Skip to content

Commit 8c25489

Browse files
lpincaaduh95
authored andcommitted
test: split test-fs-watch-ignore-*
Split and simplify the tests into individual files. Refs: #61433 PR-URL: #61494 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 830fff0 commit 8c25489

18 files changed

Lines changed: 690 additions & 701 deletions

test/parallel/parallel.status

Lines changed: 18 additions & 0 deletions
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as common from '../common/index.mjs';
2+
import { skipIfNoWatch } from '../common/watch.js';
3+
4+
skipIfNoWatch();
5+
6+
const assert = await import('node:assert');
7+
const path = await import('node:path');
8+
const tmpdir = await import('../common/tmpdir.js');
9+
const { setTimeout } = await import('node:timers/promises');
10+
const { watch } = await import('node:fs/promises');
11+
const { writeFileSync } = await import('node:fs');
12+
13+
tmpdir.refresh();
14+
15+
const testDir = tmpdir.resolve();
16+
const keepFile = 'visible.txt';
17+
const ignoreFile = '.hidden';
18+
const keepFilePath = path.join(testDir, keepFile);
19+
const ignoreFilePath = path.join(testDir, ignoreFile);
20+
21+
async function watchDir() {
22+
const watcher = watch(testDir, {
23+
ignore: (filename) => filename.startsWith('.'),
24+
});
25+
26+
for await (const { filename } of watcher) {
27+
assert.notStrictEqual(filename, ignoreFile);
28+
29+
if (filename === keepFile) {
30+
break;
31+
}
32+
}
33+
}
34+
35+
async function writeFiles() {
36+
if (common.isMacOS) {
37+
// Do the write with a delay to ensure that the OS is ready to notify us.
38+
// See https://github.com/nodejs/node/issues/52601.
39+
await setTimeout(common.platformTimeout(100));
40+
}
41+
42+
writeFileSync(ignoreFilePath, 'ignored');
43+
writeFileSync(keepFilePath, 'content');
44+
}
45+
46+
await Promise.all([watchDir(), writeFiles()]);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as common from '../common/index.mjs';
2+
import { skipIfNoWatch } from '../common/watch.js';
3+
4+
skipIfNoWatch();
5+
6+
const assert = await import('node:assert');
7+
const path = await import('node:path');
8+
const tmpdir = await import('../common/tmpdir.js');
9+
const { setTimeout } = await import('node:timers/promises');
10+
const { watch } = await import('node:fs/promises');
11+
const { writeFileSync } = await import('node:fs');
12+
13+
tmpdir.refresh();
14+
15+
const testDir = tmpdir.resolve();
16+
const keepFile = 'keep.txt';
17+
const ignoreFile = 'ignore.log';
18+
const keepFilePath = path.join(testDir, keepFile);
19+
const ignoreFilePath = path.join(testDir, ignoreFile);
20+
21+
async function watchDir() {
22+
const watcher = watch(testDir, { ignore: '*.log' });
23+
24+
for await (const { filename } of watcher) {
25+
assert.notStrictEqual(filename, ignoreFile);
26+
27+
if (filename === keepFile) {
28+
break;
29+
}
30+
}
31+
}
32+
33+
async function writeFiles() {
34+
if (common.isMacOS) {
35+
// Do the write with a delay to ensure that the OS is ready to notify us.
36+
// See https://github.com/nodejs/node/issues/52601.
37+
await setTimeout(common.platformTimeout(100));
38+
}
39+
40+
writeFileSync(ignoreFilePath, 'ignored');
41+
writeFileSync(keepFilePath, 'content');
42+
}
43+
44+
await Promise.all([watchDir(), writeFiles()]);
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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as common from '../common/index.mjs';
2+
import { skipIfNoWatch } from '../common/watch.js';
3+
4+
skipIfNoWatch();
5+
6+
const assert = await import('node:assert');
7+
const path = await import('node:path');
8+
const tmpdir = await import('../common/tmpdir.js');
9+
const { setTimeout } = await import('node:timers/promises');
10+
const { watch } = await import('node:fs/promises');
11+
const { writeFileSync } = await import('node:fs');
12+
13+
tmpdir.refresh();
14+
15+
const testDir = tmpdir.resolve();
16+
const keepFile = 'keep.txt';
17+
const ignoreLog = 'debug.log';
18+
const ignoreTmp = 'temp.tmp';
19+
const keepFilePath = path.join(testDir, keepFile);
20+
const ignoreLogPath = path.join(testDir, ignoreLog);
21+
const ignoreTmpPath = path.join(testDir, ignoreTmp);
22+
23+
async function watchDir() {
24+
const watcher = watch(testDir, {
25+
ignore: [
26+
'*.log',
27+
/\.tmp$/,
28+
],
29+
});
30+
31+
for await (const { filename } of watcher) {
32+
assert.notStrictEqual(filename, ignoreLog);
33+
assert.notStrictEqual(filename, ignoreTmp);
34+
35+
if (filename === keepFile) {
36+
break;
37+
}
38+
}
39+
}
40+
41+
async function writeFiles() {
42+
if (common.isMacOS) {
43+
// Do the write with a delay to ensure that the OS is ready to notify us.
44+
// See https://github.com/nodejs/node/issues/52601.
45+
await setTimeout(common.platformTimeout(100));
46+
}
47+
48+
writeFileSync(ignoreLogPath, 'ignored');
49+
writeFileSync(ignoreTmpPath, 'ignored');
50+
writeFileSync(keepFilePath, 'content');
51+
}
52+
53+
await Promise.all([watchDir(), writeFiles()]);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as common from '../common/index.mjs';
2+
import { skipIfNoWatch } from '../common/watch.js';
3+
4+
skipIfNoWatch();
5+
6+
const assert = await import('node:assert');
7+
const path = await import('node:path');
8+
const tmpdir = await import('../common/tmpdir.js');
9+
const { setTimeout } = await import('node:timers/promises');
10+
const { watch } = await import('node:fs/promises');
11+
const { writeFileSync } = await import('node:fs');
12+
13+
tmpdir.refresh();
14+
15+
const testDir = tmpdir.resolve();
16+
const keepFile = 'keep.txt';
17+
const ignoreFile = 'ignore.tmp';
18+
const keepFilePath = path.join(testDir, keepFile);
19+
const ignoreFilePath = path.join(testDir, ignoreFile);
20+
21+
async function watchDir() {
22+
const watcher = watch(testDir, { ignore: /\.tmp$/ });
23+
24+
for await (const { filename } of watcher) {
25+
assert.notStrictEqual(filename, ignoreFile);
26+
27+
if (filename === keepFile) {
28+
break;
29+
}
30+
}
31+
}
32+
33+
async function writeFiles() {
34+
if (common.isMacOS) {
35+
// Do the write with a delay to ensure that the OS is ready to notify us.
36+
// See https://github.com/nodejs/node/issues/52601.
37+
await setTimeout(common.platformTimeout(100));
38+
}
39+
40+
writeFileSync(ignoreFilePath, 'ignored');
41+
writeFileSync(keepFilePath, 'content');
42+
}
43+
44+
await Promise.all([watchDir(), writeFiles()]);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { skipIfNoWatch } = require('../common/watch.js');
5+
6+
skipIfNoWatch();
7+
8+
const assert = require('assert');
9+
const path = require('path');
10+
const fs = require('fs');
11+
12+
const tmpdir = require('../common/tmpdir');
13+
14+
tmpdir.refresh();
15+
16+
const testFileName = 'visible.txt';
17+
const testFilePath = path.join(tmpdir.path, testFileName);
18+
const ignoredFileName = '.hidden';
19+
const ignoredFilePath = path.join(tmpdir.path, ignoredFileName);
20+
21+
const watcher = fs.watch(tmpdir.path, {
22+
ignore: (filename) => filename.startsWith('.'),
23+
});
24+
25+
watcher.on('change', common.mustCallAtLeast((event, filename) => {
26+
assert.notStrictEqual(filename, ignoredFileName);
27+
28+
if (filename === testFileName) {
29+
watcher.close();
30+
}
31+
}, 1));
32+
33+
function writeFiles() {
34+
fs.writeFileSync(ignoredFilePath, 'ignored');
35+
fs.writeFileSync(testFilePath, 'content');
36+
}
37+
38+
if (common.isMacOS) {
39+
// Do the write with a delay to ensure that the OS is ready to notify us. See
40+
// https://github.com/nodejs/node/issues/52601.
41+
setTimeout(writeFiles, common.platformTimeout(100));
42+
} else {
43+
writeFiles();
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { skipIfNoWatch } = require('../common/watch.js');
5+
6+
skipIfNoWatch();
7+
8+
const assert = require('assert');
9+
const path = require('path');
10+
const fs = require('fs');
11+
12+
const tmpdir = require('../common/tmpdir');
13+
14+
tmpdir.refresh();
15+
16+
const testFileName = 'file.txt';
17+
const testFilePath = path.join(tmpdir.path, testFileName);
18+
const ignoredFileName = 'file.log';
19+
const ignoredFilePath = path.join(tmpdir.path, ignoredFileName);
20+
21+
const watcher = fs.watch(tmpdir.path, {
22+
ignore: '*.log',
23+
});
24+
25+
watcher.on('change', common.mustCallAtLeast((event, filename) => {
26+
assert.notStrictEqual(filename, ignoredFileName);
27+
28+
if (filename === testFileName) {
29+
watcher.close();
30+
}
31+
}, 1));
32+
33+
function writeFiles() {
34+
fs.writeFileSync(ignoredFilePath, 'ignored');
35+
fs.writeFileSync(testFilePath, 'content');
36+
}
37+
38+
if (common.isMacOS) {
39+
// Do the write with a delay to ensure that the OS is ready to notify us. See
40+
// https://github.com/nodejs/node/issues/52601.
41+
setTimeout(writeFiles, common.platformTimeout(100));
42+
} else {
43+
writeFiles();
44+
}
Lines changed: 41 additions & 0 deletions

0 commit comments

Comments
 (0)