CompileManagerTests.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. 'settings-sharelatex': (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. .callsArgWith(1, 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. .callsArgWith(2, null, this.output_files)
  165. this.OutputCacheManager.saveOutputFiles = sinon
  166. .stub()
  167. .callsArgWith(3, 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. .callsArgWith(7, 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. this.callback
  373. )
  374. })
  375. it('should execute the synctex binary', function () {
  376. const bin_path = Path.resolve(__dirname + '/../../../bin/synctex')
  377. const synctex_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/output.pdf`
  378. const file_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/${this.file_name}`
  379. return this.CommandRunner.run
  380. .calledWith(
  381. `${this.project_id}-${this.user_id}`,
  382. [
  383. '/opt/synctex',
  384. 'code',
  385. synctex_path,
  386. file_path,
  387. this.line,
  388. this.column
  389. ],
  390. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`,
  391. this.Settings.clsi.docker.image,
  392. 60000,
  393. {}
  394. )
  395. .should.equal(true)
  396. })
  397. return it('should call the callback with the parsed output', function () {
  398. return this.callback
  399. .calledWith(null, [
  400. {
  401. page: this.page,
  402. h: this.h,
  403. v: this.v,
  404. height: this.height,
  405. width: this.width
  406. }
  407. ])
  408. .should.equal(true)
  409. })
  410. })
  411. return describe('syncFromPdf', function () {
  412. beforeEach(function () {
  413. this.fs.stat = sinon.stub().callsArgWith(1, null, {
  414. isFile() {
  415. return true
  416. }
  417. })
  418. this.stdout = `NODE\t${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/${this.file_name}\t${this.line}\t${this.column}\n`
  419. this.CommandRunner.run = sinon
  420. .stub()
  421. .callsArgWith(7, null, { stdout: this.stdout })
  422. return this.CompileManager.syncFromPdf(
  423. this.project_id,
  424. this.user_id,
  425. this.page,
  426. this.h,
  427. this.v,
  428. this.callback
  429. )
  430. })
  431. it('should execute the synctex binary', function () {
  432. const bin_path = Path.resolve(__dirname + '/../../../bin/synctex')
  433. const synctex_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/output.pdf`
  434. return this.CommandRunner.run
  435. .calledWith(
  436. `${this.project_id}-${this.user_id}`,
  437. ['/opt/synctex', 'pdf', synctex_path, this.page, this.h, this.v],
  438. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`,
  439. this.Settings.clsi.docker.image,
  440. 60000,
  441. {}
  442. )
  443. .should.equal(true)
  444. })
  445. return it('should call the callback with the parsed output', function () {
  446. return this.callback
  447. .calledWith(null, [
  448. {
  449. file: this.file_name,
  450. line: this.line,
  451. column: this.column
  452. }
  453. ])
  454. .should.equal(true)
  455. })
  456. })
  457. })
  458. return describe('wordcount', function () {
  459. beforeEach(function () {
  460. this.CommandRunner.run = sinon.stub().callsArg(7)
  461. this.fs.readFile = sinon
  462. .stub()
  463. .callsArgWith(
  464. 2,
  465. null,
  466. (this.stdout = 'Encoding: ascii\nWords in text: 2')
  467. )
  468. this.callback = sinon.stub()
  469. this.project_id
  470. this.timeout = 60 * 1000
  471. this.file_name = 'main.tex'
  472. this.Settings.path.compilesDir = '/local/compile/directory'
  473. this.image = 'example.com/image'
  474. return this.CompileManager.wordcount(
  475. this.project_id,
  476. this.user_id,
  477. this.file_name,
  478. this.image,
  479. this.callback
  480. )
  481. })
  482. it('should run the texcount command', function () {
  483. this.directory = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`
  484. this.file_path = `$COMPILE_DIR/${this.file_name}`
  485. this.command = [
  486. 'texcount',
  487. '-nocol',
  488. '-inc',
  489. this.file_path,
  490. `-out=${this.file_path}.wc`
  491. ]
  492. return this.CommandRunner.run
  493. .calledWith(
  494. `${this.project_id}-${this.user_id}`,
  495. this.command,
  496. this.directory,
  497. this.image,
  498. this.timeout,
  499. {}
  500. )
  501. .should.equal(true)
  502. })
  503. return it('should call the callback with the parsed output', function () {
  504. return this.callback
  505. .calledWith(null, {
  506. encode: 'ascii',
  507. textWords: 2,
  508. headWords: 0,
  509. outside: 0,
  510. headers: 0,
  511. elements: 0,
  512. mathInline: 0,
  513. mathDisplay: 0,
  514. errors: 0,
  515. messages: ''
  516. })
  517. .should.equal(true)
  518. })
  519. })
  520. })