CompileManagerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. @env = {}
  101. @Settings.compileDir = "compiles"
  102. @compileDir = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
  103. @ResourceWriter.syncResourcesToDisk = sinon.stub().callsArgWith(2, null, @resources)
  104. @LatexRunner.runLatex = sinon.stub().callsArg(2)
  105. @OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files)
  106. @OutputCacheManager.saveOutputFiles = sinon.stub().callsArgWith(2, null, @build_files)
  107. @DraftModeManager.injectDraftMode = sinon.stub().callsArg(1)
  108. @TikzManager.checkMainFile = sinon.stub().callsArg(3, false)
  109. describe "normally", ->
  110. beforeEach ->
  111. @CompileManager.doCompile @request, @callback
  112. it "should write the resources to disk", ->
  113. @ResourceWriter.syncResourcesToDisk
  114. .calledWith(@request, @compileDir)
  115. .should.equal true
  116. it "should run LaTeX", ->
  117. @LatexRunner.runLatex
  118. .calledWith("#{@project_id}-#{@user_id}", {
  119. directory: @compileDir
  120. mainFile: @rootResourcePath
  121. compiler: @compiler
  122. timeout: @timeout
  123. image: @image
  124. environment: @env
  125. })
  126. .should.equal true
  127. it "should find the output files", ->
  128. @OutputFileFinder.findOutputFiles
  129. .calledWith(@resources, @compileDir)
  130. .should.equal true
  131. it "should return the output files", ->
  132. @callback.calledWith(null, @build_files).should.equal true
  133. it "should not inject draft mode by default", ->
  134. @DraftModeManager.injectDraftMode.called.should.equal false
  135. describe "with draft mode", ->
  136. beforeEach ->
  137. @request.draft = true
  138. @CompileManager.doCompile @request, @callback
  139. it "should inject the draft mode header", ->
  140. @DraftModeManager.injectDraftMode
  141. .calledWith(@compileDir + "/" + @rootResourcePath)
  142. .should.equal true
  143. describe "with a check option", ->
  144. beforeEach ->
  145. @request.check = "error"
  146. @CompileManager.doCompile @request, @callback
  147. it "should run chktex", ->
  148. @LatexRunner.runLatex
  149. .calledWith("#{@project_id}-#{@user_id}", {
  150. directory: @compileDir
  151. mainFile: @rootResourcePath
  152. compiler: @compiler
  153. timeout: @timeout
  154. image: @image
  155. environment: {'CHKTEX_OPTIONS': '-nall -e9 -e10 -w15 -w16', 'CHKTEX_EXIT_ON_ERROR':1, 'CHKTEX_ULIMIT_OPTIONS': '-t 5 -v 64000'}
  156. })
  157. .should.equal true
  158. describe "with a knitr file and check options", ->
  159. beforeEach ->
  160. @request.rootResourcePath = "main.Rtex"
  161. @request.check = "error"
  162. @CompileManager.doCompile @request, @callback
  163. it "should not run chktex", ->
  164. @LatexRunner.runLatex
  165. .calledWith("#{@project_id}-#{@user_id}", {
  166. directory: @compileDir
  167. mainFile: "main.Rtex"
  168. compiler: @compiler
  169. timeout: @timeout
  170. image: @image
  171. environment: @env
  172. })
  173. .should.equal true
  174. describe "clearProject", ->
  175. describe "succesfully", ->
  176. beforeEach ->
  177. @Settings.compileDir = "compiles"
  178. @fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
  179. @proc = new EventEmitter()
  180. @proc.stdout = new EventEmitter()
  181. @proc.stderr = new EventEmitter()
  182. @child_process.spawn = sinon.stub().returns(@proc)
  183. @CompileManager.clearProject @project_id, @user_id, @callback
  184. @proc.emit "close", 0
  185. it "should remove the project directory", ->
  186. @child_process.spawn
  187. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"])
  188. .should.equal true
  189. it "should call the callback", ->
  190. @callback.called.should.equal true
  191. describe "with a non-success status code", ->
  192. beforeEach ->
  193. @Settings.compileDir = "compiles"
  194. @fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
  195. @proc = new EventEmitter()
  196. @proc.stdout = new EventEmitter()
  197. @proc.stderr = new EventEmitter()
  198. @child_process.spawn = sinon.stub().returns(@proc)
  199. @CompileManager.clearProject @project_id, @user_id, @callback
  200. @proc.stderr.emit "data", @error = "oops"
  201. @proc.emit "close", 1
  202. it "should remove the project directory", ->
  203. @child_process.spawn
  204. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"])
  205. .should.equal true
  206. it "should call the callback with an error from the stderr", ->
  207. @callback
  208. .calledWith(new Error())
  209. .should.equal true
  210. @callback.args[0][0].message.should.equal "rm -r #{@Settings.path.compilesDir}/#{@project_id}-#{@user_id} failed: #{@error}"
  211. describe "syncing", ->
  212. beforeEach ->
  213. @page = 1
  214. @h = 42.23
  215. @v = 87.56
  216. @width = 100.01
  217. @height = 234.56
  218. @line = 5
  219. @column = 3
  220. @file_name = "main.tex"
  221. @child_process.execFile = sinon.stub()
  222. @Settings.path.synctexBaseDir = (project_id) => "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
  223. describe "syncFromCode", ->
  224. beforeEach ->
  225. @fs.stat = sinon.stub().callsArgWith(1, null,{isFile: ()->true})
  226. @stdout = "NODE\t#{@page}\t#{@h}\t#{@v}\t#{@width}\t#{@height}\n"
  227. @CommandRunner.run = sinon.stub().callsArgWith(6, null, {stdout:@stdout})
  228. @CompileManager.syncFromCode @project_id, @user_id, @file_name, @line, @column, @callback
  229. it "should execute the synctex binary", ->
  230. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  231. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/output.pdf"
  232. file_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/#{@file_name}"
  233. @CommandRunner.run
  234. .calledWith(
  235. "#{@project_id}-#{@user_id}",
  236. ['/opt/synctex', 'code', synctex_path, file_path, @line, @column],
  237. "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}",
  238. @Settings.clsi.docker.image,
  239. 10000,
  240. {}
  241. ).should.equal true
  242. it "should call the callback with the parsed output", ->
  243. @callback
  244. .calledWith(null, [{
  245. page: @page
  246. h: @h
  247. v: @v
  248. height: @height
  249. width: @width
  250. }])
  251. .should.equal true
  252. describe "syncFromPdf", ->
  253. beforeEach ->
  254. @fs.stat = sinon.stub().callsArgWith(1, null,{isFile: ()->true})
  255. @stdout = "NODE\t#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/#{@file_name}\t#{@line}\t#{@column}\n"
  256. @CommandRunner.run = sinon.stub().callsArgWith(6, null, {stdout:@stdout})
  257. @CompileManager.syncFromPdf @project_id, @user_id, @page, @h, @v, @callback
  258. it "should execute the synctex binary", ->
  259. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  260. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/output.pdf"
  261. @CommandRunner.run
  262. .calledWith(
  263. "#{@project_id}-#{@user_id}",
  264. ['/opt/synctex', "pdf", synctex_path, @page, @h, @v],
  265. "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}",
  266. @Settings.clsi.docker.image,
  267. 10000,
  268. {}).should.equal true
  269. it "should call the callback with the parsed output", ->
  270. @callback
  271. .calledWith(null, [{
  272. file: @file_name
  273. line: @line
  274. column: @column
  275. }])
  276. .should.equal true
  277. describe "wordcount", ->
  278. beforeEach ->
  279. @CommandRunner.run = sinon.stub().callsArg(6)
  280. @fs.readFile = sinon.stub().callsArgWith(2, null, @stdout = "Encoding: ascii\nWords in text: 2")
  281. @callback = sinon.stub()
  282. @project_id
  283. @timeout = 10 * 1000
  284. @file_name = "main.tex"
  285. @Settings.path.compilesDir = "/local/compile/directory"
  286. @image = "example.com/image"
  287. @CompileManager.wordcount @project_id, @user_id, @file_name, @image, @callback
  288. it "should run the texcount command", ->
  289. @directory = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
  290. @file_path = "$COMPILE_DIR/#{@file_name}"
  291. @command =[ "texcount", "-nocol", "-inc", @file_path, "-out=" + @file_path + ".wc"]
  292. @CommandRunner.run
  293. .calledWith("#{@project_id}-#{@user_id}", @command, @directory, @image, @timeout, {})
  294. .should.equal true
  295. it "should call the callback with the parsed output", ->
  296. @callback
  297. .calledWith(null, {
  298. encode: "ascii"
  299. textWords: 2
  300. headWords: 0
  301. outside: 0
  302. headers: 0
  303. elements: 0
  304. mathInline: 0
  305. mathDisplay: 0
  306. errors: 0
  307. messages: ""
  308. })
  309. .should.equal true