CompileManagerTests.coffee 11 KB

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