2020-02-19 06:15:37 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
2021-05-19 06:17:08 -04:00
|
|
|
const { expect } = require('chai')
|
2022-06-06 07:41:36 -04:00
|
|
|
|
|
|
|
const MODULE_PATH = require('path').join(
|
2020-02-19 06:15:37 -05:00
|
|
|
__dirname,
|
|
|
|
'../../../app/js/LatexRunner'
|
|
|
|
)
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('LatexRunner', function () {
|
|
|
|
beforeEach(function () {
|
2022-06-06 07:41:36 -04:00
|
|
|
this.Settings = {
|
|
|
|
docker: {
|
|
|
|
socketPath: '/var/run/docker.sock',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
this.commandRunnerOutput = {
|
|
|
|
stdout: 'this is stdout',
|
|
|
|
stderr: 'this is stderr',
|
|
|
|
}
|
|
|
|
this.CommandRunner = {
|
|
|
|
run: sinon.stub().yields(null, this.commandRunnerOutput),
|
|
|
|
}
|
|
|
|
this.fs = {
|
|
|
|
writeFile: sinon.stub().yields(),
|
|
|
|
}
|
|
|
|
this.LatexRunner = SandboxedModule.require(MODULE_PATH, {
|
2020-02-19 06:15:37 -05:00
|
|
|
requires: {
|
2022-06-06 07:41:36 -04:00
|
|
|
'@overleaf/settings': this.Settings,
|
|
|
|
'./CommandRunner': this.CommandRunner,
|
|
|
|
fs: this.fs,
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:15:37 -05:00
|
|
|
this.directory = '/local/compile/directory'
|
|
|
|
this.mainFile = 'main-file.tex'
|
|
|
|
this.compiler = 'pdflatex'
|
|
|
|
this.image = 'example.com/image'
|
2020-06-11 11:01:44 -04:00
|
|
|
this.compileGroup = 'compile-group'
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.project_id = 'project-id-123'
|
2022-06-06 07:41:07 -04:00
|
|
|
this.env = { foo: '123' }
|
2022-06-06 07:41:36 -04:00
|
|
|
this.timeout = 42000
|
|
|
|
this.flags = []
|
|
|
|
this.stopOnFirstError = false
|
2022-07-18 10:11:30 -04:00
|
|
|
this.stats = {}
|
|
|
|
this.timings = {}
|
2022-06-06 07:41:36 -04:00
|
|
|
|
|
|
|
this.call = function (callback) {
|
|
|
|
this.LatexRunner.runLatex(
|
|
|
|
this.project_id,
|
|
|
|
{
|
|
|
|
directory: this.directory,
|
|
|
|
mainFile: this.mainFile,
|
|
|
|
compiler: this.compiler,
|
|
|
|
timeout: this.timeout,
|
|
|
|
image: this.image,
|
|
|
|
environment: this.env,
|
|
|
|
compileGroup: this.compileGroup,
|
|
|
|
flags: this.flags,
|
|
|
|
stopOnFirstError: this.stopOnFirstError,
|
2022-07-18 10:11:30 -04:00
|
|
|
timings: this.timings,
|
|
|
|
stats: this.stats,
|
2022-06-06 07:41:36 -04:00
|
|
|
},
|
2022-07-18 10:11:30 -04:00
|
|
|
callback
|
2022-06-06 07:41:36 -04:00
|
|
|
)
|
|
|
|
}
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
describe('runLatex', function () {
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('normally', function () {
|
2021-05-19 06:17:08 -04:00
|
|
|
beforeEach(function (done) {
|
2022-06-06 07:41:36 -04:00
|
|
|
this.call(done)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should run the latex command', function () {
|
2022-06-06 07:41:36 -04:00
|
|
|
this.CommandRunner.run.should.have.been.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
[
|
|
|
|
'latexmk',
|
|
|
|
'-cd',
|
|
|
|
'-jobname=output',
|
|
|
|
'-auxdir=$COMPILE_DIR',
|
|
|
|
'-outdir=$COMPILE_DIR',
|
|
|
|
'-synctex=1',
|
|
|
|
'-interaction=batchmode',
|
|
|
|
'-f',
|
|
|
|
'-pdf',
|
|
|
|
'$COMPILE_DIR/main-file.tex',
|
|
|
|
],
|
|
|
|
this.directory,
|
|
|
|
this.image,
|
|
|
|
this.timeout,
|
|
|
|
this.env,
|
|
|
|
this.compileGroup
|
|
|
|
)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
2020-05-20 06:52:53 -04:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should record the stdout and stderr', function () {
|
2022-06-06 07:41:36 -04:00
|
|
|
this.fs.writeFile.should.have.been.calledWith(
|
|
|
|
this.directory + '/' + 'output.stdout',
|
|
|
|
'this is stdout'
|
|
|
|
)
|
|
|
|
this.fs.writeFile.should.have.been.calledWith(
|
|
|
|
this.directory + '/' + 'output.stderr',
|
|
|
|
'this is stderr'
|
|
|
|
)
|
2020-05-20 06:52:53 -04:00
|
|
|
})
|
2021-05-19 06:17:08 -04:00
|
|
|
|
|
|
|
it('should not record cpu metrics', function () {
|
|
|
|
expect(this.timings['cpu-percent']).to.not.exist
|
|
|
|
expect(this.timings['cpu-time']).to.not.exist
|
|
|
|
expect(this.timings['sys-time']).to.not.exist
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-06-06 07:41:36 -04:00
|
|
|
describe('with a different compiler', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.compiler = 'lualatex'
|
|
|
|
this.call(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set the appropriate latexmk flag', function () {
|
|
|
|
this.CommandRunner.run.should.have.been.calledWith(this.project_id, [
|
|
|
|
'latexmk',
|
|
|
|
'-cd',
|
|
|
|
'-jobname=output',
|
|
|
|
'-auxdir=$COMPILE_DIR',
|
|
|
|
'-outdir=$COMPILE_DIR',
|
|
|
|
'-synctex=1',
|
|
|
|
'-interaction=batchmode',
|
|
|
|
'-f',
|
|
|
|
'-lualatex',
|
|
|
|
'$COMPILE_DIR/main-file.tex',
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-05-19 06:17:08 -04:00
|
|
|
describe('with time -v', function () {
|
|
|
|
beforeEach(function (done) {
|
2022-06-06 07:41:36 -04:00
|
|
|
this.commandRunnerOutput.stderr =
|
|
|
|
'\tCommand being timed: "sh -c timeout 1 yes > /dev/null"\n' +
|
|
|
|
'\tUser time (seconds): 0.28\n' +
|
|
|
|
'\tSystem time (seconds): 0.70\n' +
|
|
|
|
'\tPercent of CPU this job got: 98%\n'
|
|
|
|
this.call(done)
|
2021-05-19 06:17:08 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should record cpu metrics', function () {
|
|
|
|
expect(this.timings['cpu-percent']).to.equal(98)
|
|
|
|
expect(this.timings['cpu-time']).to.equal(0.28)
|
|
|
|
expect(this.timings['sys-time']).to.equal(0.7)
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with an .Rtex main file', function () {
|
2022-06-06 07:41:36 -04:00
|
|
|
beforeEach(function (done) {
|
|
|
|
this.mainFile = 'main-file.Rtex'
|
|
|
|
this.call(done)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
it('should run the latex command on the equivalent .tex file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
const command = this.CommandRunner.run.args[0][1]
|
|
|
|
const mainFile = command.slice(-1)[0]
|
2022-06-06 07:41:07 -04:00
|
|
|
mainFile.should.equal('$COMPILE_DIR/main-file.tex')
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
describe('with a flags option', function () {
|
2022-06-06 07:41:36 -04:00
|
|
|
beforeEach(function (done) {
|
|
|
|
this.flags = ['-shell-restricted', '-halt-on-error']
|
|
|
|
this.call(done)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
2019-05-10 11:51:40 -04:00
|
|
|
|
2022-06-06 07:41:07 -04:00
|
|
|
it('should include the flags in the command', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
const command = this.CommandRunner.run.args[0][1]
|
|
|
|
const flags = command.filter(
|
2021-09-21 04:56:44 -04:00
|
|
|
arg => arg === '-shell-restricted' || arg === '-halt-on-error'
|
2020-02-19 06:15:37 -05:00
|
|
|
)
|
|
|
|
flags.length.should.equal(2)
|
2021-09-21 04:56:44 -04:00
|
|
|
flags[0].should.equal('-shell-restricted')
|
2022-06-06 07:41:07 -04:00
|
|
|
flags[1].should.equal('-halt-on-error')
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
2022-06-06 07:41:36 -04:00
|
|
|
|
|
|
|
describe('with the stopOnFirstError option', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.stopOnFirstError = true
|
|
|
|
this.call(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set the appropriate flags', function () {
|
|
|
|
this.CommandRunner.run.should.have.been.calledWith(this.project_id, [
|
|
|
|
'latexmk',
|
|
|
|
'-cd',
|
|
|
|
'-jobname=output',
|
|
|
|
'-auxdir=$COMPILE_DIR',
|
|
|
|
'-outdir=$COMPILE_DIR',
|
|
|
|
'-synctex=1',
|
|
|
|
'-interaction=batchmode',
|
|
|
|
'-halt-on-error',
|
|
|
|
'-pdf',
|
|
|
|
'$COMPILE_DIR/main-file.tex',
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|