2014-02-12 12:27:43 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
require('chai').should()
|
|
|
|
modulePath = require('path').join __dirname, '../../../app/js/CompileManager'
|
|
|
|
tk = require("timekeeper")
|
2014-04-02 07:53:02 -04:00
|
|
|
EventEmitter = require("events").EventEmitter
|
2014-04-08 10:18:56 -04:00
|
|
|
Path = require "path"
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
describe "CompileManager", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"./LatexRunner": @LatexRunner = {}
|
|
|
|
"./ResourceWriter": @ResourceWriter = {}
|
|
|
|
"./OutputFileFinder": @OutputFileFinder = {}
|
2015-02-26 10:30:57 -05:00
|
|
|
"./OutputCacheManager": @OutputCacheManager = {}
|
2018-03-01 08:55:55 -05:00
|
|
|
"settings-sharelatex": @Settings =
|
|
|
|
path:
|
|
|
|
compilesDir: "/compiles/dir"
|
|
|
|
synctexBaseDir: -> "/compile"
|
|
|
|
clsi:
|
|
|
|
docker:
|
|
|
|
image: "SOMEIMAGE"
|
|
|
|
|
2016-05-24 09:12:02 -04:00
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub() , info:->}
|
2014-04-02 07:53:02 -04:00
|
|
|
"child_process": @child_process = {}
|
2015-09-08 09:19:46 -04:00
|
|
|
"./CommandRunner": @CommandRunner = {}
|
2016-02-02 09:26:14 -05:00
|
|
|
"./DraftModeManager": @DraftModeManager = {}
|
2017-07-18 06:30:22 -04:00
|
|
|
"./TikzManager": @TikzManager = {}
|
2017-09-22 11:19:33 -04:00
|
|
|
"./LockManager": @LockManager = {}
|
2015-09-08 09:19:46 -04:00
|
|
|
"fs": @fs = {}
|
2017-09-25 10:28:31 -04:00
|
|
|
"fs-extra": @fse = { ensureDir: sinon.stub().callsArg(1) }
|
2014-02-12 12:27:43 -05:00
|
|
|
@callback = sinon.stub()
|
2018-03-01 08:55:55 -05:00
|
|
|
@project_id = "project-id-123"
|
|
|
|
@user_id = "1234"
|
2017-09-22 11:19:33 -04:00
|
|
|
describe "doCompileWithLock", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request =
|
|
|
|
resources: @resources = "mock-resources"
|
2018-03-01 08:55:55 -05:00
|
|
|
project_id: @project_id
|
|
|
|
user_id: @user_id
|
2017-09-22 11:19:33 -04:00
|
|
|
@output_files = ["foo", "bar"]
|
2017-09-25 10:28:31 -04:00
|
|
|
@Settings.compileDir = "compiles"
|
|
|
|
@compileDir = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
|
2017-09-22 11:19:33 -04:00
|
|
|
@CompileManager.doCompile = sinon.stub().callsArgWith(1, null, @output_files)
|
|
|
|
@LockManager.runWithLock = (lockFile, runner, callback) ->
|
|
|
|
runner (err, result...) ->
|
|
|
|
callback(err, result...)
|
|
|
|
|
|
|
|
describe "when the project is not locked", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager.doCompileWithLock @request, @callback
|
|
|
|
|
2017-09-25 10:28:31 -04:00
|
|
|
it "should ensure that the compile directory exists", ->
|
|
|
|
@fse.ensureDir.calledWith(@compileDir)
|
|
|
|
.should.equal true
|
|
|
|
|
2017-09-22 11:19:33 -04:00
|
|
|
it "should call doCompile with the request", ->
|
|
|
|
@CompileManager.doCompile
|
|
|
|
.calledWith(@request)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the output files", ->
|
|
|
|
@callback.calledWithExactly(null, @output_files)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when the project is locked", ->
|
|
|
|
beforeEach ->
|
|
|
|
@error = new Error("locked")
|
|
|
|
@LockManager.runWithLock = (lockFile, runner, callback) =>
|
|
|
|
callback(@error)
|
|
|
|
@CompileManager.doCompileWithLock @request, @callback
|
|
|
|
|
2017-09-25 10:28:31 -04:00
|
|
|
it "should ensure that the compile directory exists", ->
|
|
|
|
@fse.ensureDir.calledWith(@compileDir)
|
|
|
|
.should.equal true
|
|
|
|
|
2017-09-22 11:19:33 -04:00
|
|
|
it "should not call doCompile with the request", ->
|
|
|
|
@CompileManager.doCompile
|
|
|
|
.called.should.equal false
|
|
|
|
|
|
|
|
it "should call the callback with the error", ->
|
|
|
|
@callback.calledWithExactly(@error)
|
|
|
|
.should.equal true
|
|
|
|
|
2014-02-12 12:27:43 -05:00
|
|
|
describe "doCompile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@output_files = [{
|
|
|
|
path: "output.log"
|
|
|
|
type: "log"
|
|
|
|
}, {
|
|
|
|
path: "output.pdf"
|
|
|
|
type: "pdf"
|
|
|
|
}]
|
2015-02-26 10:30:57 -05:00
|
|
|
@build_files = [{
|
|
|
|
path: "output.log"
|
|
|
|
type: "log"
|
|
|
|
build: 1234
|
|
|
|
}, {
|
|
|
|
path: "output.pdf"
|
|
|
|
type: "pdf"
|
|
|
|
build: 1234
|
|
|
|
}]
|
2014-02-12 12:27:43 -05:00
|
|
|
@request =
|
|
|
|
resources: @resources = "mock-resources"
|
|
|
|
rootResourcePath: @rootResourcePath = "main.tex"
|
2018-03-01 08:55:55 -05:00
|
|
|
project_id: @project_id
|
|
|
|
user_id: @user_id
|
2014-02-12 12:27:43 -05:00
|
|
|
compiler: @compiler = "pdflatex"
|
|
|
|
timeout: @timeout = 42000
|
2016-01-12 12:04:42 -05:00
|
|
|
imageName: @image = "example.com/image"
|
2016-07-26 07:30:29 -04:00
|
|
|
@env = {}
|
2014-02-12 12:27:43 -05:00
|
|
|
@Settings.compileDir = "compiles"
|
2016-05-27 10:29:26 -04:00
|
|
|
@compileDir = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
|
2017-08-17 09:53:35 -04:00
|
|
|
@ResourceWriter.syncResourcesToDisk = sinon.stub().callsArgWith(2, null, @resources)
|
2014-02-12 12:27:43 -05:00
|
|
|
@LatexRunner.runLatex = sinon.stub().callsArg(2)
|
|
|
|
@OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files)
|
2015-02-26 10:30:57 -05:00
|
|
|
@OutputCacheManager.saveOutputFiles = sinon.stub().callsArgWith(2, null, @build_files)
|
2016-02-02 09:26:14 -05:00
|
|
|
@DraftModeManager.injectDraftMode = sinon.stub().callsArg(1)
|
2017-09-29 12:00:53 -04:00
|
|
|
@TikzManager.checkMainFile = sinon.stub().callsArg(3, false)
|
2016-02-02 09:26:14 -05:00
|
|
|
|
|
|
|
describe "normally", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager.doCompile @request, @callback
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2016-02-02 09:26:14 -05:00
|
|
|
it "should write the resources to disk", ->
|
|
|
|
@ResourceWriter.syncResourcesToDisk
|
2017-08-07 10:00:16 -04:00
|
|
|
.calledWith(@request, @compileDir)
|
2016-02-02 09:26:14 -05:00
|
|
|
.should.equal true
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2016-02-02 09:26:14 -05:00
|
|
|
it "should run LaTeX", ->
|
|
|
|
@LatexRunner.runLatex
|
2016-05-27 10:29:26 -04:00
|
|
|
.calledWith("#{@project_id}-#{@user_id}", {
|
2016-02-02 09:26:14 -05:00
|
|
|
directory: @compileDir
|
|
|
|
mainFile: @rootResourcePath
|
|
|
|
compiler: @compiler
|
|
|
|
timeout: @timeout
|
|
|
|
image: @image
|
2016-07-26 07:30:29 -04:00
|
|
|
environment: @env
|
2016-02-02 09:26:14 -05:00
|
|
|
})
|
|
|
|
.should.equal true
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2016-02-02 09:26:14 -05:00
|
|
|
it "should find the output files", ->
|
|
|
|
@OutputFileFinder.findOutputFiles
|
|
|
|
.calledWith(@resources, @compileDir)
|
|
|
|
.should.equal true
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2016-02-02 09:26:14 -05:00
|
|
|
it "should return the output files", ->
|
|
|
|
@callback.calledWith(null, @build_files).should.equal true
|
|
|
|
|
|
|
|
it "should not inject draft mode by default", ->
|
|
|
|
@DraftModeManager.injectDraftMode.called.should.equal false
|
|
|
|
|
|
|
|
describe "with draft mode", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.draft = true
|
|
|
|
@CompileManager.doCompile @request, @callback
|
|
|
|
|
|
|
|
it "should inject the draft mode header", ->
|
|
|
|
@DraftModeManager.injectDraftMode
|
|
|
|
.calledWith(@compileDir + "/" + @rootResourcePath)
|
|
|
|
.should.equal true
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2016-09-12 11:28:52 -04:00
|
|
|
describe "with a check option", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.check = "error"
|
|
|
|
@CompileManager.doCompile @request, @callback
|
|
|
|
|
|
|
|
it "should run chktex", ->
|
|
|
|
@LatexRunner.runLatex
|
|
|
|
.calledWith("#{@project_id}-#{@user_id}", {
|
|
|
|
directory: @compileDir
|
|
|
|
mainFile: @rootResourcePath
|
|
|
|
compiler: @compiler
|
|
|
|
timeout: @timeout
|
|
|
|
image: @image
|
2016-09-28 06:02:58 -04:00
|
|
|
environment: {'CHKTEX_OPTIONS': '-nall -e9 -e10 -w15 -w16', 'CHKTEX_EXIT_ON_ERROR':1, 'CHKTEX_ULIMIT_OPTIONS': '-t 5 -v 64000'}
|
2016-09-12 11:28:52 -04:00
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "with a knitr file and check options", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.rootResourcePath = "main.Rtex"
|
|
|
|
@request.check = "error"
|
|
|
|
@CompileManager.doCompile @request, @callback
|
|
|
|
|
|
|
|
it "should not run chktex", ->
|
|
|
|
@LatexRunner.runLatex
|
|
|
|
.calledWith("#{@project_id}-#{@user_id}", {
|
|
|
|
directory: @compileDir
|
|
|
|
mainFile: "main.Rtex"
|
|
|
|
compiler: @compiler
|
|
|
|
timeout: @timeout
|
|
|
|
image: @image
|
|
|
|
environment: @env
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
2014-02-12 12:27:43 -05:00
|
|
|
describe "clearProject", ->
|
2014-04-02 07:53:02 -04:00
|
|
|
describe "succesfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@Settings.compileDir = "compiles"
|
2016-03-31 06:43:36 -04:00
|
|
|
@fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
|
2014-04-02 07:53:02 -04:00
|
|
|
@proc = new EventEmitter()
|
|
|
|
@proc.stdout = new EventEmitter()
|
|
|
|
@proc.stderr = new EventEmitter()
|
|
|
|
@child_process.spawn = sinon.stub().returns(@proc)
|
2016-05-27 10:31:44 -04:00
|
|
|
@CompileManager.clearProject @project_id, @user_id, @callback
|
2014-04-02 07:53:02 -04:00
|
|
|
@proc.emit "close", 0
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2014-04-02 07:53:02 -04:00
|
|
|
it "should remove the project directory", ->
|
|
|
|
@child_process.spawn
|
2016-05-27 10:31:44 -04:00
|
|
|
.calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"])
|
2014-04-02 07:53:02 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "with a non-success status code", ->
|
|
|
|
beforeEach ->
|
|
|
|
@Settings.compileDir = "compiles"
|
2016-03-31 06:43:36 -04:00
|
|
|
@fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
|
2014-04-02 07:53:02 -04:00
|
|
|
@proc = new EventEmitter()
|
|
|
|
@proc.stdout = new EventEmitter()
|
|
|
|
@proc.stderr = new EventEmitter()
|
|
|
|
@child_process.spawn = sinon.stub().returns(@proc)
|
2016-05-27 10:31:44 -04:00
|
|
|
@CompileManager.clearProject @project_id, @user_id, @callback
|
2014-04-02 07:53:02 -04:00
|
|
|
@proc.stderr.emit "data", @error = "oops"
|
|
|
|
@proc.emit "close", 1
|
|
|
|
|
|
|
|
it "should remove the project directory", ->
|
|
|
|
@child_process.spawn
|
2016-05-27 10:31:44 -04:00
|
|
|
.calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"])
|
2014-04-02 07:53:02 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with an error from the stderr", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(new Error())
|
|
|
|
.should.equal true
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2016-05-27 10:31:44 -04:00
|
|
|
@callback.args[0][0].message.should.equal "rm -r #{@Settings.path.compilesDir}/#{@project_id}-#{@user_id} failed: #{@error}"
|
2014-04-08 10:18:56 -04:00
|
|
|
|
|
|
|
describe "syncing", ->
|
|
|
|
beforeEach ->
|
|
|
|
@page = 1
|
|
|
|
@h = 42.23
|
|
|
|
@v = 87.56
|
|
|
|
@width = 100.01
|
|
|
|
@height = 234.56
|
|
|
|
@line = 5
|
|
|
|
@column = 3
|
|
|
|
@file_name = "main.tex"
|
2014-04-09 10:34:54 -04:00
|
|
|
@child_process.execFile = sinon.stub()
|
2016-05-27 10:29:26 -04:00
|
|
|
@Settings.path.synctexBaseDir = (project_id) => "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
|
2014-04-08 10:18:56 -04:00
|
|
|
|
|
|
|
describe "syncFromCode", ->
|
|
|
|
beforeEach ->
|
2017-05-24 05:03:53 -04:00
|
|
|
@fs.stat = sinon.stub().callsArgWith(1, null,{isFile: ()->true})
|
2018-03-01 08:55:55 -05:00
|
|
|
@stdout = "NODE\t#{@page}\t#{@h}\t#{@v}\t#{@width}\t#{@height}\n"
|
|
|
|
@CommandRunner.run = sinon.stub().callsArgWith(6, null, {stdout:@stdout})
|
2016-05-27 10:29:26 -04:00
|
|
|
@CompileManager.syncFromCode @project_id, @user_id, @file_name, @line, @column, @callback
|
2014-04-08 10:18:56 -04:00
|
|
|
|
2018-03-06 08:06:10 -05:00
|
|
|
it "should execute the synctex binary", ->
|
|
|
|
bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
|
|
|
|
synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/output.pdf"
|
|
|
|
file_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/#{@file_name}"
|
|
|
|
@CommandRunner.run
|
|
|
|
.calledWith(
|
|
|
|
"#{@project_id}-#{@user_id}",
|
|
|
|
['/opt/synctex', 'code', synctex_path, file_path, @line, @column],
|
|
|
|
"#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}",
|
|
|
|
@Settings.clsi.docker.image,
|
|
|
|
10000,
|
|
|
|
{}
|
|
|
|
).should.equal true
|
2014-04-08 10:18:56 -04:00
|
|
|
|
|
|
|
it "should call the callback with the parsed output", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(null, [{
|
|
|
|
page: @page
|
|
|
|
h: @h
|
|
|
|
v: @v
|
|
|
|
height: @height
|
|
|
|
width: @width
|
|
|
|
}])
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "syncFromPdf", ->
|
|
|
|
beforeEach ->
|
2017-05-24 05:03:53 -04:00
|
|
|
@fs.stat = sinon.stub().callsArgWith(1, null,{isFile: ()->true})
|
2018-03-01 08:55:55 -05:00
|
|
|
@stdout = "NODE\t#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/#{@file_name}\t#{@line}\t#{@column}\n"
|
|
|
|
@CommandRunner.run = sinon.stub().callsArgWith(6, null, {stdout:@stdout})
|
2016-05-27 10:29:26 -04:00
|
|
|
@CompileManager.syncFromPdf @project_id, @user_id, @page, @h, @v, @callback
|
2014-04-08 10:18:56 -04:00
|
|
|
|
2018-03-06 08:06:10 -05:00
|
|
|
it "should execute the synctex binary", ->
|
|
|
|
bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
|
|
|
|
synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/output.pdf"
|
|
|
|
@CommandRunner.run
|
|
|
|
.calledWith(
|
|
|
|
"#{@project_id}-#{@user_id}",
|
|
|
|
['/opt/synctex', "pdf", synctex_path, @page, @h, @v],
|
|
|
|
"#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}",
|
|
|
|
@Settings.clsi.docker.image,
|
|
|
|
10000,
|
|
|
|
{}).should.equal true
|
2014-04-08 10:18:56 -04:00
|
|
|
|
|
|
|
it "should call the callback with the parsed output", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(null, [{
|
|
|
|
file: @file_name
|
|
|
|
line: @line
|
|
|
|
column: @column
|
|
|
|
}])
|
2015-06-08 18:27:47 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "wordcount", ->
|
|
|
|
beforeEach ->
|
2016-08-11 05:31:37 -04:00
|
|
|
@CommandRunner.run = sinon.stub().callsArg(6)
|
|
|
|
@fs.readFile = sinon.stub().callsArgWith(2, null, @stdout = "Encoding: ascii\nWords in text: 2")
|
2015-09-08 09:19:46 -04:00
|
|
|
@callback = sinon.stub()
|
|
|
|
|
2018-03-01 08:55:55 -05:00
|
|
|
@project_id
|
2015-09-08 09:19:46 -04:00
|
|
|
@timeout = 10 * 1000
|
2015-06-08 18:27:47 -04:00
|
|
|
@file_name = "main.tex"
|
2015-09-08 09:19:46 -04:00
|
|
|
@Settings.path.compilesDir = "/local/compile/directory"
|
2016-01-19 09:12:41 -05:00
|
|
|
@image = "example.com/image"
|
2015-09-08 09:19:46 -04:00
|
|
|
|
2016-05-27 10:29:26 -04:00
|
|
|
@CompileManager.wordcount @project_id, @user_id, @file_name, @image, @callback
|
2015-06-08 18:27:47 -04:00
|
|
|
|
2015-09-08 09:19:46 -04:00
|
|
|
it "should run the texcount command", ->
|
2016-05-27 10:29:26 -04:00
|
|
|
@directory = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
|
2015-09-08 09:19:46 -04:00
|
|
|
@file_path = "$COMPILE_DIR/#{@file_name}"
|
2016-08-11 05:31:37 -04:00
|
|
|
@command =[ "texcount", "-nocol", "-inc", @file_path, "-out=" + @file_path + ".wc"]
|
2015-09-08 09:19:46 -04:00
|
|
|
|
|
|
|
@CommandRunner.run
|
2016-08-11 05:31:37 -04:00
|
|
|
.calledWith("#{@project_id}-#{@user_id}", @command, @directory, @image, @timeout, {})
|
2015-06-08 18:27:47 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with the parsed output", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(null, {
|
|
|
|
encode: "ascii"
|
|
|
|
textWords: 2
|
|
|
|
headWords: 0
|
|
|
|
outside: 0
|
|
|
|
headers: 0
|
|
|
|
elements: 0
|
|
|
|
mathInline: 0
|
|
|
|
mathDisplay: 0
|
2016-08-11 05:31:37 -04:00
|
|
|
errors: 0
|
|
|
|
messages: ""
|
2015-06-08 18:27:47 -04:00
|
|
|
})
|
|
|
|
.should.equal true
|