ResourceWriterTests.js 17 KB

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