CompileManagerTests.js 17 KB

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