ResourceStateManagerTests.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. should = require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/ResourceStateManager'
  5. Path = require "path"
  6. Errors = require "../../../app/js/Errors"
  7. describe "ResourceStateManager", ->
  8. beforeEach ->
  9. @ResourceStateManager = SandboxedModule.require modulePath, requires:
  10. "fs": @fs = {}
  11. "logger-sharelatex": {log: sinon.stub(), err: sinon.stub()}
  12. "./SafeReader": @SafeReader = {}
  13. @basePath = "/path/to/write/files/to"
  14. @resources = [
  15. {path: "resource-1-mock"}
  16. {path: "resource-2-mock"}
  17. {path: "resource-3-mock"}
  18. ]
  19. @state = "1234567890"
  20. @resourceFileName = "#{@basePath}/.project-sync-state"
  21. @resourceFileContents = "#{@resources[0].path}\n#{@resources[1].path}\n#{@resources[2].path}\nstateHash:#{@state}"
  22. @callback = sinon.stub()
  23. describe "saveProjectState", ->
  24. beforeEach ->
  25. @fs.writeFile = sinon.stub().callsArg(2)
  26. describe "when the state is specified", ->
  27. beforeEach ->
  28. @ResourceStateManager.saveProjectState(@state, @resources, @basePath, @callback)
  29. it "should write the resource list to disk", ->
  30. @fs.writeFile
  31. .calledWith(@resourceFileName, @resourceFileContents)
  32. .should.equal true
  33. it "should call the callback", ->
  34. @callback.called.should.equal true
  35. describe "when the state is undefined", ->
  36. beforeEach ->
  37. @state = undefined
  38. @fs.unlink = sinon.stub().callsArg(1)
  39. @ResourceStateManager.saveProjectState(@state, @resources, @basePath, @callback)
  40. it "should unlink the resource file", ->
  41. @fs.unlink
  42. .calledWith(@resourceFileName)
  43. .should.equal true
  44. it "should not write the resource list to disk", ->
  45. @fs.writeFile.called.should.equal false
  46. it "should call the callback", ->
  47. @callback.called.should.equal true
  48. describe "checkProjectStateMatches", ->
  49. describe "when the state matches", ->
  50. beforeEach ->
  51. @SafeReader.readFile = sinon.stub().callsArgWith(3, null, @resourceFileContents)
  52. @ResourceStateManager.checkProjectStateMatches(@state, @basePath, @callback)
  53. it "should read the resource file", ->
  54. @SafeReader.readFile
  55. .calledWith(@resourceFileName)
  56. .should.equal true
  57. it "should call the callback with the results", ->
  58. @callback.calledWithMatch(null, @resources).should.equal true
  59. describe "when the state does not match", ->
  60. beforeEach ->
  61. @SafeReader.readFile = sinon.stub().callsArgWith(3, null, @resourceFileContents)
  62. @ResourceStateManager.checkProjectStateMatches("not-the-original-state", @basePath, @callback)
  63. it "should call the callback with an error", ->
  64. error = new Errors.FilesOutOfSyncError("invalid state for incremental update")
  65. @callback.calledWith(error).should.equal true
  66. describe "checkResourceFiles", ->
  67. describe "when all the files are present", ->
  68. beforeEach ->
  69. @allFiles = [ @resources[0].path, @resources[1].path, @resources[2].path]
  70. @ResourceStateManager.checkResourceFiles(@resources, @allFiles, @basePath, @callback)
  71. it "should call the callback", ->
  72. @callback.calledWithExactly().should.equal true
  73. describe "when there is a missing file", ->
  74. beforeEach ->
  75. @allFiles = [ @resources[0].path, @resources[1].path]
  76. @fs.stat = sinon.stub().callsArgWith(1, new Error())
  77. @ResourceStateManager.checkResourceFiles(@resources, @allFiles, @basePath, @callback)
  78. it "should call the callback with an error", ->
  79. error = new Errors.FilesOutOfSyncError("resource files missing in incremental update")
  80. @callback.calledWith(error).should.equal true
  81. describe "when a resource contains a relative path", ->
  82. beforeEach ->
  83. @resources[0].path = "../foo/bar.tex"
  84. @allFiles = [ @resources[0].path, @resources[1].path, @resources[2].path]
  85. @ResourceStateManager.checkResourceFiles(@resources, @allFiles, @basePath, @callback)
  86. it "should call the callback with an error", ->
  87. @callback.calledWith(new Error("relative path in resource file list")).should.equal true