ResourceWriterTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 should = require('chai').should()
  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. requires: {
  26. fs: (this.fs = {
  27. mkdir: sinon.stub().callsArg(1),
  28. unlink: sinon.stub().callsArg(1)
  29. }),
  30. './ResourceStateManager': (this.ResourceStateManager = {}),
  31. wrench: (this.wrench = {}),
  32. './UrlCache': (this.UrlCache = {}),
  33. mkdirp: (this.mkdirp = sinon.stub().callsArg(1)),
  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.resource = {
  318. path: 'main.tex',
  319. url: 'http://www.example.com/main.tex',
  320. modified: Date.now()
  321. }
  322. this.UrlCache.downloadUrlToFile = sinon
  323. .stub()
  324. .callsArgWith(4, 'fake error downloading file')
  325. return this.ResourceWriter._writeResourceToDisk(
  326. this.project_id,
  327. this.resource,
  328. this.basePath,
  329. this.callback
  330. )
  331. })
  332. it('should ensure the directory exists', function() {
  333. return this.mkdirp
  334. .calledWith(
  335. path.dirname(path.join(this.basePath, this.resource.path))
  336. )
  337. .should.equal(true)
  338. })
  339. it('should write the URL from the cache', function() {
  340. return this.UrlCache.downloadUrlToFile
  341. .calledWith(
  342. this.project_id,
  343. this.resource.url,
  344. path.join(this.basePath, this.resource.path),
  345. this.resource.modified
  346. )
  347. .should.equal(true)
  348. })
  349. it('should call the callback', function() {
  350. return this.callback.called.should.equal(true)
  351. })
  352. return it('should not return an error if the resource writer errored', function() {
  353. return should.not.exist(this.callback.args[0][0])
  354. })
  355. })
  356. describe('with a content based resource', function() {
  357. beforeEach(function() {
  358. this.resource = {
  359. path: 'main.tex',
  360. content: 'Hello world'
  361. }
  362. this.fs.writeFile = sinon.stub().callsArg(2)
  363. return this.ResourceWriter._writeResourceToDisk(
  364. this.project_id,
  365. this.resource,
  366. this.basePath,
  367. this.callback
  368. )
  369. })
  370. it('should ensure the directory exists', function() {
  371. return this.mkdirp
  372. .calledWith(
  373. path.dirname(path.join(this.basePath, this.resource.path))
  374. )
  375. .should.equal(true)
  376. })
  377. it('should write the contents to disk', function() {
  378. return this.fs.writeFile
  379. .calledWith(
  380. path.join(this.basePath, this.resource.path),
  381. this.resource.content
  382. )
  383. .should.equal(true)
  384. })
  385. return it('should call the callback', function() {
  386. return this.callback.called.should.equal(true)
  387. })
  388. })
  389. return describe('with a file path that breaks out of the root folder', function() {
  390. beforeEach(function() {
  391. this.resource = {
  392. path: '../../main.tex',
  393. content: 'Hello world'
  394. }
  395. this.fs.writeFile = sinon.stub().callsArg(2)
  396. return this.ResourceWriter._writeResourceToDisk(
  397. this.project_id,
  398. this.resource,
  399. this.basePath,
  400. this.callback
  401. )
  402. })
  403. it('should not write to disk', function() {
  404. return this.fs.writeFile.called.should.equal(false)
  405. })
  406. return it('should return an error', function() {
  407. return this.callback
  408. .calledWith(new Error('resource path is outside root directory'))
  409. .should.equal(true)
  410. })
  411. })
  412. })
  413. return describe('checkPath', function() {
  414. describe('with a valid path', function() {
  415. beforeEach(function() {
  416. return this.ResourceWriter.checkPath('foo', 'bar', this.callback)
  417. })
  418. return it('should return the joined path', function() {
  419. return this.callback.calledWith(null, 'foo/bar').should.equal(true)
  420. })
  421. })
  422. describe('with an invalid path', function() {
  423. beforeEach(function() {
  424. return this.ResourceWriter.checkPath(
  425. 'foo',
  426. 'baz/../../bar',
  427. this.callback
  428. )
  429. })
  430. return it('should return an error', function() {
  431. return this.callback
  432. .calledWith(new Error('resource path is outside root directory'))
  433. .should.equal(true)
  434. })
  435. })
  436. return 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. return it('should return an error', function() {
  445. return this.callback
  446. .calledWith(new Error('resource path is outside root directory'))
  447. .should.equal(true)
  448. })
  449. })
  450. })
  451. })