LockManager.coffee 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/LockManager'
  5. Path = require "path"
  6. Errors = require "../../../app/js/Errors"
  7. describe "LockManager", ->
  8. beforeEach ->
  9. @LockManager = SandboxedModule.require modulePath, requires:
  10. "settings-sharelatex": {}
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  12. "lockfile": @Lockfile = {}
  13. @lockFile = "/local/compile/directory/.project-lock"
  14. describe "runWithLock", ->
  15. beforeEach ->
  16. @runner = sinon.stub().callsArgWith(0, null, "foo", "bar")
  17. @callback = sinon.stub()
  18. describe "normally", ->
  19. beforeEach ->
  20. @Lockfile.lock = sinon.stub().callsArgWith(2, null)
  21. @Lockfile.unlock = sinon.stub().callsArgWith(1, null)
  22. @LockManager.runWithLock @lockFile, @runner, @callback
  23. it "should run the compile", ->
  24. @runner
  25. .calledWith()
  26. .should.equal true
  27. it "should call the callback with the response from the compile", ->
  28. @callback
  29. .calledWithExactly(null, "foo", "bar")
  30. .should.equal true
  31. describe "when the project is locked", ->
  32. beforeEach ->
  33. @error = new Error()
  34. @error.code = "EEXIST"
  35. @Lockfile.lock = sinon.stub().callsArgWith(2,@error)
  36. @Lockfile.unlock = sinon.stub().callsArgWith(1, null)
  37. @LockManager.runWithLock @lockFile, @runner, @callback
  38. it "should not run the compile", ->
  39. @runner
  40. .called
  41. .should.equal false
  42. it "should return an error", ->
  43. error = new Errors.AlreadyCompilingError()
  44. @callback
  45. .calledWithExactly(error)
  46. .should.equal true