CompileManagerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. compileGroup: (this.compileGroup = 'compile-group')
  154. }
  155. this.env = {}
  156. this.Settings.compileDir = 'compiles'
  157. this.compileDir = `${this.Settings.path.compilesDir}/${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(2, 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. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`
  291. ])
  292. .should.equal(true)
  293. })
  294. return it('should call the callback', function() {
  295. return this.callback.called.should.equal(true)
  296. })
  297. })
  298. return describe('with a non-success status code', function() {
  299. beforeEach(function() {
  300. this.Settings.compileDir = 'compiles'
  301. this.fs.lstat = sinon.stub().callsArgWith(1, null, {
  302. isDirectory() {
  303. return true
  304. }
  305. })
  306. this.proc = new EventEmitter()
  307. this.proc.stdout = new EventEmitter()
  308. this.proc.stderr = new EventEmitter()
  309. this.proc.stderr.setEncoding = sinon.stub().returns(this.proc.stderr)
  310. this.child_process.spawn = sinon.stub().returns(this.proc)
  311. this.CompileManager.clearProject(
  312. this.project_id,
  313. this.user_id,
  314. this.callback
  315. )
  316. this.proc.stderr.emit('data', (this.error = 'oops'))
  317. return this.proc.emit('close', 1)
  318. })
  319. it('should remove the project directory', function() {
  320. return this.child_process.spawn
  321. .calledWith('rm', [
  322. '-r',
  323. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`
  324. ])
  325. .should.equal(true)
  326. })
  327. it('should call the callback with an error from the stderr', function() {
  328. this.callback.calledWithExactly(sinon.match(Error)).should.equal(true)
  329. this.callback.args[0][0].message.should.equal(
  330. `rm -r ${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id} failed: ${this.error}`
  331. )
  332. })
  333. })
  334. })
  335. describe('syncing', function() {
  336. beforeEach(function() {
  337. this.page = 1
  338. this.h = 42.23
  339. this.v = 87.56
  340. this.width = 100.01
  341. this.height = 234.56
  342. this.line = 5
  343. this.column = 3
  344. this.file_name = 'main.tex'
  345. this.child_process.execFile = sinon.stub()
  346. return (this.Settings.path.synctexBaseDir = project_id =>
  347. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`)
  348. })
  349. describe('syncFromCode', function() {
  350. beforeEach(function() {
  351. this.fs.stat = sinon.stub().callsArgWith(1, null, {
  352. isFile() {
  353. return true
  354. }
  355. })
  356. this.stdout = `NODE\t${this.page}\t${this.h}\t${this.v}\t${this.width}\t${this.height}\n`
  357. this.CommandRunner.run = sinon
  358. .stub()
  359. .callsArgWith(7, null, { stdout: this.stdout })
  360. return this.CompileManager.syncFromCode(
  361. this.project_id,
  362. this.user_id,
  363. this.file_name,
  364. this.line,
  365. this.column,
  366. this.callback
  367. )
  368. })
  369. it('should execute the synctex binary', function() {
  370. const bin_path = Path.resolve(__dirname + '/../../../bin/synctex')
  371. const synctex_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/output.pdf`
  372. const file_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/${this.file_name}`
  373. return this.CommandRunner.run
  374. .calledWith(
  375. `${this.project_id}-${this.user_id}`,
  376. [
  377. '/opt/synctex',
  378. 'code',
  379. synctex_path,
  380. file_path,
  381. this.line,
  382. this.column
  383. ],
  384. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`,
  385. this.Settings.clsi.docker.image,
  386. 60000,
  387. {}
  388. )
  389. .should.equal(true)
  390. })
  391. return it('should call the callback with the parsed output', function() {
  392. return this.callback
  393. .calledWith(null, [
  394. {
  395. page: this.page,
  396. h: this.h,
  397. v: this.v,
  398. height: this.height,
  399. width: this.width
  400. }
  401. ])
  402. .should.equal(true)
  403. })
  404. })
  405. return describe('syncFromPdf', function() {
  406. beforeEach(function() {
  407. this.fs.stat = sinon.stub().callsArgWith(1, null, {
  408. isFile() {
  409. return true
  410. }
  411. })
  412. this.stdout = `NODE\t${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/${this.file_name}\t${this.line}\t${this.column}\n`
  413. this.CommandRunner.run = sinon
  414. .stub()
  415. .callsArgWith(7, null, { stdout: this.stdout })
  416. return this.CompileManager.syncFromPdf(
  417. this.project_id,
  418. this.user_id,
  419. this.page,
  420. this.h,
  421. this.v,
  422. this.callback
  423. )
  424. })
  425. it('should execute the synctex binary', function() {
  426. const bin_path = Path.resolve(__dirname + '/../../../bin/synctex')
  427. const synctex_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/output.pdf`
  428. return this.CommandRunner.run
  429. .calledWith(
  430. `${this.project_id}-${this.user_id}`,
  431. ['/opt/synctex', 'pdf', synctex_path, this.page, this.h, this.v],
  432. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`,
  433. this.Settings.clsi.docker.image,
  434. 60000,
  435. {}
  436. )
  437. .should.equal(true)
  438. })
  439. return it('should call the callback with the parsed output', function() {
  440. return this.callback
  441. .calledWith(null, [
  442. {
  443. file: this.file_name,
  444. line: this.line,
  445. column: this.column
  446. }
  447. ])
  448. .should.equal(true)
  449. })
  450. })
  451. })
  452. return describe('wordcount', function() {
  453. beforeEach(function() {
  454. this.CommandRunner.run = sinon.stub().callsArg(7)
  455. this.fs.readFile = sinon
  456. .stub()
  457. .callsArgWith(
  458. 2,
  459. null,
  460. (this.stdout = 'Encoding: ascii\nWords in text: 2')
  461. )
  462. this.callback = sinon.stub()
  463. this.project_id
  464. this.timeout = 60 * 1000
  465. this.file_name = 'main.tex'
  466. this.Settings.path.compilesDir = '/local/compile/directory'
  467. this.image = 'example.com/image'
  468. return this.CompileManager.wordcount(
  469. this.project_id,
  470. this.user_id,
  471. this.file_name,
  472. this.image,
  473. this.callback
  474. )
  475. })
  476. it('should run the texcount command', function() {
  477. this.directory = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`
  478. this.file_path = `$COMPILE_DIR/${this.file_name}`
  479. this.command = [
  480. 'texcount',
  481. '-nocol',
  482. '-inc',
  483. this.file_path,
  484. `-out=${this.file_path}.wc`
  485. ]
  486. return this.CommandRunner.run
  487. .calledWith(
  488. `${this.project_id}-${this.user_id}`,
  489. this.command,
  490. this.directory,
  491. this.image,
  492. this.timeout,
  493. {}
  494. )
  495. .should.equal(true)
  496. })
  497. return it('should call the callback with the parsed output', function() {
  498. return this.callback
  499. .calledWith(null, {
  500. encode: 'ascii',
  501. textWords: 2,
  502. headWords: 0,
  503. outside: 0,
  504. headers: 0,
  505. elements: 0,
  506. mathInline: 0,
  507. mathDisplay: 0,
  508. errors: 0,
  509. messages: ''
  510. })
  511. .should.equal(true)
  512. })
  513. })
  514. })