diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index c68261e4e..6836af577 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -83,7 +83,7 @@ var createKarmaMiddleware = function(filesPromise, serveStaticFile, filePath = filePathToUrlPath(filePath, basePath); if (requestUrl === '/context.html') { - filePath += '?' + file.mtime.getTime(); + filePath += '?' + file.sha; } } @@ -98,7 +98,7 @@ var createKarmaMiddleware = function(filesPromise, serveStaticFile, var mappings = files.served.map(function(file) { var filePath = filePathToUrlPath(file.path, basePath); - return util.format(' \'%s\': \'%d\'', filePath, file.mtime.getTime()); + return util.format(' \'%s\': \'%s\'', filePath, file.sha); }); mappings = 'window.__karma__.files = {\n' + mappings.join(',\n') + '\n};\n'; diff --git a/lib/preprocessor.js b/lib/preprocessor.js index 3743ad170..76caaa02f 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -53,13 +53,15 @@ var createPreprocessor = function(config, basePath, injector) { } } - if (preprocessors.length) { - return fs.readFile(file.originalPath, function(err, buffer) { - nextPreprocessor(buffer.toString()); - }); - } + // compute SHA of the content + preprocessors.push(function(content, file, done) { + file.sha = sha1(content); + done(content); + }); - return process.nextTick(done); + return fs.readFile(file.originalPath, function(err, buffer) { + nextPreprocessor(buffer.toString()); + }); }; }; createPreprocessor.$inject = ['config.preprocessors', 'config.basePath', 'injector']; diff --git a/test/unit/middleware/karma.spec.coffee b/test/unit/middleware/karma.spec.coffee index 0efcc4d16..010c0b51d 100644 --- a/test/unit/middleware/karma.spec.coffee +++ b/test/unit/middleware/karma.spec.coffee @@ -9,6 +9,10 @@ describe 'middleware.karma', -> File = require('../../../lib/file-list').File Url = require('../../../lib/file-list').Url + MockFile = (path, sha) -> + File.call @, path + @sha = sha or 'sha-default' + fsMock = mocks.fs.create karma: static: @@ -101,15 +105,15 @@ describe 'middleware.karma', -> it 'should serve context.html with replaced script tags', (done) -> includedFiles [ - new File('/first.js', new Date 12345) - new File('/second.dart', new Date 67890) + new MockFile('/first.js', 'sha123') + new MockFile('/second.dart', 'sha456') ] response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'CONTEXT\n' + - '\n' + - '' + '\n' + + '' done() callHandlerWith '/__karma__/context.html' @@ -117,13 +121,13 @@ describe 'middleware.karma', -> it 'should serve context.html with replaced link tags', (done) -> includedFiles [ - new File('/first.css', new Date 12345) + new MockFile('/first.css', 'sha007') ] response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'CONTEXT\n' + - '' + '' done() callHandlerWith '/__karma__/context.html' @@ -131,15 +135,15 @@ describe 'middleware.karma', -> it 'should serve context.html with the correct path for the script tags', (done) -> includedFiles [ - new File('/some/abc/a.js', new Date 12345) - new File('/base/path/b.js', new Date 67890) + new MockFile('/some/abc/a.js', 'sha') + new MockFile('/base/path/b.js', 'shaaa') ] response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'CONTEXT\n' + - '\n' + - '' + '\n' + + '' done() callHandlerWith '/__karma__/context.html' @@ -147,15 +151,15 @@ describe 'middleware.karma', -> it 'should serve context.html with the correct path for link tags', (done) -> includedFiles [ - new File('/some/abc/a.css', new Date 12345) - new File('/base/path/b.css', new Date 67890) + new MockFile('/some/abc/a.css', 'sha1') + new MockFile('/base/path/b.css', 'sha2') ] response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'CONTEXT\n' + - '\n' + - '' + '\n' + + '' done() callHandlerWith '/__karma__/context.html' @@ -193,14 +197,14 @@ describe 'middleware.karma', -> it 'should inline mappings with all served files', (done) -> fsMock._touchFile '/karma/static/context.html', 0, '%MAPPINGS%' servedFiles [ - new File('/some/abc/a.js', new Date 12345) - new File('/base/path/b.js', new Date 67890) + new MockFile('/some/abc/a.js', 'sha_a') + new MockFile('/base/path/b.js', 'sha_b') ] response.once 'end', -> expect(response).to.beServedAs 200, 'window.__karma__.files = {\n' + - " '/absolute/some/abc/a.js': '12345',\n" + - " '/base/b.js': '67890'\n" + + " '/absolute/some/abc/a.js': 'sha_a',\n" + + " '/base/b.js': 'sha_b'\n" + "};\n" done() @@ -209,8 +213,8 @@ describe 'middleware.karma', -> it 'should serve debug.html with replaced script tags without timestamps', (done) -> includedFiles [ - new File('/first.js', new Date 12345) - new File('/base/path/b.js', new Date 67890) + new MockFile('/first.js') + new MockFile('/base/path/b.js') ] response.once 'end', -> @@ -225,8 +229,8 @@ describe 'middleware.karma', -> it 'should serve debug.html with replaced link tags without timestamps', (done) -> includedFiles [ - new File('/first.css', new Date 12345) - new File('/base/path/b.css', new Date 67890) + new MockFile('/first.css') + new MockFile('/base/path/b.css') ] response.once 'end', -> diff --git a/test/unit/preprocessor.spec.coffee b/test/unit/preprocessor.spec.coffee index cb9742be7..f9d040afc 100644 --- a/test/unit/preprocessor.spec.coffee +++ b/test/unit/preprocessor.spec.coffee @@ -11,6 +11,7 @@ describe 'preprocessor', -> mockFs = mocks.fs.create some: 'a.js': mocks.fs.file 0, 'content' + 'a.txt': mocks.fs.file 0, 'some-text' mocks_ = 'graceful-fs': mockFs @@ -74,3 +75,22 @@ describe 'preprocessor', -> expect(file.path).to.equal 'path-p1-p2' expect(file.content).to.equal 'content-c1-c2' done() + + + it 'should compute SHA', (done) -> + pp = m.createPreprocessor {}, null, new di.Injector([]) + file = {originalPath: '/some/a.js', path: 'path'} + + pp file, -> + expect(file.sha).to.exist + expect(file.sha.length).to.equal 40 + previousSHA = file.sha + + pp file, -> + expect(file.sha).to.equal previousSHA + mockFs._touchFile '/some/a.js', null, 'new-content' + + pp file, -> + expect(file.sha.length).to.equal 40 + expect(file.sha).not.to.equal previousSHA + done()