forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCachePlugin.test.js
284 lines (231 loc) · 8.38 KB
/
CachePlugin.test.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
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
"use strict";
const should = require("should");
const sinon = require("sinon");
const CachePlugin = require("../lib/CachePlugin");
const applyPluginWithOptions = require("./helpers/applyPluginWithOptions");
describe("CachePlugin", () => {
let env;
beforeEach(() => {
env = {
compilation: {
compiler: {},
warnings: []
}
};
});
it("has apply ", () => {
(new CachePlugin()).apply.should.be.a.Function();
});
describe("applyMtime", () => {
beforeEach(() => env.plugin = new CachePlugin());
it("sets file system accuracy to 1 for granular modification timestamp", () => {
env.plugin.applyMtime(1483819067001);
env.plugin.FS_ACCURENCY.should.be.exactly(1);
});
it("sets file system accuracy to 10 for moderately granular modification timestamp", () => {
env.plugin.applyMtime(1483819067004);
env.plugin.FS_ACCURENCY.should.be.exactly(10);
});
it("sets file system accuracy to 100 for moderately coarse modification timestamp", () => {
env.plugin.applyMtime(1483819067040);
env.plugin.FS_ACCURENCY.should.be.exactly(100);
});
it("sets file system accuracy to 1000 for coarse modification timestamp", () => {
env.plugin.applyMtime(1483819067400);
env.plugin.FS_ACCURENCY.should.be.exactly(1000);
});
});
describe("when applied", () => {
describe("for multiple compilers", () => {
beforeEach(() => {
const plugin = new CachePlugin();
env.compilers = [sinon.spy(), sinon.spy()];
plugin.apply({
compilers: env.compilers
});
});
it("calls each compilers apply with the cache plugin context", () => {
env.compilers[0].callCount.should.be.exactly(1);
env.compilers[0].firstCall.thisValue.should.be.instanceOf(CachePlugin);
env.compilers[1].callCount.should.be.exactly(1);
env.compilers[1].firstCall.thisValue.should.be.instanceOf(CachePlugin);
});
});
describe("for a single compiler", () => {
beforeEach(() => {
const applyContext = {};
env.eventBindings = applyPluginWithOptions.call(applyContext, CachePlugin, {
test: true
});
env.plugin = applyContext.plugin;
});
it("binds four event handlers", () =>
env.eventBindings.length.should.be.exactly(4));
it("sets the initial cache", () =>
env.plugin.cache.test.should.be.true());
describe("compilation handler", () => {
it("binds to compilation event", () =>
env.eventBindings[0].name.should.be.exactly("compilation"));
describe("when cachable", () => {
describe("and not watching", () => {
beforeEach(() =>
env.eventBindings[0].handler(env.compilation));
it("sets the compilation cache", () =>
env.compilation.cache.should.deepEqual({
test: true
}));
});
describe("and watching", () => {
beforeEach(() => {
env.eventBindings[1].handler(env.compilation, () => {});
env.eventBindings[0].handler(env.compilation);
});
it("does not add a compilation warning is added", () =>
env.compilation.warnings.should.be.empty());
});
});
describe("when not cachable", () => {
beforeEach(() =>
env.compilation.notCacheable = true);
describe("and not watching", () => {
beforeEach(() =>
env.eventBindings[0].handler(env.compilation));
it("does not set the cache", () =>
should(env.compilation.cache).be.undefined());
});
describe("and watching", () => {
beforeEach(() => {
env.eventBindings[1].handler(env.compilation, () => {});
env.eventBindings[0].handler(env.compilation);
});
it("adds a compilation warning", () => {
env.compilation.warnings.length.should.be.exactly(1);
env.compilation.warnings[0].should.be.Error("CachePlugin - Cache cannot be used because of: true");
});
});
});
});
describe("watch-run handler", () => {
beforeEach(() => {
env.callback = sinon.spy();
env.eventBindings[1].handler(env.compilation.compiler, env.callback);
});
it("binds to watch-run event", () =>
env.eventBindings[1].name.should.be.exactly("watch-run"));
it("sets watching flag", () =>
env.plugin.watching.should.be.true());
it("calls callback", () =>
env.callback.callCount.should.be.exactly(1));
});
describe("run handler", () => {
beforeEach(() => {
env.fsStat = sinon.spy();
env.callback = sinon.spy();
env.compilation.compiler.inputFileSystem = {
stat: env.fsStat
};
});
it("binds to run event", () =>
env.eventBindings[2].name.should.be.exactly("run"));
describe("Has not previously compiled", () => {
beforeEach(() =>
env.eventBindings[2].handler(env.compilation.compiler, env.callback));
it("does not get any file stats", () =>
env.fsStat.callCount.should.be.exactly(0));
it("calls the callback", () => {
env.callback.callCount.should.be.exactly(1);
should(env.callback.firstCall.args[0]).be.undefined();
});
});
describe("Has previously compiled", () => {
beforeEach(() => {
env.compilation.fileDependencies = ["foo"];
env.compilation.contextDependencies = ["bar"];
env.eventBindings[3].handler(env.compilation, () => {});
env.eventBindings[2].handler(env.compilation.compiler, env.callback);
});
it("calls for file stats for file dependencies", () => {
env.fsStat.callCount.should.be.exactly(1);
env.fsStat.firstCall.args[0].should.be.exactly("foo");
});
describe("file stats callback", () => {
beforeEach(() =>
env.fsStatCallback = env.fsStat.firstCall.args[1]);
describe("when error occurs", () => {
beforeEach(() =>
env.fsStatCallback(new Error("Test Error")));
it("calls handler callback with error", () => {
env.callback.callCount.should.be.exactly(1);
env.callback.firstCall.args[0].should.be.Error("Test Error");
});
});
describe("when ENOENT error occurs", () => {
beforeEach(() =>
env.fsStatCallback({
code: "ENOENT"
}));
it("calls handler callback without error", () => {
env.callback.callCount.should.be.exactly(1);
should(env.callback.firstCall.args[0]).be.undefined();
});
});
describe("when stat does not have modified time", () => {
beforeEach(() => {
sinon.stub(env.plugin, "applyMtime");
env.fsStatCallback(null, {});
});
afterEach(() => env.plugin.applyMtime.restore());
it("does not update file system accuracy", () =>
env.plugin.applyMtime.callCount.should.be.exactly(0));
it("updates file modified timestamp to infinity", () =>
env.compilation.compiler.fileTimestamps.should.deepEqual({
foo: Infinity
}));
it("calls handler callback without error", () => {
env.callback.callCount.should.be.exactly(1);
should(env.callback.firstCall.args[0]).be.undefined();
});
});
describe("when stat has modified time", () => {
beforeEach(() => {
sinon.stub(env.plugin, "applyMtime");
env.fsStatCallback(null, {
mtime: 1483819067001
});
});
afterEach(() => env.plugin.applyMtime.restore());
it("does not update file system accuracy", () =>
env.plugin.applyMtime.callCount.should.be.exactly(1));
it("updates file modified timestamp to modified time with accuracy value", () =>
env.compilation.compiler.fileTimestamps.should.deepEqual({
foo: 1483819069001
}));
it("calls handler callback without error", () => {
env.callback.callCount.should.be.exactly(1);
should(env.callback.firstCall.args[0]).be.undefined();
});
});
});
});
});
describe("after-compile handler", () => {
beforeEach(() => {
env.compilation.fileDependencies = ["foo"];
env.compilation.contextDependencies = ["bar"];
env.callback = sinon.spy();
env.eventBindings[3].handler(env.compilation, env.callback);
});
it("binds to after-compile event", () =>
env.eventBindings[3].name.should.be.exactly("after-compile"));
it("saves copy of compilation file dependecies", () => {
env.compilation.compiler.should.deepEqual({
_lastCompilationFileDependencies: ["foo"],
_lastCompilationContextDependencies: ["bar"]
});
});
it("calls callback", () =>
env.callback.callCount.should.be.exactly(1));
});
});
});
});