From 6cf795576bd6d77decac68ecc4838871b6df4836 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Mon, 13 Oct 2014 19:12:34 +0200 Subject: [PATCH] fix(preprocessor): calculate sha1 on content returned from a preprocessor Fixes #1204 --- lib/preprocessor.js | 2 +- test/unit/preprocessor.spec.coffee | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/preprocessor.js b/lib/preprocessor.js index 4e147187c..3d7db2a5d 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -52,6 +52,7 @@ var createPreprocessor = function(config, basePath, injector) { if (!preprocessors.length) { file.contentPath = null; file.content = content; + file.sha = sha1(content); return done(); } @@ -93,7 +94,6 @@ var createPreprocessor = function(config, basePath, injector) { if (err) { throw err; } - file.sha = sha1(buffer); nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString()); }); }; diff --git a/test/unit/preprocessor.spec.coffee b/test/unit/preprocessor.spec.coffee index 61dd224bc..ae8cedc94 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' + 'b.js': mocks.fs.file 0, 'content' 'a.txt': mocks.fs.file 0, 'some-text' 'photo.png': mocks.fs.file 0, 'binary' @@ -96,6 +97,28 @@ describe 'preprocessor', -> expect(file.sha).not.to.equal previousSHA done() + it 'should compute SHA from content returned by a processor', (done) -> + fakePreprocessor = sinon.spy (content, file, done) -> + done null, content + '-processed' + + injector = new di.Injector [{ + 'preprocessor:fake': ['factory', -> fakePreprocessor] + }] + + pp = m.createPreprocessor {'**/a.js': ['fake']}, null, injector + + fileProcess = {originalPath: '/some/a.js', path: 'path'} + fileSkip = {originalPath: '/some/b.js', path: 'path'} + + pp fileProcess, -> + pp fileSkip, -> + expect(fileProcess.sha).to.exist + expect(fileProcess.sha.length).to.equal 40 + expect(fileSkip.sha).to.exist + expect(fileSkip.sha.length).to.equal 40 + expect(fileProcess.sha).not.to.equal fileSkip.sha + done() + it 'should return error if any preprocessor fails', (done) -> failingPreprocessor = sinon.spy (content, file, done) ->