CompileManagerTests.coffee 7.8 KB

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