ResourceWriterTests.js 16 KB

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