ResourceWriterTests.js 16 KB

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