ResourceWriterTests.js 16 KB

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