CompileManagerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /* eslint-disable
  2. camelcase,
  3. chai-friendly/no-unused-expressions,
  4. no-path-concat,
  5. no-return-assign,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const SandboxedModule = require('sandboxed-module')
  17. const sinon = require('sinon')
  18. require('chai').should()
  19. const modulePath = require('path').join(
  20. __dirname,
  21. '../../../app/js/CompileManager'
  22. )
  23. const tk = require('timekeeper')
  24. const { EventEmitter } = require('events')
  25. const Path = require('path')
  26. describe('CompileManager', function() {
  27. beforeEach(function() {
  28. this.CompileManager = SandboxedModule.require(modulePath, {
  29. requires: {
  30. './LatexRunner': (this.LatexRunner = {}),
  31. './ResourceWriter': (this.ResourceWriter = {}),
  32. './OutputFileFinder': (this.OutputFileFinder = {}),
  33. './OutputCacheManager': (this.OutputCacheManager = {}),
  34. 'settings-sharelatex': (this.Settings = {
  35. path: {
  36. compilesDir: '/compiles/dir'
  37. },
  38. synctexBaseDir() {
  39. return '/compile'
  40. },
  41. clsi: {
  42. docker: {
  43. image: 'SOMEIMAGE'
  44. }
  45. }
  46. }),
  47. 'logger-sharelatex': (this.logger = { log: sinon.stub(), info() {} }),
  48. child_process: (this.child_process = {}),
  49. './CommandRunner': (this.CommandRunner = {}),
  50. './DraftModeManager': (this.DraftModeManager = {}),
  51. './TikzManager': (this.TikzManager = {}),
  52. './LockManager': (this.LockManager = {}),
  53. fs: (this.fs = {}),
  54. 'fs-extra': (this.fse = { ensureDir: sinon.stub().callsArg(1) })
  55. }
  56. })
  57. this.callback = sinon.stub()
  58. this.project_id = 'project-id-123'
  59. return (this.user_id = '1234')
  60. })
  61. describe('doCompileWithLock', function() {
  62. beforeEach(function() {
  63. this.request = {
  64. resources: (this.resources = 'mock-resources'),
  65. project_id: this.project_id,
  66. user_id: this.user_id
  67. }
  68. this.output_files = ['foo', 'bar']
  69. this.Settings.compileDir = 'compiles'
  70. this.compileDir = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`
  71. this.CompileManager.doCompile = sinon
  72. .stub()
  73. .callsArgWith(1, null, this.output_files)
  74. return (this.LockManager.runWithLock = (lockFile, runner, callback) =>
  75. runner((err, ...result) => callback(err, ...Array.from(result))))
  76. })
  77. describe('when the project is not locked', function() {
  78. beforeEach(function() {
  79. return this.CompileManager.doCompileWithLock(
  80. this.request,
  81. this.callback
  82. )
  83. })
  84. it('should ensure that the compile directory exists', function() {
  85. return this.fse.ensureDir.calledWith(this.compileDir).should.equal(true)
  86. })
  87. it('should call doCompile with the request', function() {
  88. return this.CompileManager.doCompile
  89. .calledWith(this.request)
  90. .should.equal(true)
  91. })
  92. return it('should call the callback with the output files', function() {
  93. return this.callback
  94. .calledWithExactly(null, this.output_files)
  95. .should.equal(true)
  96. })
  97. })
  98. return describe('when the project is locked', function() {
  99. beforeEach(function() {
  100. this.error = new Error('locked')
  101. this.LockManager.runWithLock = (lockFile, runner, callback) => {
  102. return callback(this.error)
  103. }
  104. return this.CompileManager.doCompileWithLock(
  105. this.request,
  106. this.callback
  107. )
  108. })
  109. it('should ensure that the compile directory exists', function() {
  110. return this.fse.ensureDir.calledWith(this.compileDir).should.equal(true)
  111. })
  112. it('should not call doCompile with the request', function() {
  113. return this.CompileManager.doCompile.called.should.equal(false)
  114. })
  115. return it('should call the callback with the error', function() {
  116. return this.callback.calledWithExactly(this.error).should.equal(true)
  117. })
  118. })
  119. })
  120. describe('doCompile', function() {
  121. beforeEach(function() {
  122. this.output_files = [
  123. {
  124. path: 'output.log',
  125. type: 'log'
  126. },
  127. {
  128. path: 'output.pdf',
  129. type: 'pdf'
  130. }
  131. ]
  132. this.build_files = [
  133. {
  134. path: 'output.log',
  135. type: 'log',
  136. build: 1234
  137. },
  138. {
  139. path: 'output.pdf',
  140. type: 'pdf',
  141. build: 1234
  142. }
  143. ]
  144. this.request = {
  145. resources: (this.resources = 'mock-resources'),
  146. rootResourcePath: (this.rootResourcePath = 'main.tex'),
  147. project_id: this.project_id,
  148. user_id: this.user_id,
  149. compiler: (this.compiler = 'pdflatex'),
  150. timeout: (this.timeout = 42000),
  151. imageName: (this.image = 'example.com/image'),
  152. flags: (this.flags = ['-file-line-error'])
  153. }
  154. this.env = {}
  155. this.Settings.compileDir = 'compiles'
  156. this.compileDir = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`
  157. this.ResourceWriter.syncResourcesToDisk = sinon
  158. .stub()
  159. .callsArgWith(2, null, this.resources)
  160. this.LatexRunner.runLatex = sinon.stub().callsArg(2)
  161. this.OutputFileFinder.findOutputFiles = sinon
  162. .stub()
  163. .callsArgWith(2, null, this.output_files)
  164. this.OutputCacheManager.saveOutputFiles = sinon
  165. .stub()
  166. .callsArgWith(2, null, this.build_files)
  167. this.DraftModeManager.injectDraftMode = sinon.stub().callsArg(1)
  168. return (this.TikzManager.checkMainFile = sinon.stub().callsArg(3, false))
  169. })
  170. describe('normally', function() {
  171. beforeEach(function() {
  172. return this.CompileManager.doCompile(this.request, this.callback)
  173. })
  174. it('should write the resources to disk', function() {
  175. return this.ResourceWriter.syncResourcesToDisk
  176. .calledWith(this.request, this.compileDir)
  177. .should.equal(true)
  178. })
  179. it('should run LaTeX', function() {
  180. return this.LatexRunner.runLatex
  181. .calledWith(`${this.project_id}-${this.user_id}`, {
  182. directory: this.compileDir,
  183. mainFile: this.rootResourcePath,
  184. compiler: this.compiler,
  185. timeout: this.timeout,
  186. image: this.image,
  187. flags: this.flags,
  188. environment: this.env
  189. })
  190. .should.equal(true)
  191. })
  192. it('should find the output files', function() {
  193. return this.OutputFileFinder.findOutputFiles
  194. .calledWith(this.resources, this.compileDir)
  195. .should.equal(true)
  196. })
  197. it('should return the output files', function() {
  198. return this.callback
  199. .calledWith(null, this.build_files)
  200. .should.equal(true)
  201. })
  202. return it('should not inject draft mode by default', function() {
  203. return this.DraftModeManager.injectDraftMode.called.should.equal(false)
  204. })
  205. })
  206. describe('with draft mode', function() {
  207. beforeEach(function() {
  208. this.request.draft = true
  209. return this.CompileManager.doCompile(this.request, this.callback)
  210. })
  211. return it('should inject the draft mode header', function() {
  212. return this.DraftModeManager.injectDraftMode
  213. .calledWith(this.compileDir + '/' + this.rootResourcePath)
  214. .should.equal(true)
  215. })
  216. })
  217. describe('with a check option', function() {
  218. beforeEach(function() {
  219. this.request.check = 'error'
  220. return this.CompileManager.doCompile(this.request, this.callback)
  221. })
  222. return it('should run chktex', function() {
  223. return this.LatexRunner.runLatex
  224. .calledWith(`${this.project_id}-${this.user_id}`, {
  225. directory: this.compileDir,
  226. mainFile: this.rootResourcePath,
  227. compiler: this.compiler,
  228. timeout: this.timeout,
  229. image: this.image,
  230. flags: this.flags,
  231. environment: {
  232. CHKTEX_OPTIONS: '-nall -e9 -e10 -w15 -w16',
  233. CHKTEX_EXIT_ON_ERROR: 1,
  234. CHKTEX_ULIMIT_OPTIONS: '-t 5 -v 64000'
  235. }
  236. })
  237. .should.equal(true)
  238. })
  239. })
  240. return describe('with a knitr file and check options', function() {
  241. beforeEach(function() {
  242. this.request.rootResourcePath = 'main.Rtex'
  243. this.request.check = 'error'
  244. return this.CompileManager.doCompile(this.request, this.callback)
  245. })
  246. return it('should not run chktex', function() {
  247. return this.LatexRunner.runLatex
  248. .calledWith(`${this.project_id}-${this.user_id}`, {
  249. directory: this.compileDir,
  250. mainFile: 'main.Rtex',
  251. compiler: this.compiler,
  252. timeout: this.timeout,
  253. image: this.image,
  254. flags: this.flags,
  255. environment: this.env
  256. })
  257. .should.equal(true)
  258. })
  259. })
  260. })
  261. describe('clearProject', function() {
  262. describe('succesfully', function() {
  263. beforeEach(function() {
  264. this.Settings.compileDir = 'compiles'
  265. this.fs.lstat = sinon.stub().callsArgWith(1, null, {
  266. isDirectory() {
  267. return true
  268. }
  269. })
  270. this.proc = new EventEmitter()
  271. this.proc.stdout = new EventEmitter()
  272. this.proc.stderr = new EventEmitter()
  273. this.proc.stderr.setEncoding = sinon.stub().returns(this.proc.stderr)
  274. this.child_process.spawn = sinon.stub().returns(this.proc)
  275. this.CompileManager.clearProject(
  276. this.project_id,
  277. this.user_id,
  278. this.callback
  279. )
  280. return this.proc.emit('close', 0)
  281. })
  282. it('should remove the project directory', function() {
  283. return this.child_process.spawn
  284. .calledWith('rm', [
  285. '-r',
  286. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`
  287. ])
  288. .should.equal(true)
  289. })
  290. return it('should call the callback', function() {
  291. return this.callback.called.should.equal(true)
  292. })
  293. })
  294. return describe('with a non-success status code', function() {
  295. beforeEach(function() {
  296. this.Settings.compileDir = 'compiles'
  297. this.fs.lstat = sinon.stub().callsArgWith(1, null, {
  298. isDirectory() {
  299. return true
  300. }
  301. })
  302. this.proc = new EventEmitter()
  303. this.proc.stdout = new EventEmitter()
  304. this.proc.stderr = new EventEmitter()
  305. this.proc.stderr.setEncoding = sinon.stub().returns(this.proc.stderr)
  306. this.child_process.spawn = sinon.stub().returns(this.proc)
  307. this.CompileManager.clearProject(
  308. this.project_id,
  309. this.user_id,
  310. this.callback
  311. )
  312. this.proc.stderr.emit('data', (this.error = 'oops'))
  313. return this.proc.emit('close', 1)
  314. })
  315. it('should remove the project directory', function() {
  316. return this.child_process.spawn
  317. .calledWith('rm', [
  318. '-r',
  319. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`
  320. ])
  321. .should.equal(true)
  322. })
  323. it('should call the callback with an error from the stderr', function() {
  324. this.callback.calledWithExactly(sinon.match(Error)).should.equal(true)
  325. this.callback.args[0][0].message.should.equal(
  326. `rm -r ${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id} failed: ${this.error}`
  327. )
  328. })
  329. })
  330. })
  331. describe('syncing', function() {
  332. beforeEach(function() {
  333. this.page = 1
  334. this.h = 42.23
  335. this.v = 87.56
  336. this.width = 100.01
  337. this.height = 234.56
  338. this.line = 5
  339. this.column = 3
  340. this.file_name = 'main.tex'
  341. this.child_process.execFile = sinon.stub()
  342. return (this.Settings.path.synctexBaseDir = project_id =>
  343. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`)
  344. })
  345. describe('syncFromCode', function() {
  346. beforeEach(function() {
  347. this.fs.stat = sinon.stub().callsArgWith(1, null, {
  348. isFile() {
  349. return true
  350. }
  351. })
  352. this.stdout = `NODE\t${this.page}\t${this.h}\t${this.v}\t${this.width}\t${this.height}\n`
  353. this.CommandRunner.run = sinon
  354. .stub()
  355. .callsArgWith(6, null, { stdout: this.stdout })
  356. return this.CompileManager.syncFromCode(
  357. this.project_id,
  358. this.user_id,
  359. this.file_name,
  360. this.line,
  361. this.column,
  362. this.callback
  363. )
  364. })
  365. it('should execute the synctex binary', function() {
  366. const bin_path = Path.resolve(__dirname + '/../../../bin/synctex')
  367. const synctex_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/output.pdf`
  368. const file_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/${this.file_name}`
  369. return this.CommandRunner.run
  370. .calledWith(
  371. `${this.project_id}-${this.user_id}`,
  372. [
  373. '/opt/synctex',
  374. 'code',
  375. synctex_path,
  376. file_path,
  377. this.line,
  378. this.column
  379. ],
  380. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`,
  381. this.Settings.clsi.docker.image,
  382. 60000,
  383. {}
  384. )
  385. .should.equal(true)
  386. })
  387. return it('should call the callback with the parsed output', function() {
  388. return this.callback
  389. .calledWith(null, [
  390. {
  391. page: this.page,
  392. h: this.h,
  393. v: this.v,
  394. height: this.height,
  395. width: this.width
  396. }
  397. ])
  398. .should.equal(true)
  399. })
  400. })
  401. return describe('syncFromPdf', function() {
  402. beforeEach(function() {
  403. this.fs.stat = sinon.stub().callsArgWith(1, null, {
  404. isFile() {
  405. return true
  406. }
  407. })
  408. this.stdout = `NODE\t${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/${this.file_name}\t${this.line}\t${this.column}\n`
  409. this.CommandRunner.run = sinon
  410. .stub()
  411. .callsArgWith(6, null, { stdout: this.stdout })
  412. return this.CompileManager.syncFromPdf(
  413. this.project_id,
  414. this.user_id,
  415. this.page,
  416. this.h,
  417. this.v,
  418. this.callback
  419. )
  420. })
  421. it('should execute the synctex binary', function() {
  422. const bin_path = Path.resolve(__dirname + '/../../../bin/synctex')
  423. const synctex_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/output.pdf`
  424. return this.CommandRunner.run
  425. .calledWith(
  426. `${this.project_id}-${this.user_id}`,
  427. ['/opt/synctex', 'pdf', synctex_path, this.page, this.h, this.v],
  428. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`,
  429. this.Settings.clsi.docker.image,
  430. 60000,
  431. {}
  432. )
  433. .should.equal(true)
  434. })
  435. return it('should call the callback with the parsed output', function() {
  436. return this.callback
  437. .calledWith(null, [
  438. {
  439. file: this.file_name,
  440. line: this.line,
  441. column: this.column
  442. }
  443. ])
  444. .should.equal(true)
  445. })
  446. })
  447. })
  448. return describe('wordcount', function() {
  449. beforeEach(function() {
  450. this.CommandRunner.run = sinon.stub().callsArg(6)
  451. this.fs.readFile = sinon
  452. .stub()
  453. .callsArgWith(
  454. 2,
  455. null,
  456. (this.stdout = 'Encoding: ascii\nWords in text: 2')
  457. )
  458. this.callback = sinon.stub()
  459. this.project_id
  460. this.timeout = 60 * 1000
  461. this.file_name = 'main.tex'
  462. this.Settings.path.compilesDir = '/local/compile/directory'
  463. this.image = 'example.com/image'
  464. return this.CompileManager.wordcount(
  465. this.project_id,
  466. this.user_id,
  467. this.file_name,
  468. this.image,
  469. this.callback
  470. )
  471. })
  472. it('should run the texcount command', function() {
  473. this.directory = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`
  474. this.file_path = `$COMPILE_DIR/${this.file_name}`
  475. this.command = [
  476. 'texcount',
  477. '-nocol',
  478. '-inc',
  479. this.file_path,
  480. `-out=${this.file_path}.wc`
  481. ]
  482. return this.CommandRunner.run
  483. .calledWith(
  484. `${this.project_id}-${this.user_id}`,
  485. this.command,
  486. this.directory,
  487. this.image,
  488. this.timeout,
  489. {}
  490. )
  491. .should.equal(true)
  492. })
  493. return it('should call the callback with the parsed output', function() {
  494. return this.callback
  495. .calledWith(null, {
  496. encode: 'ascii',
  497. textWords: 2,
  498. headWords: 0,
  499. outside: 0,
  500. headers: 0,
  501. elements: 0,
  502. mathInline: 0,
  503. mathDisplay: 0,
  504. errors: 0,
  505. messages: ''
  506. })
  507. .should.equal(true)
  508. })
  509. })
  510. })