ResourceWriterTests.js 16 KB

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