diff --git a/services/clsi/app/js/LatexRunner.js b/services/clsi/app/js/LatexRunner.js index 51286dce91..d956ee4949 100644 --- a/services/clsi/app/js/LatexRunner.js +++ b/services/clsi/app/js/LatexRunner.js @@ -110,11 +110,14 @@ function _writeLogOutput(projectId, directory, output, callback) { // internal method for writing non-empty log files function _writeFile(file, content, cb) { if (content && content.length > 0) { - fs.writeFile(file, content, err => { - if (err) { - logger.error({ err, projectId, file }, 'error writing log file') // don't fail on error - } - cb() + fs.unlink(file, () => { + fs.writeFile(file, content, { flag: 'wx' }, err => { + if (err) { + // don't fail on error + logger.error({ err, projectId, file }, 'error writing log file') + } + cb() + }) }) } else { cb() diff --git a/services/clsi/test/unit/js/LatexRunnerTests.js b/services/clsi/test/unit/js/LatexRunnerTests.js index 4da40837c4..ca36d7308f 100644 --- a/services/clsi/test/unit/js/LatexRunnerTests.js +++ b/services/clsi/test/unit/js/LatexRunnerTests.js @@ -23,6 +23,9 @@ describe('LatexRunner', function () { } this.fs = { writeFile: sinon.stub().yields(), + unlink: sinon + .stub() + .yields(new Error('ENOENT: no such file or directory, unlink ...')), } this.LatexRunner = SandboxedModule.require(MODULE_PATH, { requires: { @@ -99,11 +102,19 @@ describe('LatexRunner', function () { it('should record the stdout and stderr', function () { this.fs.writeFile.should.have.been.calledWith( this.directory + '/' + 'output.stdout', - 'this is stdout' + 'this is stdout', + { flag: 'wx' } ) this.fs.writeFile.should.have.been.calledWith( this.directory + '/' + 'output.stderr', - 'this is stderr' + 'this is stderr', + { flag: 'wx' } + ) + this.fs.unlink.should.have.been.calledWith( + this.directory + '/' + 'output.stdout' + ) + this.fs.unlink.should.have.been.calledWith( + this.directory + '/' + 'output.stderr' ) })