CompileManagerTests.js 18 KB

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