CompileManagerTests.coffee 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. @callback = sinon.stub()
  19. describe "doCompile", ->
  20. beforeEach ->
  21. @output_files = [{
  22. path: "output.log"
  23. type: "log"
  24. }, {
  25. path: "output.pdf"
  26. type: "pdf"
  27. }]
  28. @build_files = [{
  29. path: "output.log"
  30. type: "log"
  31. build: 1234
  32. }, {
  33. path: "output.pdf"
  34. type: "pdf"
  35. build: 1234
  36. }]
  37. @request =
  38. resources: @resources = "mock-resources"
  39. rootResourcePath: @rootResourcePath = "main.tex"
  40. project_id: @project_id = "project-id-123"
  41. compiler: @compiler = "pdflatex"
  42. timeout: @timeout = 42000
  43. @Settings.compileDir = "compiles"
  44. @compileDir = "#{@Settings.path.compilesDir}/#{@project_id}"
  45. @ResourceWriter.syncResourcesToDisk = sinon.stub().callsArg(3)
  46. @LatexRunner.runLatex = sinon.stub().callsArg(2)
  47. @OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files)
  48. @OutputCacheManager.saveOutputFiles = sinon.stub().callsArgWith(2, null, @build_files)
  49. @CompileManager.doCompile @request, @callback
  50. it "should write the resources to disk", ->
  51. @ResourceWriter.syncResourcesToDisk
  52. .calledWith(@project_id, @resources, @compileDir)
  53. .should.equal true
  54. it "should run LaTeX", ->
  55. @LatexRunner.runLatex
  56. .calledWith(@project_id, {
  57. directory: @compileDir
  58. mainFile: @rootResourcePath
  59. compiler: @compiler
  60. timeout: @timeout
  61. })
  62. .should.equal true
  63. it "should find the output files", ->
  64. @OutputFileFinder.findOutputFiles
  65. .calledWith(@resources, @compileDir)
  66. .should.equal true
  67. it "should return the output files", ->
  68. @callback.calledWith(null, @build_files).should.equal true
  69. describe "clearProject", ->
  70. describe "succesfully", ->
  71. beforeEach ->
  72. @Settings.compileDir = "compiles"
  73. @proc = new EventEmitter()
  74. @proc.stdout = new EventEmitter()
  75. @proc.stderr = new EventEmitter()
  76. @child_process.spawn = sinon.stub().returns(@proc)
  77. @CompileManager.clearProject @project_id, @callback
  78. @proc.emit "close", 0
  79. it "should remove the project directory", ->
  80. @child_process.spawn
  81. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}"])
  82. .should.equal true
  83. it "should call the callback", ->
  84. @callback.called.should.equal true
  85. describe "with a non-success status code", ->
  86. beforeEach ->
  87. @Settings.compileDir = "compiles"
  88. @proc = new EventEmitter()
  89. @proc.stdout = new EventEmitter()
  90. @proc.stderr = new EventEmitter()
  91. @child_process.spawn = sinon.stub().returns(@proc)
  92. @CompileManager.clearProject @project_id, @callback
  93. @proc.stderr.emit "data", @error = "oops"
  94. @proc.emit "close", 1
  95. it "should remove the project directory", ->
  96. @child_process.spawn
  97. .calledWith("rm", ["-r", "#{@Settings.path.compilesDir}/#{@project_id}"])
  98. .should.equal true
  99. it "should call the callback with an error from the stderr", ->
  100. @callback
  101. .calledWith(new Error())
  102. .should.equal true
  103. @callback.args[0][0].message.should.equal "rm -r #{@Settings.path.compilesDir}/#{@project_id} failed: #{@error}"
  104. describe "syncing", ->
  105. beforeEach ->
  106. @page = 1
  107. @h = 42.23
  108. @v = 87.56
  109. @width = 100.01
  110. @height = 234.56
  111. @line = 5
  112. @column = 3
  113. @file_name = "main.tex"
  114. @child_process.execFile = sinon.stub()
  115. @Settings.path.synctexBaseDir = (project_id) => "#{@Settings.path.compilesDir}/#{@project_id}"
  116. describe "syncFromCode", ->
  117. beforeEach ->
  118. @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@page}\t#{@h}\t#{@v}\t#{@width}\t#{@height}\n", "")
  119. @CompileManager.syncFromCode @project_id, @file_name, @line, @column, @callback
  120. it "should execute the synctex binary", ->
  121. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  122. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}/output.pdf"
  123. file_path = "#{@Settings.path.compilesDir}/#{@project_id}/#{@file_name}"
  124. @child_process.execFile
  125. .calledWith(bin_path, ["code", synctex_path, file_path, @line, @column], timeout: 10000)
  126. .should.equal true
  127. it "should call the callback with the parsed output", ->
  128. @callback
  129. .calledWith(null, [{
  130. page: @page
  131. h: @h
  132. v: @v
  133. height: @height
  134. width: @width
  135. }])
  136. .should.equal true
  137. describe "syncFromPdf", ->
  138. beforeEach ->
  139. @child_process.execFile.callsArgWith(3, null, @stdout = "NODE\t#{@Settings.path.compilesDir}/#{@project_id}/#{@file_name}\t#{@line}\t#{@column}\n", "")
  140. @CompileManager.syncFromPdf @project_id, @page, @h, @v, @callback
  141. it "should execute the synctex binary", ->
  142. bin_path = Path.resolve(__dirname + "/../../../bin/synctex")
  143. synctex_path = "#{@Settings.path.compilesDir}/#{@project_id}/output.pdf"
  144. @child_process.execFile
  145. .calledWith(bin_path, ["pdf", synctex_path, @page, @h, @v], timeout: 10000)
  146. .should.equal true
  147. it "should call the callback with the parsed output", ->
  148. @callback
  149. .calledWith(null, [{
  150. file: @file_name
  151. line: @line
  152. column: @column
  153. }])
  154. .should.equal true