-
Notifications
You must be signed in to change notification settings - Fork 111
/
watcher.es
190 lines (171 loc) · 4.56 KB
/
watcher.es
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
import fs from "fs"
import path from "path"
import tape from "tape"
import {sync as rm} from "rimraf"
import {sync as mkdirp} from "mkdirp"
import chalk from "chalk"
import Metalsmith from "metalsmith"
import watch from "../watcher"
const noop = () => {}
const closers = {}
function cleanTests(key) {
closers[key]()
rm(`./tmp-${key}`)
}
function prepareTests(key, testCb, assertionCb, options) {
const folder = `./tmp-${key}`
const metalsmith = new Metalsmith(folder)
rm(folder)
mkdirp(`${folder}/src`)
fs.writeFileSync(`${folder}/src/dummy`, "")
const watcherOpts = {
log: noop,
...options,
}
let done = false
// chokidar might still detect dummy file created above
// so we delay just a bit the tests...
setTimeout(
() => {
metalsmith
.use(watch(watcherOpts))
.build(() => {
metalsmith
.use((files) => {
if (done) {
throw new Error("This assertion block should not be called twice")
}
closers[key] = watcherOpts.close
if (assertionCb !== noop) {
done = true
assertionCb(files)
// metalsmith write the builded files after this plugin
setTimeout(() => cleanTests(key), 1000)
}
})
setTimeout(() => testCb(), 500)
})
},
500
)
return folder
}
tape("metalsmith-server/watcher", (test) => {
test.test("logs", (t) => {
const logs = []
const key = "logs"
const folder = prepareTests(
key,
() => fs.writeFile(`${folder}/src/test`, "Test", noop),
() => {
t.deepEqual(
logs,
[
"✔︎ Watching **/*",
"✔︎ test added",
"- Updating 1 file...",
],
"should logs things")
t.end()
},
{
log: (log) => {
// console.log("## " + log)
logs.push(chalk.stripColor(log))
},
}
)
})
test.test("track create", (t) => {
const key = "create"
const folder = prepareTests(
key,
() => fs.writeFile(`${folder}/src/test`, "Test", noop),
(files) => {
t.ok(files.test, "should rebuild on file creation")
t.end()
}
)
})
test.test("track rename", (t) => {
const key = "rename"
const folder = prepareTests(
key,
() => fs.rename(`${folder}/src/dummy`, `${folder}/src/renamed`, noop),
(files) => {
t.ok(files.renamed, "should keep track of renamed files")
t.end()
}
)
})
// FIXME make this works on CI
if (!process.env.TRAVIS && !process.env.APPVEYOR) {
test.test("rebuild sibling mapping", (t) => {
const key = "sibling"
const sibling = `./tmp--sibling`
prepareTests(
key,
() => {
rm(sibling)
mkdirp(sibling)
fs.writeFile(`${sibling}/test`, "test", noop)
},
() => {
t.pass("should rebuild if a mapped item get updated")
t.end()
setTimeout(() => rm(sibling), 500)
},
{
paths: {
"**/*": true,
[`${sibling}/**/*`]: "**/*",
},
}
)
})
}
test.test("invalidate js cache", (t) => {
const key = "cache"
const folder = prepareTests(
key,
() => {
const jsfile = path.join(process.cwd(), `${folder}/src/dummy.js`)
fs.writeFile(jsfile, "module.exports = 1;", () => {
t.equal(require(jsfile), 1, "should get the exported value")
fs.writeFile(jsfile, "module.exports = 2;", () => {
setTimeout(() => {
t.equal(require(jsfile), 2, "should get the fresh exported value")
t.end()
cleanTests(key)
}, 1000) // let watcher clean the cache
})
})
},
noop
)
})
test.test("keep js cache", (t) => {
const key = "keepcache"
const folder = prepareTests(
key,
() => {
const jsfile = path.join(process.cwd(), `${folder}/src/dummy.js`)
fs.writeFile(jsfile, "module.exports = 1;", () => {
t.equal(require(jsfile), 1, "should get the exported value")
fs.writeFile(jsfile, "module.exports = 2;", () => {
setTimeout(() => {
t.equal(require(jsfile), 1, "should not update the cache")
t.end()
cleanTests(key)
}, 1000) // let watcher clean the cache
})
})
},
noop,
{
invalidateCache: false,
}
)
})
test.end()
})