ResourceStateManagerTests.js 5.3 KB

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