overleaf/services/clsi/test/unit/coffee/CompileManagerTests.coffee

174 lines
5.7 KiB
CoffeeScript
Raw Normal View History

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")
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 = {}
2014-02-12 12:27:43 -05:00
"settings-sharelatex": @Settings = { path: compilesDir: "/compiles/dir" }
"logger-sharelatex": @logger = { log: sinon.stub() }
"child_process": @child_process = {}
2014-02-12 12:27:43 -05:00
@callback = sinon.stub()
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"
project_id: @project_id = "project-id-123"
compiler: @compiler = "pdflatex"
timeout: @timeout = 42000
@Settings.compileDir = "compiles"
@compileDir = "#{@Settings.path.compilesDir}/#{@project_id}"
@ResourceWriter.syncResourcesToDisk = sinon.stub().callsArg(3)
@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)
2014-02-12 12:27:43 -05:00
@CompileManager.doCompile @request, @callback
it "should write the resources to disk", ->
@ResourceWriter.syncResourcesToDisk
.calledWith(@project_id, @resources, @compileDir)
.should.equal true
it "should run LaTeX", ->
@LatexRunner.runLatex
.calledWith(@project_id, {
directory: @compileDir
mainFile: @rootResourcePath
compiler: @compiler
timeout: @timeout
})
.should.equal true
it "should find the output files", ->
@OutputFileFinder.findOutputFiles
.calledWith(@resources, @compileDir)
.should.equal true
it "should return the output files", ->
2015-02-26 10:30:57 -05:00
@callback.calledWith(null, @build_files).should.equal true
2014-02-12 12:27:43 -05:00
describe "clearProject", ->
describe "succesfully", ->
beforeEach ->
@Settings.compileDir = "compiles"
@proc = new EventEmitter()
@proc.stdout = new EventEmitter()
@proc.stderr = new EventEmitter()
@child_process.spawn = sinon.stub().returns(@proc)
@CompileManager.clearProject @project_id, @callback
@proc.emit "close", 0
2014-02-12 12:27:43 -05:00
it "should remove the project directory", ->
@child_process.spawn
.calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}"])
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
describe "with a non-success status code", ->
beforeEach ->
@Settings.compileDir = "compiles"
@proc = new EventEmitter()
@proc.stdout = new EventEmitter()
@proc.stderr = new EventEmitter()
@child_process.spawn = sinon.stub().returns(@proc)
@CompileManager.clearProject @project_id, @callback
@proc.stderr.emit "data", @error = "oops"
@proc.emit "close", 1
it "should remove the project directory", ->
@child_process.spawn
.calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}"])
.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
@callback.args[0][0].message.should.equal "rm -r #{@Settings.path.compilesDir}/#{@project_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"
@child_process.execFile = sinon.stub()
2014-04-08 10:18:56 -04:00
@Settings.path.synctexBaseDir = (project_id) => "#{@Settings.path.compilesDir}/#{@project_id}"
describe "syncFromCode", ->
beforeEach ->
@child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@page}\t#{@h}\t#{@v}\t#{@width}\t#{@height}\n", "")
2014-04-08 10:18:56 -04:00
@CompileManager.syncFromCode @project_id, @file_name, @line, @column, @callback
it "should execute the synctex binary", ->
bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}/output.pdf"
file_path = "#{@Settings.path.compilesDir}/#{@project_id}/#{@file_name}"
@child_process.execFile
.calledWith(bin_path, ["code", synctex_path, file_path, @line, @column], timeout: 10000)
2014-04-08 10:18:56 -04:00
.should.equal true
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 ->
@child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@Settings.path.compilesDir}/#{@project_id}/#{@file_name}\t#{@line}\t#{@column}\n", "")
2014-04-08 10:18:56 -04:00
@CompileManager.syncFromPdf @project_id, @page, @h, @v, @callback
it "should execute the synctex binary", ->
bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}/output.pdf"
@child_process.execFile
.calledWith(bin_path, ["pdf", synctex_path, @page, @h, @v], timeout: 10000)
2014-04-08 10:18:56 -04:00
.should.equal true
it "should call the callback with the parsed output", ->
@callback
.calledWith(null, [{
file: @file_name
line: @line
column: @column
}])
.should.equal true