CompileManagerTests.js 18 KB

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