Skip to content

Commit

Permalink
fix(preprocessor): retry if fs.readFile fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ywongau committed May 17, 2017
1 parent 2a847c2 commit 4b60513
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,19 @@ var createPreprocessor = function (config, basePath, injector) {

return function preprocess (file, done) {
patterns = Object.keys(config)

return fs.readFile(file.originalPath, function (err, buffer) {
var retryCount = 0
var maxRetries = 3
function handleFile (err, buffer) {
if (err) {
throw err
log.warn(err)
if (retryCount < maxRetries) {
retryCount++
log.warn('retrying ' + retryCount)
fs.readFile(file.originalPath, handleFile)
return
} else {
done()
}
}

isBinaryFile(buffer, buffer.length, function (err, thisFileIsBinary) {
Expand Down Expand Up @@ -121,7 +130,8 @@ var createPreprocessor = function (config, basePath, injector) {

nextPreprocessor(null, thisFileIsBinary ? buffer : buffer.toString())
})
})
}
return fs.readFile(file.originalPath, handleFile)
}
}
createPreprocessor.$inject = ['config.preprocessors', 'config.basePath', 'injector']
Expand Down
48 changes: 48 additions & 0 deletions test/unit/preprocessor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,54 @@ describe('preprocessor', () => {
})
})

describe('when fs.readFile fails', () => {
var file = {originalPath: '/some/a.js', path: 'path'}
var getReadFileCallback = (nthCall) => {
return mockFs.readFile.args[nthCall][1]
}

beforeEach(() => {
sinon.stub(mockFs, 'readFile')
})

it('should retry up to 3 times', (done) => {
var fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
})

var injector = new di.Injector([{
'preprocessor:fake': ['factory', () => fakePreprocessor]
}, emitterSetting])

var pp = m.createPreprocessor({'**/*.js': ['fake']}, null, injector)

pp(file, () => {
expect(fakePreprocessor).to.have.been.called
done()
})
getReadFileCallback(0)('error')
getReadFileCallback(1)('error')
var thirdCallback = getReadFileCallback(2)
mockFs.readFile.restore()
thirdCallback('error')
})

it('should abort after 3 retries', (done) => {
var injector = new di.Injector([{}, emitterSetting])

var pp = m.createPreprocessor({'**/*.js': []}, null, injector)

pp(file, () => {
done()
})

getReadFileCallback(0)('error')
getReadFileCallback(1)('error')
getReadFileCallback(2)('error')
getReadFileCallback(3)('error')
})
})

it('should not preprocess binary files', (done) => {
var fakePreprocessor = sinon.spy((content, file, done) => {
done(null, content)
Expand Down

0 comments on commit 4b60513

Please sign in to comment.