ResourceStateManagerTests.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module');
  13. const sinon = require('sinon');
  14. const should = require('chai').should();
  15. const modulePath = require('path').join(__dirname, '../../../app/js/ResourceStateManager');
  16. const Path = require("path");
  17. const Errors = require("../../../app/js/Errors");
  18. describe("ResourceStateManager", function() {
  19. beforeEach(function() {
  20. this.ResourceStateManager = SandboxedModule.require(modulePath, { requires: {
  21. "fs": (this.fs = {}),
  22. "logger-sharelatex": {log: sinon.stub(), err: sinon.stub()},
  23. "./SafeReader": (this.SafeReader = {})
  24. }
  25. });
  26. this.basePath = "/path/to/write/files/to";
  27. this.resources = [
  28. {path: "resource-1-mock"},
  29. {path: "resource-2-mock"},
  30. {path: "resource-3-mock"}
  31. ];
  32. this.state = "1234567890";
  33. this.resourceFileName = `${this.basePath}/.project-sync-state`;
  34. this.resourceFileContents = `${this.resources[0].path}\n${this.resources[1].path}\n${this.resources[2].path}\nstateHash:${this.state}`;
  35. return this.callback = sinon.stub();
  36. });
  37. describe("saveProjectState", function() {
  38. beforeEach(function() {
  39. return this.fs.writeFile = sinon.stub().callsArg(2);
  40. });
  41. describe("when the state is specified", function() {
  42. beforeEach(function() {
  43. return this.ResourceStateManager.saveProjectState(this.state, this.resources, this.basePath, this.callback);
  44. });
  45. it("should write the resource list to disk", function() {
  46. return this.fs.writeFile
  47. .calledWith(this.resourceFileName, this.resourceFileContents)
  48. .should.equal(true);
  49. });
  50. return it("should call the callback", function() {
  51. return this.callback.called.should.equal(true);
  52. });
  53. });
  54. return describe("when the state is undefined", function() {
  55. beforeEach(function() {
  56. this.state = undefined;
  57. this.fs.unlink = sinon.stub().callsArg(1);
  58. return this.ResourceStateManager.saveProjectState(this.state, this.resources, this.basePath, this.callback);
  59. });
  60. it("should unlink the resource file", function() {
  61. return this.fs.unlink
  62. .calledWith(this.resourceFileName)
  63. .should.equal(true);
  64. });
  65. it("should not write the resource list to disk", function() {
  66. return this.fs.writeFile.called.should.equal(false);
  67. });
  68. return it("should call the callback", function() {
  69. return this.callback.called.should.equal(true);
  70. });
  71. });
  72. });
  73. describe("checkProjectStateMatches", function() {
  74. describe("when the state matches", function() {
  75. beforeEach(function() {
  76. this.SafeReader.readFile = sinon.stub().callsArgWith(3, null, this.resourceFileContents);
  77. return this.ResourceStateManager.checkProjectStateMatches(this.state, this.basePath, this.callback);
  78. });
  79. it("should read the resource file", function() {
  80. return this.SafeReader.readFile
  81. .calledWith(this.resourceFileName)
  82. .should.equal(true);
  83. });
  84. return it("should call the callback with the results", function() {
  85. return this.callback.calledWithMatch(null, this.resources).should.equal(true);
  86. });
  87. });
  88. return describe("when the state does not match", function() {
  89. beforeEach(function() {
  90. this.SafeReader.readFile = sinon.stub().callsArgWith(3, null, this.resourceFileContents);
  91. return this.ResourceStateManager.checkProjectStateMatches("not-the-original-state", this.basePath, this.callback);
  92. });
  93. return it("should call the callback with an error", function() {
  94. const error = new Errors.FilesOutOfSyncError("invalid state for incremental update");
  95. return this.callback.calledWith(error).should.equal(true);
  96. });
  97. });
  98. });
  99. return describe("checkResourceFiles", function() {
  100. describe("when all the files are present", function() {
  101. beforeEach(function() {
  102. this.allFiles = [ this.resources[0].path, this.resources[1].path, this.resources[2].path];
  103. return this.ResourceStateManager.checkResourceFiles(this.resources, this.allFiles, this.basePath, this.callback);
  104. });
  105. return it("should call the callback", function() {
  106. return this.callback.calledWithExactly().should.equal(true);
  107. });
  108. });
  109. describe("when there is a missing file", function() {
  110. beforeEach(function() {
  111. this.allFiles = [ this.resources[0].path, this.resources[1].path];
  112. this.fs.stat = sinon.stub().callsArgWith(1, new Error());
  113. return this.ResourceStateManager.checkResourceFiles(this.resources, this.allFiles, this.basePath, this.callback);
  114. });
  115. return it("should call the callback with an error", function() {
  116. const error = new Errors.FilesOutOfSyncError("resource files missing in incremental update");
  117. return this.callback.calledWith(error).should.equal(true);
  118. });
  119. });
  120. return describe("when a resource contains a relative path", function() {
  121. beforeEach(function() {
  122. this.resources[0].path = "../foo/bar.tex";
  123. this.allFiles = [ this.resources[0].path, this.resources[1].path, this.resources[2].path];
  124. return this.ResourceStateManager.checkResourceFiles(this.resources, this.allFiles, this.basePath, this.callback);
  125. });
  126. return it("should call the callback with an error", function() {
  127. return this.callback.calledWith(new Error("relative path in resource file list")).should.equal(true);
  128. });
  129. });
  130. });
  131. });