-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
watch.js
166 lines (136 loc) 路 4.18 KB
/
watch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import fs from 'node:fs/promises';
import path from 'node:path';
import {setTimeout as delay} from 'node:timers/promises';
import {fileURLToPath} from 'node:url';
import ava from '@ava/test';
import {temporaryDirectoryTask} from 'tempy';
import {available} from '../../../lib/watcher.js';
import {cwd, exec} from '../../helpers/exec.js';
export const test = available(fileURLToPath(import.meta.url)) ? ava : ava.skip;
export const serial = available(fileURLToPath(import.meta.url)) ? ava.serial : ava.serial.skip;
export const withFixture = fixture => async (t, task) => {
let completedTask = false;
await temporaryDirectoryTask(async dir => {
await fs.cp(cwd(fixture), dir, {recursive: true});
async function * run(args = [], options = {}) {
yield * exec(['--watch', ...args], {...options, cwd: dir, env: {AVA_FORCE_CI: 'not-ci', ...options.env}});
}
async function mkdir(file, options = {}) {
await fs.mkdir(path.join(dir, file), options);
}
async function read(file) {
return fs.readFile(path.join(dir, file), 'utf8');
}
async function rm(file, options = {}) {
await fs.rm(path.join(dir, file), options);
}
async function stat(file) {
return fs.stat(path.join(dir, file));
}
async function touch(file) {
const time = new Date();
await fs.utimes(path.join(dir, file), time, time);
}
async function write(file, contents = '') {
await fs.writeFile(path.join(dir, file), contents);
}
const operations = {
mkdir,
read,
rm,
stat,
touch,
write,
};
let activeWatchCount = 0;
await task(t, {
...operations,
dir,
run,
async watch(handlers, args = [], options = {}) {
activeWatchCount++;
let signalDone;
const donePromise = new Promise(resolve => {
signalDone = resolve;
});
let isDone = false;
const done = () => {
activeWatchCount--;
isDone = true;
signalDone({done: true});
};
let idlePromise = new Promise(() => {});
let assertingIdle = false;
let failedIdleAssertion = false;
const assertIdle = async next => {
assertingIdle = true;
// TODO: When testing using AVA 6, enable for better managed timeouts.
// t.timeout(10_000);
const promise = Promise.all([delay(5000, null, {ref: false}), next?.()]).finally(() => {
if (idlePromise === promise) {
idlePromise = new Promise(() => {});
assertingIdle = false;
// TODO: When testing using AVA 6, enable for better managed timeouts.
// t.timeout.clear();
if (failedIdleAssertion) {
failedIdleAssertion = false;
t.fail('Watcher performed a test run while it should have been idle');
}
}
return {};
});
idlePromise = promise;
await promise;
};
let state = {};
let pendingState;
const results = run(args, options);
try {
let nextResult = results.next();
while (true) { // eslint-disable-line no-constant-condition
const item = await Promise.race([nextResult, idlePromise, donePromise]); // eslint-disable-line no-await-in-loop
if (item.value) {
failedIdleAssertion ||= assertingIdle;
state = (await pendingState) ?? state; // eslint-disable-line no-await-in-loop
const result = item.value;
const {[result.runCount]: handler = handlers.else} = handlers;
pendingState = handler?.call({assertIdle, done, ...operations}, result, state);
if (!item.done && !isDone) {
nextResult = results.next();
}
}
if (item.done || isDone) {
item.value?.process.send('abort-watcher');
break;
}
}
} finally {
results.return();
// Handle outstanding promises in case they reject.
if (assertingIdle) {
await idlePromise;
}
await pendingState;
}
},
});
t.is(activeWatchCount, 0, 'Handlers for all watch() calls should have invoked `this.done()` to end their tests');
completedTask = true;
}).catch(error => {
if (!completedTask) {
throw error;
}
switch (error.code) { // https://github.com/sindresorhus/tempy/issues/47
case 'EBUSY':
case 'EMFILE':
case 'ENFILE':
case 'ENOTEMPTY':
case 'EPERM ': {
return;
}
default: {
throw error;
}
}
});
};