ResourceWriterTests.js 16 KB

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