ResourceWriterTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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.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. }
  234. this.OutputFileFinder.findOutputFiles = sinon
  235. .stub()
  236. .callsArgWith(2, null, this.output_files)
  237. this.ResourceWriter._deleteFileIfNotDirectory = sinon.stub().callsArg(1)
  238. return this.ResourceWriter._removeExtraneousFiles(
  239. this.request,
  240. this.resources,
  241. this.basePath,
  242. this.callback
  243. )
  244. })
  245. it('should find the existing output files', function () {
  246. return this.OutputFileFinder.findOutputFiles
  247. .calledWith(this.resources, this.basePath)
  248. .should.equal(true)
  249. })
  250. it('should delete the output files', function () {
  251. return this.ResourceWriter._deleteFileIfNotDirectory
  252. .calledWith(path.join(this.basePath, 'output.pdf'))
  253. .should.equal(true)
  254. })
  255. it('should delete the stdout log file', function () {
  256. return this.ResourceWriter._deleteFileIfNotDirectory
  257. .calledWith(path.join(this.basePath, 'output.stdout'))
  258. .should.equal(true)
  259. })
  260. it('should delete the stderr log file', function () {
  261. return this.ResourceWriter._deleteFileIfNotDirectory
  262. .calledWith(path.join(this.basePath, 'output.stderr'))
  263. .should.equal(true)
  264. })
  265. it('should delete the extra files', function () {
  266. return this.ResourceWriter._deleteFileIfNotDirectory
  267. .calledWith(path.join(this.basePath, 'extra/file.tex'))
  268. .should.equal(true)
  269. })
  270. it('should not delete the extra aux files', function () {
  271. return this.ResourceWriter._deleteFileIfNotDirectory
  272. .calledWith(path.join(this.basePath, 'extra.aux'))
  273. .should.equal(false)
  274. })
  275. it('should not delete the knitr cache file', function () {
  276. return this.ResourceWriter._deleteFileIfNotDirectory
  277. .calledWith(path.join(this.basePath, 'cache/_chunk1'))
  278. .should.equal(false)
  279. })
  280. it('should not delete the epstopdf converted files', function () {
  281. return this.ResourceWriter._deleteFileIfNotDirectory
  282. .calledWith(
  283. path.join(this.basePath, 'figures/image-eps-converted-to.pdf')
  284. )
  285. .should.equal(false)
  286. })
  287. it('should not delete the tikz md5 files', function () {
  288. return this.ResourceWriter._deleteFileIfNotDirectory
  289. .calledWith(path.join(this.basePath, 'foo/main-figure0.md5'))
  290. .should.equal(false)
  291. })
  292. it('should not delete the tikz dpth files', function () {
  293. return this.ResourceWriter._deleteFileIfNotDirectory
  294. .calledWith(path.join(this.basePath, 'foo/main-figure0.dpth'))
  295. .should.equal(false)
  296. })
  297. it('should not delete the tikz pdf files', function () {
  298. return this.ResourceWriter._deleteFileIfNotDirectory
  299. .calledWith(path.join(this.basePath, 'foo/main-figure0.pdf'))
  300. .should.equal(false)
  301. })
  302. it('should not delete the minted pygstyle files', function () {
  303. return this.ResourceWriter._deleteFileIfNotDirectory
  304. .calledWith(
  305. path.join(this.basePath, '_minted-main/default-pyg-prefix.pygstyle')
  306. )
  307. .should.equal(false)
  308. })
  309. it('should not delete the minted default pygstyle files', function () {
  310. return this.ResourceWriter._deleteFileIfNotDirectory
  311. .calledWith(path.join(this.basePath, '_minted-main/default.pygstyle'))
  312. .should.equal(false)
  313. })
  314. it('should not delete the minted default pygtex files', function () {
  315. return this.ResourceWriter._deleteFileIfNotDirectory
  316. .calledWith(
  317. path.join(
  318. this.basePath,
  319. '_minted-main/35E248B60965545BD232AE9F0FE9750D504A7AF0CD3BAA7542030FC560DFCC45.pygtex'
  320. )
  321. )
  322. .should.equal(false)
  323. })
  324. it('should not delete the markdown md.tex files', function () {
  325. return this.ResourceWriter._deleteFileIfNotDirectory
  326. .calledWith(
  327. path.join(
  328. this.basePath,
  329. '_markdown_main/30893013dec5d869a415610079774c2f.md.tex'
  330. )
  331. )
  332. .should.equal(false)
  333. })
  334. it('should call the callback', function () {
  335. return this.callback.called.should.equal(true)
  336. })
  337. return it('should time the request', function () {
  338. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  339. })
  340. })
  341. describe('_writeResourceToDisk', function () {
  342. describe('with a url based resource', function () {
  343. beforeEach(function () {
  344. this.fs.mkdir = sinon.stub().callsArg(2)
  345. this.resource = {
  346. path: 'main.tex',
  347. url: 'http://www.example.com/main.tex',
  348. modified: Date.now(),
  349. }
  350. this.UrlCache.downloadUrlToFile = sinon
  351. .stub()
  352. .callsArgWith(4, 'fake error downloading file')
  353. return this.ResourceWriter._writeResourceToDisk(
  354. this.project_id,
  355. this.resource,
  356. this.basePath,
  357. this.callback
  358. )
  359. })
  360. it('should ensure the directory exists', function () {
  361. this.fs.mkdir
  362. .calledWith(
  363. path.dirname(path.join(this.basePath, this.resource.path))
  364. )
  365. .should.equal(true)
  366. })
  367. it('should write the URL from the cache', function () {
  368. return this.UrlCache.downloadUrlToFile
  369. .calledWith(
  370. this.project_id,
  371. this.resource.url,
  372. path.join(this.basePath, this.resource.path),
  373. this.resource.modified
  374. )
  375. .should.equal(true)
  376. })
  377. it('should call the callback', function () {
  378. return this.callback.called.should.equal(true)
  379. })
  380. return it('should not return an error if the resource writer errored', function () {
  381. return expect(this.callback.args[0][0]).not.to.exist
  382. })
  383. })
  384. describe('with a content based resource', function () {
  385. beforeEach(function () {
  386. this.resource = {
  387. path: 'main.tex',
  388. content: 'Hello world',
  389. }
  390. this.fs.writeFile = sinon.stub().callsArg(2)
  391. this.fs.mkdir = sinon.stub().callsArg(2)
  392. return this.ResourceWriter._writeResourceToDisk(
  393. this.project_id,
  394. this.resource,
  395. this.basePath,
  396. this.callback
  397. )
  398. })
  399. it('should ensure the directory exists', function () {
  400. return this.fs.mkdir
  401. .calledWith(
  402. path.dirname(path.join(this.basePath, this.resource.path))
  403. )
  404. .should.equal(true)
  405. })
  406. it('should write the contents to disk', function () {
  407. return this.fs.writeFile
  408. .calledWith(
  409. path.join(this.basePath, this.resource.path),
  410. this.resource.content
  411. )
  412. .should.equal(true)
  413. })
  414. return it('should call the callback', function () {
  415. return this.callback.called.should.equal(true)
  416. })
  417. })
  418. return describe('with a file path that breaks out of the root folder', function () {
  419. beforeEach(function () {
  420. this.resource = {
  421. path: '../../main.tex',
  422. content: 'Hello world',
  423. }
  424. this.fs.writeFile = sinon.stub().callsArg(2)
  425. return this.ResourceWriter._writeResourceToDisk(
  426. this.project_id,
  427. this.resource,
  428. this.basePath,
  429. this.callback
  430. )
  431. })
  432. it('should not write to disk', function () {
  433. return this.fs.writeFile.called.should.equal(false)
  434. })
  435. it('should return an error', function () {
  436. this.callback.calledWith(sinon.match(Error)).should.equal(true)
  437. const message = this.callback.args[0][0].message
  438. expect(message).to.include('resource path is outside root directory')
  439. })
  440. })
  441. })
  442. return describe('checkPath', function () {
  443. describe('with a valid path', function () {
  444. beforeEach(function () {
  445. return this.ResourceWriter.checkPath('foo', 'bar', this.callback)
  446. })
  447. return it('should return the joined path', function () {
  448. return this.callback.calledWith(null, 'foo/bar').should.equal(true)
  449. })
  450. })
  451. describe('with an invalid path', function () {
  452. beforeEach(function () {
  453. this.ResourceWriter.checkPath('foo', 'baz/../../bar', this.callback)
  454. })
  455. it('should return an error', function () {
  456. this.callback.calledWith(sinon.match(Error)).should.equal(true)
  457. const message = this.callback.args[0][0].message
  458. expect(message).to.include('resource path is outside root directory')
  459. })
  460. })
  461. describe('with another invalid path matching on a prefix', function () {
  462. beforeEach(function () {
  463. return this.ResourceWriter.checkPath(
  464. 'foo',
  465. '../foobar/baz',
  466. this.callback
  467. )
  468. })
  469. it('should return an error', function () {
  470. this.callback.calledWith(sinon.match(Error)).should.equal(true)
  471. const message = this.callback.args[0][0].message
  472. expect(message).to.include('resource path is outside root directory')
  473. })
  474. })
  475. })
  476. })