CompileManagerTests.coffee 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "clearProject", ->
  91. describe "succesfully", ->
  92. beforeEach ->
  93. @Settings.compileDir = "compiles"
  94. @fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
  95. @proc = new EventEmitter()
  96. @proc.stdout = new EventEmitter()
  97. @proc.stderr = new EventEmitter()
  98. @child_process.spawn = sinon.stub().returns(@proc)
  99. @CompileManager.clearProject @project_id, @user_id, @callback
  100. @proc.emit "close", 0
  101. it "should remove the project directory", ->
  102. @child_process.spawn
  103. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"])
  104. .should.equal true
  105. it "should call the callback", ->
  106. @callback.called.should.equal true
  107. describe "with a non-success status code", ->
  108. beforeEach ->
  109. @Settings.compileDir = "compiles"
  110. @fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory: ()->true})
  111. @proc = new EventEmitter()
  112. @proc.stdout = new EventEmitter()
  113. @proc.stderr = new EventEmitter()
  114. @child_process.spawn = sinon.stub().returns(@proc)
  115. @CompileManager.clearProject @project_id, @user_id, @callback
  116. @proc.stderr.emit "data", @error = "oops"
  117. @proc.emit "close", 1
  118. it "should remove the project directory", ->
  119. @child_process.spawn
  120. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"])
  121. .should.equal true
  122. it "should call the callback with an error from the stderr", ->
  123. @callback
  124. .calledWith(new Error())
  125. .should.equal true
  126. @callback.args[0][0].message.should.equal "rm -r #{@Settings.path.compilesDir}/#{@project_id}-#{@user_id} failed: #{@error}"
  127. describe "syncing", ->
  128. beforeEach ->
  129. @page = 1
  130. @h = 42.23
  131. @v = 87.56
  132. @width = 100.01
  133. @height = 234.56
  134. @line = 5
  135. @column = 3
  136. @file_name = "main.tex"
  137. @child_process.execFile = sinon.stub()
  138. @Settings.path.synctexBaseDir = (project_id) => "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
  139. describe "syncFromCode", ->
  140. beforeEach ->
  141. @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@page}\t#{@h}\t#{@v}\t#{@width}\t#{@height}\n", "")
  142. @CompileManager.syncFromCode @project_id, @user_id, @file_name, @line, @column, @callback
  143. it "should execute the synctex binary", ->
  144. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  145. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/output.pdf"
  146. file_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/#{@file_name}"
  147. @child_process.execFile
  148. .calledWith(bin_path, ["code", synctex_path, file_path, @line, @column], timeout: 10000)
  149. .should.equal true
  150. it "should call the callback with the parsed output", ->
  151. @callback
  152. .calledWith(null, [{
  153. page: @page
  154. h: @h
  155. v: @v
  156. height: @height
  157. width: @width
  158. }])
  159. .should.equal true
  160. describe "syncFromPdf", ->
  161. beforeEach ->
  162. @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/#{@file_name}\t#{@line}\t#{@column}\n", "")
  163. @CompileManager.syncFromPdf @project_id, @user_id, @page, @h, @v, @callback
  164. it "should execute the synctex binary", ->
  165. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  166. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}/output.pdf"
  167. @child_process.execFile
  168. .calledWith(bin_path, ["pdf", synctex_path, @page, @h, @v], timeout: 10000)
  169. .should.equal true
  170. it "should call the callback with the parsed output", ->
  171. @callback
  172. .calledWith(null, [{
  173. file: @file_name
  174. line: @line
  175. column: @column
  176. }])
  177. .should.equal true
  178. describe "wordcount", ->
  179. beforeEach ->
  180. @CommandRunner.run = sinon.stub().callsArg(6)
  181. @fs.readFile = sinon.stub().callsArgWith(2, null, @stdout = "Encoding: ascii\nWords in text: 2")
  182. @callback = sinon.stub()
  183. @project_id = "project-id-123"
  184. @timeout = 10 * 1000
  185. @file_name = "main.tex"
  186. @Settings.path.compilesDir = "/local/compile/directory"
  187. @image = "example.com/image"
  188. @CompileManager.wordcount @project_id, @user_id, @file_name, @image, @callback
  189. it "should run the texcount command", ->
  190. @directory = "#{@Settings.path.compilesDir}/#{@project_id}-#{@user_id}"
  191. @file_path = "$COMPILE_DIR/#{@file_name}"
  192. @command =[ "texcount", "-nocol", "-inc", @file_path, "-out=" + @file_path + ".wc"]
  193. @CommandRunner.run
  194. .calledWith("#{@project_id}-#{@user_id}", @command, @directory, @image, @timeout, {})
  195. .should.equal true
  196. it "should call the callback with the parsed output", ->
  197. @callback
  198. .calledWith(null, {
  199. encode: "ascii"
  200. textWords: 2
  201. headWords: 0
  202. outside: 0
  203. headers: 0
  204. elements: 0
  205. mathInline: 0
  206. mathDisplay: 0
  207. errors: 0
  208. messages: ""
  209. })
  210. .should.equal true