CompileManagerTests.js 20 KB

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