ResourceWriterTests.js 14 KB

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