ResourceWriter.test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. import { vi, expect, describe, beforeEach, it } from 'vitest'
  2. import sinon from 'sinon'
  3. import path from 'node:path'
  4. const modulePath = path.join(
  5. import.meta.dirname,
  6. '../../../app/js/ResourceWriter'
  7. )
  8. describe('ResourceWriter', () => {
  9. beforeEach(async ctx => {
  10. let Timer
  11. vi.doMock('fs', () => ({
  12. default: (ctx.fs = {
  13. mkdir: sinon.stub().callsArg(1),
  14. unlink: sinon.stub().callsArg(1),
  15. }),
  16. }))
  17. vi.doMock('@overleaf/settings', () => ({
  18. default: (ctx.settings = {
  19. preciousFilePattern: '{keepdir/**,.other/keepdir/**}',
  20. }),
  21. }))
  22. vi.doMock('../../../app/js/ResourceStateManager', () => ({
  23. default: (ctx.ResourceStateManager = {}),
  24. }))
  25. vi.doMock('../../../app/js/UrlCache', () => ({
  26. default: (ctx.UrlCache = {
  27. createProjectDir: sinon.stub().yields(),
  28. }),
  29. }))
  30. vi.doMock('../../../app/js/OutputFileFinder', () => ({
  31. default: (ctx.OutputFileFinder = {}),
  32. }))
  33. vi.doMock('@overleaf/metrics', () => ({
  34. // Mocks allow us to import Metrics.js twice without getting errors.
  35. prom: {
  36. Gauge: sinon.stub(),
  37. Histogram: sinon.stub(),
  38. Counter: sinon.stub(),
  39. },
  40. default: (ctx.Metrics = {
  41. inc: sinon.stub(),
  42. Timer: (Timer = (function () {
  43. Timer = class Timer {
  44. static initClass() {
  45. this.prototype.done = sinon.stub()
  46. }
  47. }
  48. Timer.initClass()
  49. return Timer
  50. })()),
  51. }),
  52. }))
  53. ctx.ResourceWriter = (await import(modulePath)).default
  54. ctx.project_id = 'project-id-123'
  55. ctx.basePath = '/path/to/write/files/to'
  56. return (ctx.callback = sinon.stub())
  57. })
  58. describe('syncResourcesToDisk on a full request', () => {
  59. beforeEach(ctx => {
  60. ctx.resources = ['resource-1-mock', 'resource-2-mock', 'resource-3-mock']
  61. ctx.request = {
  62. project_id: ctx.project_id,
  63. syncState: (ctx.syncState = '0123456789abcdef'),
  64. resources: ctx.resources,
  65. }
  66. ctx.ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
  67. ctx.ResourceWriter._removeExtraneousFiles = sinon.stub().yields(null)
  68. ctx.ResourceStateManager.saveProjectState = sinon.stub().callsArg(3)
  69. return ctx.ResourceWriter.syncResourcesToDisk(
  70. ctx.request,
  71. ctx.basePath,
  72. ctx.callback
  73. )
  74. })
  75. it('should remove old files', ctx => {
  76. return ctx.ResourceWriter._removeExtraneousFiles
  77. .calledWith(ctx.request, ctx.resources, ctx.basePath)
  78. .should.equal(true)
  79. })
  80. it('should write each resource to disk', ctx => {
  81. return Array.from(ctx.resources).map(resource =>
  82. ctx.ResourceWriter._writeResourceToDisk
  83. .calledWith(ctx.project_id, resource, ctx.basePath)
  84. .should.equal(true)
  85. )
  86. })
  87. it('should store the sync state and resource list', ctx => {
  88. return ctx.ResourceStateManager.saveProjectState
  89. .calledWith(ctx.syncState, ctx.resources, ctx.basePath)
  90. .should.equal(true)
  91. })
  92. return it('should call the callback', ctx => {
  93. return ctx.callback.called.should.equal(true)
  94. })
  95. })
  96. describe('syncResourcesToDisk on an incremental update', () => {
  97. beforeEach(ctx => {
  98. ctx.resources = ['resource-1-mock']
  99. ctx.request = {
  100. project_id: ctx.project_id,
  101. syncType: 'incremental',
  102. syncState: (ctx.syncState = '1234567890abcdef'),
  103. resources: ctx.resources,
  104. }
  105. ctx.fullResources = ctx.resources.concat(['file-1'])
  106. ctx.ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
  107. ctx.ResourceWriter._removeExtraneousFiles = sinon
  108. .stub()
  109. .yields(null, (ctx.outputFiles = []), (ctx.allFiles = []))
  110. ctx.ResourceStateManager.checkProjectStateMatches = sinon
  111. .stub()
  112. .callsArgWith(2, null, ctx.fullResources)
  113. ctx.ResourceStateManager.saveProjectState = sinon.stub().callsArg(3)
  114. ctx.ResourceStateManager.checkResourceFiles = sinon.stub().callsArg(3)
  115. return ctx.ResourceWriter.syncResourcesToDisk(
  116. ctx.request,
  117. ctx.basePath,
  118. ctx.callback
  119. )
  120. })
  121. it('should check the sync state matches', ctx => {
  122. return ctx.ResourceStateManager.checkProjectStateMatches
  123. .calledWith(ctx.syncState, ctx.basePath)
  124. .should.equal(true)
  125. })
  126. it('should remove old files', ctx => {
  127. return ctx.ResourceWriter._removeExtraneousFiles
  128. .calledWith(ctx.request, ctx.fullResources, ctx.basePath)
  129. .should.equal(true)
  130. })
  131. it('should check each resource exists', ctx => {
  132. return ctx.ResourceStateManager.checkResourceFiles
  133. .calledWith(ctx.fullResources, ctx.allFiles, ctx.basePath)
  134. .should.equal(true)
  135. })
  136. it('should write each resource to disk', ctx => {
  137. return Array.from(ctx.resources).map(resource =>
  138. ctx.ResourceWriter._writeResourceToDisk
  139. .calledWith(ctx.project_id, resource, ctx.basePath)
  140. .should.equal(true)
  141. )
  142. })
  143. return it('should call the callback', ctx => {
  144. return ctx.callback.called.should.equal(true)
  145. })
  146. })
  147. describe('syncResourcesToDisk on an incremental update when the state does not match', () => {
  148. beforeEach(ctx => {
  149. ctx.resources = ['resource-1-mock']
  150. ctx.request = {
  151. project_id: ctx.project_id,
  152. syncType: 'incremental',
  153. syncState: (ctx.syncState = '1234567890abcdef'),
  154. resources: ctx.resources,
  155. }
  156. ctx.ResourceStateManager.checkProjectStateMatches = sinon
  157. .stub()
  158. .callsArgWith(2, (ctx.error = new Error()))
  159. return ctx.ResourceWriter.syncResourcesToDisk(
  160. ctx.request,
  161. ctx.basePath,
  162. ctx.callback
  163. )
  164. })
  165. it('should check whether the sync state matches', ctx => {
  166. return ctx.ResourceStateManager.checkProjectStateMatches
  167. .calledWith(ctx.syncState, ctx.basePath)
  168. .should.equal(true)
  169. })
  170. return it('should call the callback with an error', ctx => {
  171. return ctx.callback.calledWith(ctx.error).should.equal(true)
  172. })
  173. })
  174. describe('_removeExtraneousFiles', () => {
  175. beforeEach(ctx => {
  176. ctx.output_files = [
  177. {
  178. path: 'output.pdf',
  179. type: 'pdf',
  180. },
  181. {
  182. path: 'extra/file.tex',
  183. type: 'tex',
  184. },
  185. {
  186. path: 'extra.aux',
  187. type: 'aux',
  188. },
  189. {
  190. path: 'cache/_chunk1',
  191. },
  192. {
  193. path: 'figures/image-eps-converted-to.pdf',
  194. type: 'pdf',
  195. },
  196. {
  197. path: 'foo/main-figure0.md5',
  198. type: 'md5',
  199. },
  200. {
  201. path: 'foo/main-figure0.dpth',
  202. type: 'dpth',
  203. },
  204. {
  205. path: 'foo/main-figure0.pdf',
  206. type: 'pdf',
  207. },
  208. {
  209. path: '_minted-main/default-pyg-prefix.pygstyle',
  210. type: 'pygstyle',
  211. },
  212. {
  213. path: '_minted-main/default.pygstyle',
  214. type: 'pygstyle',
  215. },
  216. {
  217. path: '_minted-main/35E248B60965545BD232AE9F0FE9750D504A7AF0CD3BAA7542030FC560DFCC45.pygtex',
  218. type: 'pygtex',
  219. },
  220. {
  221. path: '_markdown_main/30893013dec5d869a415610079774c2f.md.tex',
  222. type: 'tex',
  223. },
  224. {
  225. path: 'output.stdout',
  226. },
  227. {
  228. path: 'output.stderr',
  229. },
  230. {
  231. path: 'keepdir/file',
  232. },
  233. {
  234. path: '.other/keepdir/.subdir/file',
  235. },
  236. ]
  237. ctx.resources = 'mock-resources'
  238. ctx.request = {
  239. project_id: ctx.project_id,
  240. syncType: 'incremental',
  241. syncState: (ctx.syncState = '1234567890abcdef'),
  242. resources: ctx.resources,
  243. metricsOpts: { path: 'foo' },
  244. }
  245. ctx.OutputFileFinder.findOutputFiles = sinon
  246. .stub()
  247. .callsArgWith(2, null, ctx.output_files)
  248. ctx.ResourceWriter._deleteFileIfNotDirectory = sinon.stub().callsArg(1)
  249. return ctx.ResourceWriter._removeExtraneousFiles(
  250. ctx.request,
  251. ctx.resources,
  252. ctx.basePath,
  253. ctx.callback
  254. )
  255. })
  256. it('should find the existing output files', ctx => {
  257. return ctx.OutputFileFinder.findOutputFiles
  258. .calledWith(ctx.resources, ctx.basePath)
  259. .should.equal(true)
  260. })
  261. it('should delete the output files', ctx => {
  262. return ctx.ResourceWriter._deleteFileIfNotDirectory
  263. .calledWith(path.join(ctx.basePath, 'output.pdf'))
  264. .should.equal(true)
  265. })
  266. it('should delete the stdout log file', ctx => {
  267. return ctx.ResourceWriter._deleteFileIfNotDirectory
  268. .calledWith(path.join(ctx.basePath, 'output.stdout'))
  269. .should.equal(true)
  270. })
  271. it('should delete the stderr log file', ctx => {
  272. return ctx.ResourceWriter._deleteFileIfNotDirectory
  273. .calledWith(path.join(ctx.basePath, 'output.stderr'))
  274. .should.equal(true)
  275. })
  276. it('should delete the extra files', ctx => {
  277. return ctx.ResourceWriter._deleteFileIfNotDirectory
  278. .calledWith(path.join(ctx.basePath, 'extra/file.tex'))
  279. .should.equal(true)
  280. })
  281. it('should not delete additionally specified files', ctx => {
  282. return ctx.ResourceWriter._deleteFileIfNotDirectory
  283. .calledWith(path.join(ctx.basePath, 'keepdir/file'))
  284. .should.equal(false)
  285. })
  286. it('should not delete additionally specified hidden files', ctx => {
  287. return ctx.ResourceWriter._deleteFileIfNotDirectory
  288. .calledWith(path.join(ctx.basePath, '.other/keepdir/.subdir/file'))
  289. .should.equal(false)
  290. })
  291. it('should not delete the extra aux files', ctx => {
  292. return ctx.ResourceWriter._deleteFileIfNotDirectory
  293. .calledWith(path.join(ctx.basePath, 'extra.aux'))
  294. .should.equal(false)
  295. })
  296. it('should not delete the knitr cache file', ctx => {
  297. return ctx.ResourceWriter._deleteFileIfNotDirectory
  298. .calledWith(path.join(ctx.basePath, 'cache/_chunk1'))
  299. .should.equal(false)
  300. })
  301. it('should not delete the epstopdf converted files', ctx => {
  302. return ctx.ResourceWriter._deleteFileIfNotDirectory
  303. .calledWith(
  304. path.join(ctx.basePath, 'figures/image-eps-converted-to.pdf')
  305. )
  306. .should.equal(false)
  307. })
  308. it('should not delete the tikz md5 files', ctx => {
  309. return ctx.ResourceWriter._deleteFileIfNotDirectory
  310. .calledWith(path.join(ctx.basePath, 'foo/main-figure0.md5'))
  311. .should.equal(false)
  312. })
  313. it('should not delete the tikz dpth files', ctx => {
  314. return ctx.ResourceWriter._deleteFileIfNotDirectory
  315. .calledWith(path.join(ctx.basePath, 'foo/main-figure0.dpth'))
  316. .should.equal(false)
  317. })
  318. it('should not delete the tikz pdf files', ctx => {
  319. return ctx.ResourceWriter._deleteFileIfNotDirectory
  320. .calledWith(path.join(ctx.basePath, 'foo/main-figure0.pdf'))
  321. .should.equal(false)
  322. })
  323. it('should not delete the minted pygstyle files', ctx => {
  324. return ctx.ResourceWriter._deleteFileIfNotDirectory
  325. .calledWith(
  326. path.join(ctx.basePath, '_minted-main/default-pyg-prefix.pygstyle')
  327. )
  328. .should.equal(false)
  329. })
  330. it('should not delete the minted default pygstyle files', ctx => {
  331. return ctx.ResourceWriter._deleteFileIfNotDirectory
  332. .calledWith(path.join(ctx.basePath, '_minted-main/default.pygstyle'))
  333. .should.equal(false)
  334. })
  335. it('should not delete the minted default pygtex files', ctx => {
  336. return ctx.ResourceWriter._deleteFileIfNotDirectory
  337. .calledWith(
  338. path.join(
  339. ctx.basePath,
  340. '_minted-main/35E248B60965545BD232AE9F0FE9750D504A7AF0CD3BAA7542030FC560DFCC45.pygtex'
  341. )
  342. )
  343. .should.equal(false)
  344. })
  345. it('should not delete the markdown md.tex files', ctx => {
  346. return ctx.ResourceWriter._deleteFileIfNotDirectory
  347. .calledWith(
  348. path.join(
  349. ctx.basePath,
  350. '_markdown_main/30893013dec5d869a415610079774c2f.md.tex'
  351. )
  352. )
  353. .should.equal(false)
  354. })
  355. it('should call the callback', ctx => {
  356. return ctx.callback.called.should.equal(true)
  357. })
  358. return it('should time the request', ctx => {
  359. return ctx.Metrics.Timer.prototype.done.called.should.equal(true)
  360. })
  361. })
  362. describe('_writeResourceToDisk', () => {
  363. describe('with a url based resource', () => {
  364. beforeEach(ctx => {
  365. ctx.fs.mkdir = sinon.stub().callsArg(2)
  366. ctx.resource = {
  367. path: 'main.tex',
  368. url: 'http://www.example.com/primary/main.tex',
  369. fallbackURL: 'http://fallback.example.com/fallback/main.tex',
  370. modified: Date.now(),
  371. }
  372. ctx.UrlCache.downloadUrlToFile = sinon
  373. .stub()
  374. .callsArgWith(5, 'fake error downloading file')
  375. return ctx.ResourceWriter._writeResourceToDisk(
  376. ctx.project_id,
  377. ctx.resource,
  378. ctx.basePath,
  379. ctx.callback
  380. )
  381. })
  382. it('should ensure the directory exists', ctx => {
  383. ctx.fs.mkdir
  384. .calledWith(path.dirname(path.join(ctx.basePath, ctx.resource.path)))
  385. .should.equal(true)
  386. })
  387. it('should write the URL from the cache', ctx => {
  388. return ctx.UrlCache.downloadUrlToFile
  389. .calledWith(
  390. ctx.project_id,
  391. ctx.resource.url,
  392. ctx.resource.fallbackURL,
  393. path.join(ctx.basePath, ctx.resource.path),
  394. ctx.resource.modified
  395. )
  396. .should.equal(true)
  397. })
  398. it('should call the callback', ctx => {
  399. return ctx.callback.called.should.equal(true)
  400. })
  401. return it('should not return an error if the resource writer errored', ctx => {
  402. return expect(ctx.callback.args[0][0]).not.to.exist
  403. })
  404. })
  405. describe('with a content based resource', () => {
  406. beforeEach(ctx => {
  407. ctx.resource = {
  408. path: 'main.tex',
  409. content: 'Hello world',
  410. }
  411. ctx.fs.writeFile = sinon.stub().callsArg(2)
  412. ctx.fs.mkdir = sinon.stub().callsArg(2)
  413. return ctx.ResourceWriter._writeResourceToDisk(
  414. ctx.project_id,
  415. ctx.resource,
  416. ctx.basePath,
  417. ctx.callback
  418. )
  419. })
  420. it('should ensure the directory exists', ctx => {
  421. return ctx.fs.mkdir
  422. .calledWith(path.dirname(path.join(ctx.basePath, ctx.resource.path)))
  423. .should.equal(true)
  424. })
  425. it('should write the contents to disk', ctx => {
  426. return ctx.fs.writeFile
  427. .calledWith(
  428. path.join(ctx.basePath, ctx.resource.path),
  429. ctx.resource.content
  430. )
  431. .should.equal(true)
  432. })
  433. return it('should call the callback', ctx => {
  434. return ctx.callback.called.should.equal(true)
  435. })
  436. })
  437. return describe('with a file path that breaks out of the root folder', () => {
  438. beforeEach(ctx => {
  439. ctx.resource = {
  440. path: '../../main.tex',
  441. content: 'Hello world',
  442. }
  443. ctx.fs.writeFile = sinon.stub().callsArg(2)
  444. return ctx.ResourceWriter._writeResourceToDisk(
  445. ctx.project_id,
  446. ctx.resource,
  447. ctx.basePath,
  448. ctx.callback
  449. )
  450. })
  451. it('should not write to disk', ctx => {
  452. return ctx.fs.writeFile.called.should.equal(false)
  453. })
  454. it('should return an error', ctx => {
  455. ctx.callback.calledWith(sinon.match(Error)).should.equal(true)
  456. const message = ctx.callback.args[0][0].message
  457. expect(message).to.include('resource path is outside root directory')
  458. })
  459. })
  460. })
  461. return describe('checkPath', () => {
  462. describe('with a valid path', () => {
  463. beforeEach(ctx => {
  464. return ctx.ResourceWriter.checkPath('foo', 'bar', ctx.callback)
  465. })
  466. return it('should return the joined path', ctx => {
  467. return ctx.callback.calledWith(null, 'foo/bar').should.equal(true)
  468. })
  469. })
  470. describe('with an invalid path', () => {
  471. beforeEach(ctx => {
  472. ctx.ResourceWriter.checkPath('foo', 'baz/../../bar', ctx.callback)
  473. })
  474. it('should return an error', ctx => {
  475. ctx.callback.calledWith(sinon.match(Error)).should.equal(true)
  476. const message = ctx.callback.args[0][0].message
  477. expect(message).to.include('resource path is outside root directory')
  478. })
  479. })
  480. describe('with another invalid path matching on a prefix', () => {
  481. beforeEach(ctx => {
  482. return ctx.ResourceWriter.checkPath(
  483. 'foo',
  484. '../foobar/baz',
  485. ctx.callback
  486. )
  487. })
  488. it('should return an error', ctx => {
  489. ctx.callback.calledWith(sinon.match(Error)).should.equal(true)
  490. const message = ctx.callback.args[0][0].message
  491. expect(message).to.include('resource path is outside root directory')
  492. })
  493. })
  494. })
  495. })