ResourceWriterTests.js 16 KB

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