CompileManagerTests.coffee 8.9 KB

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