ResourceWriterTests.js 16 KB

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