CompileManagerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/CompileManager'
  5. tk = require("timekeeper")
  6. EventEmitter = require("events").EventEmitter
  7. Path = require "path"
  8. describe "CompileManager", ->
  9. beforeEach ->
  10. @CompileManager = SandboxedModule.require modulePath, requires:
  11. "./LatexRunner": @LatexRunner = {}
  12. "./ResourceWriter": @ResourceWriter = {}
  13. "./OutputFileFinder": @OutputFileFinder = {}
  14. "./OutputCacheManager": @OutputCacheManager = {}
  15. "settings-sharelatex": @Settings = { path: compilesDir: "/compiles/dir" }
  16. "logger-sharelatex": @logger = { log: sinon.stub() , info:->}
  17. "child_process": @child_process = {}
  18. "./CommandRunner": @CommandRunner = {}
  19. "./DraftModeManager": @DraftModeManager = {}
  20. "./TikzManager": @TikzManager = {}
  21. "./LockManager": @LockManager = {}
  22. "fs": @fs = {}
  23. "fs-extra": @fse = { ensureDir: sinon.stub().callsArg(1) }
  24. @callback = sinon.stub()
  25. describe "doCompileWithLock", ->
  26. beforeEach ->
  27. @request =
  28. resources: @resources = "mock-resources"
  29. project_id: @project_id = "project-id-123"
  30. user_id: @user_id = "1234"
  31. @output_files = ["foo", "bar"]
  32. @Settings.compileDir = "compiles"
  33. @compileDir = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
  34. @CompileManager.doCompile = sinon.stub().callsArgWith(1, null, @output_files)
  35. @LockManager.runWithLock = (lockFile, runner, callback) ->
  36. runner (err, result...) ->
  37. callback(err, result...)
  38. describe "when the project is not locked", ->
  39. beforeEach ->
  40. @CompileManager.doCompileWithLock @request, @callback
  41. it "should ensure that the compile directory exists", ->
  42. @fse.ensureDir.calledWith(@compileDir)
  43. .should.equal true
  44. it "should call doCompile with the request", ->
  45. @CompileManager.doCompile
  46. .calledWith(@request)
  47. .should.equal true
  48. it "should call the callback with the output files", ->
  49. @callback.calledWithExactly(null, @output_files)
  50. .should.equal true
  51. describe "when the project is locked", ->
  52. beforeEach ->
  53. @error = new Error("locked")
  54. @LockManager.runWithLock = (lockFile, runner, callback) =>
  55. callback(@error)
  56. @CompileManager.doCompileWithLock @request, @callback
  57. it "should ensure that the compile directory exists", ->
  58. @fse.ensureDir.calledWith(@compileDir)
  59. .should.equal true
  60. it "should not call doCompile with the request", ->
  61. @CompileManager.doCompile
  62. .called.should.equal false
  63. it "should call the callback with the error", ->
  64. @callback.calledWithExactly(@error)
  65. .should.equal true
  66. describe "doCompile", ->
  67. beforeEach ->
  68. @output_files = [{
  69. path: "output.log"
  70. type: "log"
  71. }, {
  72. path: "output.pdf"
  73. type: "pdf"
  74. }]
  75. @build_files = [{
  76. path: "output.log"
  77. type: "log"
  78. build: 1234
  79. }, {
  80. path: "output.pdf"
  81. type: "pdf"
  82. build: 1234
  83. }]
  84. @request =
  85. resources: @resources = "mock-resources"
  86. rootResourcePath: @rootResourcePath = "main.tex"
  87. project_id: @project_id = "project-id-123"
  88. user_id: @user_id = "1234"
  89. compiler: @compiler = "pdflatex"
  90. timeout: @timeout = 42000
  91. imageName: @image = "example.com/image"
  92. @env = {}
  93. @Settings.compileDir = "compiles"
  94. @compileDir = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
  95. @ResourceWriter.syncResourcesToDisk = sinon.stub().callsArgWith(2, null, @resources)
  96. @LatexRunner.runLatex = sinon.stub().callsArg(2)
  97. @OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files)
  98. @OutputCacheManager.saveOutputFiles = sinon.stub().callsArgWith(2, null, @build_files)
  99. @DraftModeManager.injectDraftMode = sinon.stub().callsArg(1)
  100. @TikzManager.checkMainFile = sinon.stub().callsArg(3, false)
  101. describe "normally", ->
  102. beforeEach ->
  103. @CompileManager.doCompile @request, @callback
  104. it "should write the resources to disk", ->
  105. @ResourceWriter.syncResourcesToDisk
  106. .calledWith(@request, @compileDir)
  107. .should.equal true
  108. it "should run LaTeX", ->
  109. @LatexRunner.runLatex
  110. .calledWith("#{@project_id}-#{@user_id}", {
  111. directory: @compileDir
  112. mainFile: @rootResourcePath
  113. compiler: @compiler
  114. timeout: @timeout
  115. image: @image
  116. environment: @env
  117. })
  118. .should.equal true
  119. it "should find the output files", ->
  120. @OutputFileFinder.findOutputFiles
  121. .calledWith(@resources, @compileDir)
  122. .should.equal true
  123. it "should return the output files", ->
  124. @callback.calledWith(null, @build_files).should.equal true
  125. it "should not inject draft mode by default", ->
  126. @DraftModeManager.injectDraftMode.called.should.equal false
  127. describe "with draft mode", ->
  128. beforeEach ->
  129. @request.draft = true
  130. @CompileManager.doCompile @request, @callback
  131. it "should inject the draft mode header", ->
  132. @DraftModeManager.injectDraftMode
  133. .calledWith(@compileDir + "/" + @rootResourcePath)
  134. .should.equal true
  135. describe "with a check option", ->
  136. beforeEach ->
  137. @request.check = "error"
  138. @CompileManager.doCompile @request, @callback
  139. it "should run chktex", ->
  140. @LatexRunner.runLatex
  141. .calledWith("#{@project_id}-#{@user_id}", {
  142. directory: @compileDir
  143. mainFile: @rootResourcePath
  144. compiler: @compiler
  145. timeout: @timeout
  146. image: @image
  147. environment: {'CHKTEX_OPTIONS': '-nall -e9 -e10 -w15 -w16', 'CHKTEX_EXIT_ON_ERROR':1, 'CHKTEX_ULIMIT_OPTIONS': '-t 5 -v 64000'}
  148. })
  149. .should.equal true
  150. describe "with a knitr file and check options", ->
  151. beforeEach ->
  152. @request.rootResourcePath = "main.Rtex"
  153. @request.check = "error"
  154. @CompileManager.doCompile @request, @callback
  155. it "should not run chktex", ->
  156. @LatexRunner.runLatex
  157. .calledWith("#{@project_id}-#{@user_id}", {
  158. directory: @compileDir
  159. mainFile: "main.Rtex"
  160. compiler: @compiler
  161. timeout: @timeout
  162. image: @image
  163. environment: @env
  164. })
  165. .should.equal true
  166. describe "clearProject", ->
  167. describe "succesfully", ->
  168. beforeEach ->
  169. @Settings.compileDir = "compiles"
  170. @fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
  171. @proc = new EventEmitter()
  172. @proc.stdout = new EventEmitter()
  173. @proc.stderr = new EventEmitter()
  174. @child_process.spawn = sinon.stub().returns(@proc)
  175. @CompileManager.clearProject @project_id, @user_id, @callback
  176. @proc.emit "close", 0
  177. it "should remove the project directory", ->
  178. @child_process.spawn
  179. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"])
  180. .should.equal true
  181. it "should call the callback", ->
  182. @callback.called.should.equal true
  183. describe "with a non-success status code", ->
  184. beforeEach ->
  185. @Settings.compileDir = "compiles"
  186. @fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
  187. @proc = new EventEmitter()
  188. @proc.stdout = new EventEmitter()
  189. @proc.stderr = new EventEmitter()
  190. @child_process.spawn = sinon.stub().returns(@proc)
  191. @CompileManager.clearProject @project_id, @user_id, @callback
  192. @proc.stderr.emit "data", @error = "oops"
  193. @proc.emit "close", 1
  194. it "should remove the project directory", ->
  195. @child_process.spawn
  196. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"])
  197. .should.equal true
  198. it "should call the callback with an error from the stderr", ->
  199. @callback
  200. .calledWith(new Error())
  201. .should.equal true
  202. @callback.args[0][0].message.should.equal "rm -r #{@Settings.path.compilesDir}/#{@project_id}-#{@user_id} failed: #{@error}"
  203. describe "syncing", ->
  204. beforeEach ->
  205. @page = 1
  206. @h = 42.23
  207. @v = 87.56
  208. @width = 100.01
  209. @height = 234.56
  210. @line = 5
  211. @column = 3
  212. @file_name = "main.tex"
  213. @child_process.execFile = sinon.stub()
  214. @Settings.path.synctexBaseDir = (project_id) => "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
  215. describe "syncFromCode", ->
  216. beforeEach ->
  217. @fs.stat = sinon.stub().callsArgWith(1, null,{isFile: ()->true})
  218. @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@page}\t#{@h}\t#{@v}\t#{@width}\t#{@height}\n", "")
  219. @CompileManager.syncFromCode @project_id, @user_id, @file_name, @line, @column, @callback
  220. it "should execute the synctex binary", ->
  221. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  222. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/output.pdf"
  223. file_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/#{@file_name}"
  224. @child_process.execFile
  225. .calledWith(bin_path, ["code", synctex_path, file_path, @line, @column], timeout: 10000)
  226. .should.equal true
  227. it "should call the callback with the parsed output", ->
  228. @callback
  229. .calledWith(null, [{
  230. page: @page
  231. h: @h
  232. v: @v
  233. height: @height
  234. width: @width
  235. }])
  236. .should.equal true
  237. describe "syncFromPdf", ->
  238. beforeEach ->
  239. @fs.stat = sinon.stub().callsArgWith(1, null,{isFile: ()->true})
  240. @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/#{@file_name}\t#{@line}\t#{@column}\n", "")
  241. @CompileManager.syncFromPdf @project_id, @user_id, @page, @h, @v, @callback
  242. it "should execute the synctex binary", ->
  243. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  244. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/output.pdf"
  245. @child_process.execFile
  246. .calledWith(bin_path, ["pdf", synctex_path, @page, @h, @v], timeout: 10000)
  247. .should.equal true
  248. it "should call the callback with the parsed output", ->
  249. @callback
  250. .calledWith(null, [{
  251. file: @file_name
  252. line: @line
  253. column: @column
  254. }])
  255. .should.equal true
  256. describe "wordcount", ->
  257. beforeEach ->
  258. @CommandRunner.run = sinon.stub().callsArg(6)
  259. @fs.readFile = sinon.stub().callsArgWith(2, null, @stdout = "Encoding: ascii\nWords in text: 2")
  260. @callback = sinon.stub()
  261. @project_id = "project-id-123"
  262. @timeout = 10 * 1000
  263. @file_name = "main.tex"
  264. @Settings.path.compilesDir = "/local/compile/directory"
  265. @image = "example.com/image"
  266. @CompileManager.wordcount @project_id, @user_id, @file_name, @image, @callback
  267. it "should run the texcount command", ->
  268. @directory = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
  269. @file_path = "$COMPILE_DIR/#{@file_name}"
  270. @command =[ "texcount", "-nocol", "-inc", @file_path, "-out=" + @file_path + ".wc"]
  271. @CommandRunner.run
  272. .calledWith("#{@project_id}-#{@user_id}", @command, @directory, @image, @timeout, {})
  273. .should.equal true
  274. it "should call the callback with the parsed output", ->
  275. @callback
  276. .calledWith(null, {
  277. encode: "ascii"
  278. textWords: 2
  279. headWords: 0
  280. outside: 0
  281. headers: 0
  282. elements: 0
  283. mathInline: 0
  284. mathDisplay: 0
  285. errors: 0
  286. messages: ""
  287. })
  288. .should.equal true