CompileManagerTests.coffee 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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() }
  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. compiler: @compiler = "pdflatex"
  45. timeout: @timeout = 42000
  46. imageName: @image = "example.com/image"
  47. @Settings.compileDir = "compiles"
  48. @compileDir = "#{@Settings.path.compilesDir}/#{@project_id}"
  49. @ResourceWriter.syncResourcesToDisk = sinon.stub().callsArg(3)
  50. @LatexRunner.runLatex = sinon.stub().callsArg(2)
  51. @OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files)
  52. @OutputCacheManager.saveOutputFiles = sinon.stub().callsArgWith(2, null, @build_files)
  53. @DraftModeManager.injectDraftMode = sinon.stub().callsArg(1)
  54. describe "normally", ->
  55. beforeEach ->
  56. @CompileManager.doCompile @request, @callback
  57. it "should write the resources to disk", ->
  58. @ResourceWriter.syncResourcesToDisk
  59. .calledWith(@project_id, @resources, @compileDir)
  60. .should.equal true
  61. it "should run LaTeX", ->
  62. @LatexRunner.runLatex
  63. .calledWith(@project_id, {
  64. directory: @compileDir
  65. mainFile: @rootResourcePath
  66. compiler: @compiler
  67. timeout: @timeout
  68. image: @image
  69. })
  70. .should.equal true
  71. it "should find the output files", ->
  72. @OutputFileFinder.findOutputFiles
  73. .calledWith(@resources, @compileDir)
  74. .should.equal true
  75. it "should return the output files", ->
  76. @callback.calledWith(null, @build_files).should.equal true
  77. it "should not inject draft mode by default", ->
  78. @DraftModeManager.injectDraftMode.called.should.equal false
  79. describe "with draft mode", ->
  80. beforeEach ->
  81. @request.draft = true
  82. @CompileManager.doCompile @request, @callback
  83. it "should inject the draft mode header", ->
  84. @DraftModeManager.injectDraftMode
  85. .calledWith(@compileDir + "/" + @rootResourcePath)
  86. .should.equal true
  87. describe "clearProject", ->
  88. describe "succesfully", ->
  89. beforeEach ->
  90. @Settings.compileDir = "compiles"
  91. @fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
  92. @proc = new EventEmitter()
  93. @proc.stdout = new EventEmitter()
  94. @proc.stderr = new EventEmitter()
  95. @child_process.spawn = sinon.stub().returns(@proc)
  96. @CompileManager.clearProject @project_id, @callback
  97. @proc.emit "close", 0
  98. it "should remove the project directory", ->
  99. @child_process.spawn
  100. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}"])
  101. .should.equal true
  102. it "should call the callback", ->
  103. @callback.called.should.equal true
  104. describe "with a non-success status code", ->
  105. beforeEach ->
  106. @Settings.compileDir = "compiles"
  107. @fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
  108. @proc = new EventEmitter()
  109. @proc.stdout = new EventEmitter()
  110. @proc.stderr = new EventEmitter()
  111. @child_process.spawn = sinon.stub().returns(@proc)
  112. @CompileManager.clearProject @project_id, @callback
  113. @proc.stderr.emit "data", @error = "oops"
  114. @proc.emit "close", 1
  115. it "should remove the project directory", ->
  116. @child_process.spawn
  117. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}"])
  118. .should.equal true
  119. it "should call the callback with an error from the stderr", ->
  120. @callback
  121. .calledWith(new Error())
  122. .should.equal true
  123. @callback.args[0][0].message.should.equal "rm -r #{@Settings.path.compilesDir}/#{@project_id} failed: #{@error}"
  124. describe "syncing", ->
  125. beforeEach ->
  126. @page = 1
  127. @h = 42.23
  128. @v = 87.56
  129. @width = 100.01
  130. @height = 234.56
  131. @line = 5
  132. @column = 3
  133. @file_name = "main.tex"
  134. @child_process.execFile = sinon.stub()
  135. @Settings.path.synctexBaseDir = (project_id) => "#{@Settings.path.compilesDir}/#{@project_id}"
  136. describe "syncFromCode", ->
  137. beforeEach ->
  138. @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@page}\t#{@h}\t#{@v}\t#{@width}\t#{@height}\n", "")
  139. @CompileManager.syncFromCode @project_id, @file_name, @line, @column, @callback
  140. it "should execute the synctex binary", ->
  141. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  142. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}/output.pdf"
  143. file_path = "#{@Settings.path.compilesDir}/#{@project_id}/#{@file_name}"
  144. @child_process.execFile
  145. .calledWith(bin_path, ["code", synctex_path, file_path, @line, @column], timeout: 10000)
  146. .should.equal true
  147. it "should call the callback with the parsed output", ->
  148. @callback
  149. .calledWith(null, [{
  150. page: @page
  151. h: @h
  152. v: @v
  153. height: @height
  154. width: @width
  155. }])
  156. .should.equal true
  157. describe "syncFromPdf", ->
  158. beforeEach ->
  159. @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@Settings.path.compilesDir}/#{@project_id}/#{@file_name}\t#{@line}\t#{@column}\n", "")
  160. @CompileManager.syncFromPdf @project_id, @page, @h, @v, @callback
  161. it "should execute the synctex binary", ->
  162. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  163. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}/output.pdf"
  164. @child_process.execFile
  165. .calledWith(bin_path, ["pdf", synctex_path, @page, @h, @v], timeout: 10000)
  166. .should.equal true
  167. it "should call the callback with the parsed output", ->
  168. @callback
  169. .calledWith(null, [{
  170. file: @file_name
  171. line: @line
  172. column: @column
  173. }])
  174. .should.equal true
  175. describe "wordcount", ->
  176. beforeEach ->
  177. @CommandRunner.run = sinon.stub().callsArg(5)
  178. @fs.readFileSync = sinon.stub().returns @stdout = "Encoding: ascii\nWords in text: 2"
  179. @callback = sinon.stub()
  180. @project_id = "project-id-123"
  181. @timeout = 10 * 1000
  182. @file_name = "main.tex"
  183. @Settings.path.compilesDir = "/local/compile/directory"
  184. @image = "example.com/image"
  185. @CompileManager.wordcount @project_id, @file_name, @image, @callback
  186. it "should run the texcount command", ->
  187. @directory = "#{@Settings.path.compilesDir}/#{@project_id}"
  188. @file_path = "$COMPILE_DIR/#{@file_name}"
  189. @command =[ "texcount", "-inc", @file_path, "-out=" + @file_path + ".wc"]
  190. @CommandRunner.run
  191. .calledWith(@project_id, @command, @directory, @image, @timeout)
  192. .should.equal true
  193. it "should call the callback with the parsed output", ->
  194. @callback
  195. .calledWith(null, {
  196. encode: "ascii"
  197. textWords: 2
  198. headWords: 0
  199. outside: 0
  200. headers: 0
  201. elements: 0
  202. mathInline: 0
  203. mathDisplay: 0
  204. })
  205. .should.equal true