aboutsummaryrefslogtreecommitdiff
path: root/node_modules/ava/lib/watcher.js
blob: 3f5ed3ee79270d3a1dffd43b14bd783b6300077b (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
'use strict';
const nodePath = require('path');
const debug = require('debug')('ava:watcher');
const diff = require('lodash.difference');
const chokidar = require('chokidar');
const flatten = require('arr-flatten');
const union = require('array-union');
const uniq = require('array-uniq');
const AvaFiles = require('./ava-files');

function rethrowAsync(err) {
	// Don't swallow exceptions. Note that any
	// expected error should already have been logged
	setImmediate(() => {
		throw err;
	});
}

const MIN_DEBOUNCE_DELAY = 10;
const INITIAL_DEBOUNCE_DELAY = 100;

class Debouncer {
	constructor(watcher) {
		this.watcher = watcher;
		this.timer = null;
		this.repeat = false;
	}

	debounce(delay) {
		if (this.timer) {
			this.again = true;
			return;
		}

		delay = delay ? Math.max(delay, MIN_DEBOUNCE_DELAY) : INITIAL_DEBOUNCE_DELAY;

		const timer = setTimeout(() => {
			this.watcher.busy.then(() => {
				// Do nothing if debouncing was canceled while waiting for the busy
				// promise to fulfil
				if (this.timer !== timer) {
					return;
				}

				if (this.again) {
					this.timer = null;
					this.again = false;
					this.debounce(delay / 2);
				} else {
					this.watcher.runAfterChanges();
					this.timer = null;
					this.again = false;
				}
			});
		}, delay);

		this.timer = timer;
	}

	cancel() {
		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = null;
			this.again = false;
		}
	}
}

class TestDependency {
	constructor(file, sources) {
		this.file = file;
		this.sources = sources;
	}

	contains(source) {
		return this.sources.indexOf(source) !== -1;
	}
}

class Watcher {
	constructor(logger, api, files, sources) {
		this.debouncer = new Debouncer(this);
		this.avaFiles = new AvaFiles({
			files,
			sources
		});

		this.clearLogOnNextRun = true;
		this.runVector = 0;
		this.previousFiles = files;
		this.run = (specificFiles, updateSnapshots) => {
			if (this.runVector > 0) {
				const cleared = this.clearLogOnNextRun && logger.clear();
				if (!cleared) {
					logger.reset();
					logger.section();
				}
				this.clearLogOnNextRun = true;

				logger.reset();
				logger.start();
			}

			this.runVector += 1;

			const currentVector = this.runVector;

			let runOnlyExclusive = false;

			if (specificFiles) {
				const exclusiveFiles = specificFiles.filter(file => this.filesWithExclusiveTests.indexOf(file) !== -1);

				runOnlyExclusive = exclusiveFiles.length !== this.filesWithExclusiveTests.length;

				if (runOnlyExclusive) {
					// The test files that previously contained exclusive tests are always
					// run, together with the remaining specific files.
					const remainingFiles = diff(specificFiles, exclusiveFiles);
					specificFiles = this.filesWithExclusiveTests.concat(remainingFiles);
				}
			}

			this.touchedFiles.clear();
			this.previousFiles = specificFiles || files;
			this.busy = api.run(this.previousFiles, {runOnlyExclusive, updateSnapshots: updateSnapshots === true})
				.then(runStatus => {
					runStatus.previousFailCount = this.sumPreviousFailures(currentVector);
					logger.finish(runStatus);

					const badCounts = runStatus.failCount + runStatus.rejectionCount + runStatus.exceptionCount;
					this.clearLogOnNextRun = this.clearLogOnNextRun && badCounts === 0;
				})
				.catch(rethrowAsync);
		};

		this.testDependencies = [];
		this.trackTestDependencies(api, sources);

		this.touchedFiles = new Set();
		this.trackTouchedFiles(api);

		this.filesWithExclusiveTests = [];
		this.trackExclusivity(api);

		this.filesWithFailures = [];
		this.trackFailures(api);

		this.dirtyStates = {};
		this.watchFiles();
		this.rerunAll();
	}

	watchFiles() {
		const patterns = this.avaFiles.getChokidarPatterns();

		chokidar.watch(patterns.paths, {
			ignored: patterns.ignored,
			ignoreInitial: true
		}).on('all', (event, path) => {
			if (event === 'add' || event === 'change' || event === 'unlink') {
				debug('Detected %s of %s', event, path);
				this.dirtyStates[path] = event;
				this.debouncer.debounce();
			}
		});
	}

	trackTestDependencies(api) {
		const relative = absPath => nodePath.relative(process.cwd(), absPath);

		api.on('test-run', runStatus => {
			runStatus.on('dependencies', (file, dependencies) => {
				const sourceDeps = dependencies.map(x => relative(x)).filter(this.avaFiles.isSource);
				this.updateTestDependencies(file, sourceDeps);
			});
		});
	}

	updateTestDependencies(file, sources) {
		if (sources.length === 0) {
			this.testDependencies = this.testDependencies.filter(dep => dep.file !== file);
			return;
		}

		const isUpdate = this.testDependencies.some(dep => {
			if (dep.file !== file) {
				return false;
			}

			dep.sources = sources;

			return true;
		});

		if (!isUpdate) {
			this.testDependencies.push(new TestDependency(file, sources));
		}
	}

	trackTouchedFiles(api) {
		api.on('test-run', runStatus => {
			runStatus.on('touchedFiles', files => {
				for (const file of files) {
					this.touchedFiles.add(nodePath.relative(process.cwd(), file));
				}
			});
		});
	}

	trackExclusivity(api) {
		api.on('stats', stats => {
			this.updateExclusivity(stats.file, stats.hasExclusive);
		});
	}

	updateExclusivity(file, hasExclusiveTests) {
		const index = this.filesWithExclusiveTests.indexOf(file);

		if (hasExclusiveTests && index === -1) {
			this.filesWithExclusiveTests.push(file);
		} else if (!hasExclusiveTests && index !== -1) {
			this.filesWithExclusiveTests.splice(index, 1);
		}
	}

	trackFailures(api) {
		api.on('test-run', (runStatus, files) => {
			files.forEach(file => {
				this.pruneFailures(nodePath.relative(process.cwd(), file));
			});

			const currentVector = this.runVector;
			runStatus.on('error', err => {
				this.countFailure(err.file, currentVector);
			});
			runStatus.on('test', result => {
				if (result.error) {
					this.countFailure(result.file, currentVector);
				}
			});
		});
	}

	pruneFailures(file) {
		this.filesWithFailures = this.filesWithFailures.filter(state => state.file !== file);
	}

	countFailure(file, vector) {
		const isUpdate = this.filesWithFailures.some(state => {
			if (state.file !== file) {
				return false;
			}

			state.count++;
			return true;
		});

		if (!isUpdate) {
			this.filesWithFailures.push({
				file,
				vector,
				count: 1
			});
		}
	}

	sumPreviousFailures(beforeVector) {
		let total = 0;

		this.filesWithFailures.forEach(state => {
			if (state.vector < beforeVector) {
				total += state.count;
			}
		});

		return total;
	}

	cleanUnlinkedTests(unlinkedTests) {
		unlinkedTests.forEach(testFile => {
			this.updateTestDependencies(testFile, []);
			this.updateExclusivity(testFile, false);
			this.pruneFailures(testFile);
		});
	}

	observeStdin(stdin) {
		stdin.resume();
		stdin.setEncoding('utf8');

		stdin.on('data', data => {
			data = data.trim().toLowerCase();
			if (data !== 'r' && data !== 'rs' && data !== 'u') {
				return;
			}

			// Cancel the debouncer, it might rerun specific tests whereas *all* tests
			// need to be rerun
			this.debouncer.cancel();
			this.busy.then(() => {
				// Cancel the debouncer again, it might have restarted while waiting for
				// the busy promise to fulfil
				this.debouncer.cancel();
				this.clearLogOnNextRun = false;
				if (data === 'u') {
					this.updatePreviousSnapshots();
				} else {
					this.rerunAll();
				}
			});
		});
	}

	rerunAll() {
		this.dirtyStates = {};
		this.run();
	}

	updatePreviousSnapshots() {
		this.dirtyStates = {};
		this.run(this.previousFiles, true);
	}

	runAfterChanges() {
		const dirtyStates = this.dirtyStates;
		this.dirtyStates = {};

		const dirtyPaths = Object.keys(dirtyStates).filter(path => {
			if (this.touchedFiles.has(path)) {
				debug('Ignoring known touched file %s', path);
				this.touchedFiles.delete(path);
				return false;
			}
			return true;
		});
		const dirtyTests = dirtyPaths.filter(this.avaFiles.isTest);
		const dirtySources = diff(dirtyPaths, dirtyTests);
		const addedOrChangedTests = dirtyTests.filter(path => dirtyStates[path] !== 'unlink');
		const unlinkedTests = diff(dirtyTests, addedOrChangedTests);

		this.cleanUnlinkedTests(unlinkedTests);

		// No need to rerun tests if the only change is that tests were deleted
		if (unlinkedTests.length === dirtyPaths.length) {
			return;
		}

		if (dirtySources.length === 0) {
			// Run any new or changed tests
			this.run(addedOrChangedTests);
			return;
		}

		// Try to find tests that depend on the changed source files
		const testsBySource = dirtySources.map(path => {
			return this.testDependencies.filter(dep => dep.contains(path)).map(dep => {
				debug('%s is a dependency of %s', path, dep.file);
				return dep.file;
			});
		}, this).filter(tests => tests.length > 0);

		// Rerun all tests if source files were changed that could not be traced to
		// specific tests
		if (testsBySource.length !== dirtySources.length) {
			debug('Sources remain that cannot be traced to specific tests: %O', dirtySources);
			debug('Rerunning all tests');
			this.run();
			return;
		}

		// Run all affected tests
		this.run(union(addedOrChangedTests, uniq(flatten(testsBySource))));
	}
}

module.exports = Watcher;