|
| 1 | +var should = require("should"); |
| 2 | +var sinon = require("sinon"); |
| 3 | +var CachePlugin = require("../lib/CachePlugin"); |
| 4 | +var applyPluginWithOptions = require("./helpers/applyPluginWithOptions"); |
| 5 | + |
| 6 | +describe("CachePlugin", function() { |
| 7 | + var env; |
| 8 | + |
| 9 | + beforeEach(function() { |
| 10 | + env = { |
| 11 | + compilation: { |
| 12 | + compiler: {}, |
| 13 | + warnings: [] |
| 14 | + } |
| 15 | + }; |
| 16 | + }); |
| 17 | + |
| 18 | + it("has apply function", function() { |
| 19 | + (new CachePlugin()).apply.should.be.a.Function(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('applyMtime', function() { |
| 23 | + beforeEach(function() { |
| 24 | + env.plugin = new CachePlugin(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("sets file system accuracy to 1 for granular modification timestamp", function() { |
| 28 | + env.plugin.applyMtime(1483819067001) |
| 29 | + env.plugin.FS_ACCURENCY.should.be.exactly(1); |
| 30 | + }); |
| 31 | + |
| 32 | + it("sets file system accuracy to 10 for moderately granular modification timestamp", function() { |
| 33 | + env.plugin.applyMtime(1483819067004) |
| 34 | + env.plugin.FS_ACCURENCY.should.be.exactly(10); |
| 35 | + }); |
| 36 | + |
| 37 | + it("sets file system accuracy to 100 for moderately coarse modification timestamp", function() { |
| 38 | + env.plugin.applyMtime(1483819067040) |
| 39 | + env.plugin.FS_ACCURENCY.should.be.exactly(100); |
| 40 | + }); |
| 41 | + |
| 42 | + it("sets file system accuracy to 1000 for coarse modification timestamp", function() { |
| 43 | + env.plugin.applyMtime(1483819067400) |
| 44 | + env.plugin.FS_ACCURENCY.should.be.exactly(1000); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("when applied", function() { |
| 49 | + describe("for multiple compilers", function() { |
| 50 | + beforeEach(function() { |
| 51 | + var plugin = new CachePlugin(); |
| 52 | + env.compilers = [sinon.spy(), sinon.spy()]; |
| 53 | + plugin.apply({ |
| 54 | + compilers: env.compilers |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("calls each compilers apply with the cache plugin context", function() { |
| 59 | + env.compilers[0].callCount.should.be.exactly(1); |
| 60 | + env.compilers[0].firstCall.thisValue.should.be.instanceOf(CachePlugin); |
| 61 | + env.compilers[1].callCount.should.be.exactly(1); |
| 62 | + env.compilers[1].firstCall.thisValue.should.be.instanceOf(CachePlugin); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("for a single compiler", function() { |
| 67 | + beforeEach(function() { |
| 68 | + var applyContext = {}; |
| 69 | + env.eventBindings = applyPluginWithOptions.call(applyContext, CachePlugin, { |
| 70 | + test: true |
| 71 | + }); |
| 72 | + env.plugin = applyContext.plugin; |
| 73 | + }); |
| 74 | + |
| 75 | + it("binds four event handlers", function() { |
| 76 | + env.eventBindings.length.should.be.exactly(4); |
| 77 | + }); |
| 78 | + |
| 79 | + it("sets the initial cache", function() { |
| 80 | + env.plugin.cache.test.should.be.true(); |
| 81 | + }); |
| 82 | + |
| 83 | + describe("compilation handler", function() { |
| 84 | + it("binds to compilation event", function() { |
| 85 | + env.eventBindings[0].name.should.be.exactly("compilation"); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("when cachable", function() { |
| 89 | + describe("and not watching", function() { |
| 90 | + beforeEach(function() { |
| 91 | + env.eventBindings[0].handler(env.compilation); |
| 92 | + }); |
| 93 | + |
| 94 | + it("sets the compilation cache", function() { |
| 95 | + env.compilation.cache.should.deepEqual({ |
| 96 | + test: true |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("and watching", function() { |
| 102 | + beforeEach(function() { |
| 103 | + env.eventBindings[1].handler(env.compilation, function() {}); |
| 104 | + env.eventBindings[0].handler(env.compilation); |
| 105 | + }); |
| 106 | + |
| 107 | + it("does not add a compilation warning is added", function() { |
| 108 | + env.compilation.warnings.should.be.empty(); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("when not cachable", function() { |
| 114 | + beforeEach(function() { |
| 115 | + env.compilation.notCacheable = true; |
| 116 | + }); |
| 117 | + |
| 118 | + describe("and not watching", function() { |
| 119 | + beforeEach(function() { |
| 120 | + env.eventBindings[0].handler(env.compilation); |
| 121 | + }); |
| 122 | + |
| 123 | + it("does not set the cache", function() { |
| 124 | + should(env.compilation.cache).be.undefined(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe("and watching", function() { |
| 129 | + beforeEach(function() { |
| 130 | + env.eventBindings[1].handler(env.compilation, function() {}); |
| 131 | + env.eventBindings[0].handler(env.compilation); |
| 132 | + }); |
| 133 | + |
| 134 | + it("adds a compilation warning", function() { |
| 135 | + env.compilation.warnings.length.should.be.exactly(1); |
| 136 | + env.compilation.warnings[0].should.be.Error("CachePlugin - Cache cannot be used because of: true"); |
| 137 | + }); |
| 138 | + }); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe("watch-run handler", function() { |
| 143 | + beforeEach(function() { |
| 144 | + env.callback = sinon.spy(); |
| 145 | + env.eventBindings[1].handler(env.compilation.compiler, env.callback); |
| 146 | + }); |
| 147 | + |
| 148 | + it("binds to watch-run event", function() { |
| 149 | + env.eventBindings[1].name.should.be.exactly("watch-run"); |
| 150 | + }); |
| 151 | + |
| 152 | + it("sets watching flag", function() { |
| 153 | + env.plugin.watching.should.be.true(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("calls callback", function() { |
| 157 | + env.callback.callCount.should.be.exactly(1); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe("run handler", function() { |
| 162 | + beforeEach(function() { |
| 163 | + env.fsStat = sinon.spy(); |
| 164 | + env.callback = sinon.spy(); |
| 165 | + env.compilation.compiler.inputFileSystem = { |
| 166 | + stat: env.fsStat |
| 167 | + }; |
| 168 | + }); |
| 169 | + |
| 170 | + it("binds to run event", function() { |
| 171 | + env.eventBindings[2].name.should.be.exactly("run"); |
| 172 | + }); |
| 173 | + |
| 174 | + describe("Has not previously compiled", function() { |
| 175 | + beforeEach(function() { |
| 176 | + env.eventBindings[2].handler(env.compilation.compiler, env.callback); |
| 177 | + }); |
| 178 | + |
| 179 | + it("does not get any file stats", function() { |
| 180 | + env.fsStat.callCount.should.be.exactly(0); |
| 181 | + }); |
| 182 | + |
| 183 | + it("calls the callback", function() { |
| 184 | + env.callback.callCount.should.be.exactly(1); |
| 185 | + should(env.callback.firstCall.args[0]).be.undefined(); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + describe("Has previously compiled", function() { |
| 190 | + beforeEach(function() { |
| 191 | + env.compilation.fileDependencies = ["foo"]; |
| 192 | + env.compilation.contextDependencies = ["bar"]; |
| 193 | + env.eventBindings[3].handler(env.compilation, function() {}); |
| 194 | + env.eventBindings[2].handler(env.compilation.compiler, env.callback); |
| 195 | + }); |
| 196 | + |
| 197 | + it("calls for file stats for file dependencies", function() { |
| 198 | + env.fsStat.callCount.should.be.exactly(1); |
| 199 | + env.fsStat.firstCall.args[0].should.be.exactly("foo"); |
| 200 | + }); |
| 201 | + |
| 202 | + describe('file stats callback', function() { |
| 203 | + beforeEach(function() { |
| 204 | + env.fsStatCallback = env.fsStat.firstCall.args[1]; |
| 205 | + }); |
| 206 | + |
| 207 | + describe('when error occurs', function() { |
| 208 | + beforeEach(function() { |
| 209 | + env.fsStatCallback(new Error('Test Error')); |
| 210 | + }); |
| 211 | + |
| 212 | + it('calls handler callback with error', function() { |
| 213 | + env.callback.callCount.should.be.exactly(1); |
| 214 | + env.callback.firstCall.args[0].should.be.Error('Test Error'); |
| 215 | + }); |
| 216 | + }); |
| 217 | + |
| 218 | + describe('when ENOENT error occurs', function() { |
| 219 | + beforeEach(function() { |
| 220 | + env.fsStatCallback({ |
| 221 | + code: 'ENOENT' |
| 222 | + }); |
| 223 | + }); |
| 224 | + |
| 225 | + it('calls handler callback without error', function() { |
| 226 | + env.callback.callCount.should.be.exactly(1); |
| 227 | + should(env.callback.firstCall.args[0]).be.undefined(); |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
| 231 | + describe('when stat does not have modified time', function() { |
| 232 | + beforeEach(function() { |
| 233 | + sinon.stub(env.plugin, 'applyMtime'); |
| 234 | + env.fsStatCallback(null, {}); |
| 235 | + }); |
| 236 | + |
| 237 | + afterEach(function() { |
| 238 | + env.plugin.applyMtime.restore(); |
| 239 | + }); |
| 240 | + |
| 241 | + it('does not update file system accuracy', function() { |
| 242 | + env.plugin.applyMtime.callCount.should.be.exactly(0); |
| 243 | + }); |
| 244 | + |
| 245 | + it('updates file modified timestamp to infinity', function() { |
| 246 | + env.compilation.compiler.fileTimestamps.should.deepEqual({ |
| 247 | + foo: Infinity |
| 248 | + }); |
| 249 | + }); |
| 250 | + |
| 251 | + it('calls handler callback without error', function() { |
| 252 | + env.callback.callCount.should.be.exactly(1); |
| 253 | + should(env.callback.firstCall.args[0]).be.undefined(); |
| 254 | + }); |
| 255 | + }); |
| 256 | + |
| 257 | + describe('when stat has modified time', function() { |
| 258 | + beforeEach(function() { |
| 259 | + sinon.stub(env.plugin, 'applyMtime'); |
| 260 | + env.fsStatCallback(null, { |
| 261 | + mtime: 1483819067001 |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + afterEach(function() { |
| 266 | + env.plugin.applyMtime.restore(); |
| 267 | + }); |
| 268 | + |
| 269 | + it('does not update file system accuracy', function() { |
| 270 | + env.plugin.applyMtime.callCount.should.be.exactly(1); |
| 271 | + }); |
| 272 | + |
| 273 | + it('updates file modified timestamp to modified time with accuracy value', function() { |
| 274 | + env.compilation.compiler.fileTimestamps.should.deepEqual({ |
| 275 | + foo: 1483819069001 |
| 276 | + }); |
| 277 | + }); |
| 278 | + |
| 279 | + it('calls handler callback without error', function() { |
| 280 | + env.callback.callCount.should.be.exactly(1); |
| 281 | + should(env.callback.firstCall.args[0]).be.undefined(); |
| 282 | + }); |
| 283 | + }); |
| 284 | + }); |
| 285 | + }); |
| 286 | + }); |
| 287 | + |
| 288 | + describe("after-compile handler", function() { |
| 289 | + beforeEach(function() { |
| 290 | + env.compilation.fileDependencies = ["foo"]; |
| 291 | + env.compilation.contextDependencies = ["bar"]; |
| 292 | + env.callback = sinon.spy(); |
| 293 | + env.eventBindings[3].handler(env.compilation, env.callback); |
| 294 | + }); |
| 295 | + |
| 296 | + it("binds to after-compile event", function() { |
| 297 | + env.eventBindings[3].name.should.be.exactly("after-compile"); |
| 298 | + }); |
| 299 | + |
| 300 | + it("saves copy of compilation file dependecies", function() { |
| 301 | + env.compilation.compiler.should.deepEqual({ |
| 302 | + _lastCompilationFileDependencies: ["foo"], |
| 303 | + _lastCompilationContextDependencies: ["bar"] |
| 304 | + }); |
| 305 | + }); |
| 306 | + |
| 307 | + it("calls callback", function() { |
| 308 | + env.callback.callCount.should.be.exactly(1); |
| 309 | + }); |
| 310 | + }); |
| 311 | + }); |
| 312 | + }); |
| 313 | +}); |
0 commit comments