9835c038d030c788919c16257d08ed2d9f67ca2c.svn-base
4.88 KB
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
const chokidar = require('chokidar')
const EE = require('events')
const Minipass = require('minipass')
const bin = require.resolve('../bin/run.js')
const {spawn} = require('child_process')
const mkdirp = require('mkdirp').sync
const rimraf = require('rimraf').sync
const onExit = require('signal-exit')
const {writeFileSync, readFileSync} = require('fs')
const {stringify} = require('tap-yaml')
const {resolve} = require('path')
class Watch extends Minipass {
constructor (options) {
if (!options.coverage)
throw new Error('--watch requires coverage to be enabled')
super()
this.args = [bin, ...options._.parsed, '--no-watch']
this.positionals = [...options._]
this.log('initial test run', this.args)
this.proc = spawn(process.execPath, this.args, {
stdio: 'inherit'
})
this.proc.on('close', () => this.main())
const saveFolder = 'node_modules/.cache/tap'
mkdirp(saveFolder)
this.saveFile = saveFolder + '/watch-' + process.pid
/* istanbul ignore next */
onExit(() => rimraf(this.saveFile))
this.index = null
this.indexFile = '.nyc_output/processinfo/index.json'
this.fileList = []
this.queue = []
this.watcher = null
this.env = { ...process.env }
}
readIndex () {
this.index = JSON.parse(readFileSync(this.indexFile, 'utf8'))
}
kill (signal) {
if (this.proc)
this.proc.kill(signal)
}
watchList () {
if (!this.index)
this.readIndex()
// externalIds are the relative path to a test file
// the files object keys are fully resolved.
// If a test is covered, it'll show up in both!
// Since a covered test was definitely included in its own
// test run, don't add it a second time, so we don't get
// two chokidar events for the same file change.
const cwd = process.cwd()
const fileSet = new Set(Object.keys(this.index.files))
Object.keys(this.index.externalIds)
.filter(f => !fileSet.has(resolve(f)))
.forEach(f => fileSet.add(f))
return [...fileSet]
}
pause () {
if (this.watcher)
this.watcher.close()
this.watcher = null
}
resume () {
if (!this.watcher)
this.watch()
}
main () {
this.emit('main')
this.proc = null
this.fileList = this.watchList()
this.watch()
}
watch () {
this.pause()
const sawAdd = new Map()
const watcher = this.watcher = chokidar.watch(this.fileList)
// ignore the first crop of add events, since we already ran the tests
watcher.on('all', (ev, file) => {
if (ev === 'add' && !sawAdd.get(file))
sawAdd.set(file, true)
else
this.onChange(ev, file)
})
return watcher
}
onChange (ev, file) {
const tests = this.testsFromChange(file)
this.queue.push(...tests)
this.log(ev + ' ' + file)
if (this.proc)
return this.log('test in progress, queuing for next run')
this.run()
}
run (env) {
const set = [...new Set(this.queue)]
this.log('running tests', set)
writeFileSync(this.saveFile, set.join('\n') + '\n')
this.queue.length = 0
this.proc = spawn(process.execPath, [
...this.args, '--save=' + this.saveFile, '--nyc-arg=--no-clean'
], {
stdio: 'inherit',
env: this.env,
})
this.proc.on('close', (code, signal) => this.onClose(code, signal))
this.emit('process', this.proc)
}
onClose (code, signal) {
this.readIndex()
this.proc = null
// only add if it's not already there as either a test or included file
const newFileList = this.watchList().filter(f =>
!this.fileList.includes(f) &&
!this.fileList.includes(resolve(f)))
this.fileList.push(...newFileList)
this.resume()
newFileList.forEach(f => this.watcher.add(f))
// if there are any failures (especially, from a bail out)
// then add those, but ignore if it's not there.
const leftover = (() => {
try {
return fs.readFileSync(saveFile, 'utf8').trim().split('\n')
} catch (er) {
return []
}
})()
// run again if something was added during the process
const runAgain = this.queue.length
this.queue.push(...leftover)
if (runAgain)
this.run()
else
this.emit('afterProcess', {code, signal})
}
log (msg, arg) {
if (arg && typeof arg !== 'string')
msg += '\n' + stringify(arg)
this.write(msg + '\n')
}
testsFromChange (file) {
return this.index.externalIds[file] ? [file]
: this.testsFromFile(file)
}
testsFromFile (file) {
const reducer = (set, uuid) => {
for (let process = this.index.processes[uuid];
process;
process = process.parent && this.index.processes[process.parent]) {
if (process.externalId)
set.add(process.externalId)
}
return set
}
const procs = this.index.files[file] || /* istanbul ignore next */ []
return [...procs.reduce(reducer, new Set())]
}
}
module.exports = {Watch}