|
|
@@ -7,7 +7,11 @@ path = require "path"
|
|
|
describe "ResourceWriter", ->
|
|
|
beforeEach ->
|
|
|
@ResourceWriter = SandboxedModule.require modulePath, requires:
|
|
|
- "fs": @fs = { mkdir: sinon.stub().callsArg(1) }
|
|
|
+ "fs": @fs =
|
|
|
+ mkdir: sinon.stub().callsArg(1)
|
|
|
+ unlink: sinon.stub().callsArg(1)
|
|
|
+ "./ResourceListManager": @ResourceListManager = {}
|
|
|
+ "./ResourceStateManager": @ResourceStateManager = {}
|
|
|
"wrench": @wrench = {}
|
|
|
"./UrlCache" : @UrlCache = {}
|
|
|
"mkdirp" : @mkdirp = sinon.stub().callsArg(1)
|
|
|
@@ -20,7 +24,7 @@ describe "ResourceWriter", ->
|
|
|
@basePath = "/path/to/write/files/to"
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
- describe "syncResourcesToDisk", ->
|
|
|
+ describe "syncResourcesToDisk on a full request", ->
|
|
|
beforeEach ->
|
|
|
@resources = [
|
|
|
"resource-1-mock"
|
|
|
@@ -29,7 +33,15 @@ describe "ResourceWriter", ->
|
|
|
]
|
|
|
@ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
|
|
|
@ResourceWriter._removeExtraneousFiles = sinon.stub().callsArg(2)
|
|
|
- @ResourceWriter.syncResourcesToDisk(@project_id, @resources, @basePath, @callback)
|
|
|
+ @ResourceStateManager.checkProjectStateHashMatches = sinon.stub().callsArg(2)
|
|
|
+ @ResourceStateManager.saveProjectStateHash = sinon.stub().callsArg(2)
|
|
|
+ @ResourceListManager.saveResourceList = sinon.stub().callsArg(2)
|
|
|
+ @ResourceListManager.loadResourceList = sinon.stub().callsArg(1)
|
|
|
+ @ResourceWriter.syncResourcesToDisk({
|
|
|
+ project_id: @project_id
|
|
|
+ syncState: @syncState = "0123456789abcdef"
|
|
|
+ resources: @resources
|
|
|
+ }, @basePath, @callback)
|
|
|
|
|
|
it "should remove old files", ->
|
|
|
@ResourceWriter._removeExtraneousFiles
|
|
|
@@ -42,9 +54,78 @@ describe "ResourceWriter", ->
|
|
|
.calledWith(@project_id, resource, @basePath)
|
|
|
.should.equal true
|
|
|
|
|
|
+ it "should store the sync state", ->
|
|
|
+ @ResourceStateManager.saveProjectStateHash
|
|
|
+ .calledWith(@syncState, @basePath)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should save the resource list", ->
|
|
|
+ @ResourceListManager.saveResourceList
|
|
|
+ .calledWith(@resources, @basePath)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
it "should call the callback", ->
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
+ describe "syncResourcesToDisk on an incremental update", ->
|
|
|
+ beforeEach ->
|
|
|
+ @resources = [
|
|
|
+ "resource-1-mock"
|
|
|
+ ]
|
|
|
+ @ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
|
|
|
+ @ResourceWriter._removeExtraneousFiles = sinon.stub().callsArg(2)
|
|
|
+ @ResourceStateManager.checkProjectStateHashMatches = sinon.stub().callsArg(2)
|
|
|
+ @ResourceStateManager.saveProjectStateHash = sinon.stub().callsArg(2)
|
|
|
+ @ResourceListManager.saveResourceList = sinon.stub().callsArg(2)
|
|
|
+ @ResourceListManager.loadResourceList = sinon.stub().callsArgWith(1, null, @resources)
|
|
|
+ @ResourceWriter.syncResourcesToDisk({
|
|
|
+ project_id: @project_id,
|
|
|
+ syncType: "incremental",
|
|
|
+ syncState: @syncState = "1234567890abcdef",
|
|
|
+ resources: @resources
|
|
|
+ }, @basePath, @callback)
|
|
|
+
|
|
|
+ it "should check the sync state matches", ->
|
|
|
+ @ResourceStateManager.checkProjectStateHashMatches
|
|
|
+ .calledWith(@syncState, @basePath)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should remove old files", ->
|
|
|
+ @ResourceWriter._removeExtraneousFiles
|
|
|
+ .calledWith(@resources, @basePath)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should write each resource to disk", ->
|
|
|
+ for resource in @resources
|
|
|
+ @ResourceWriter._writeResourceToDisk
|
|
|
+ .calledWith(@project_id, resource, @basePath)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should call the callback", ->
|
|
|
+ @callback.called.should.equal true
|
|
|
+
|
|
|
+ describe "syncResourcesToDisk on an incremental update when the state does not match", ->
|
|
|
+ beforeEach ->
|
|
|
+ @resources = [
|
|
|
+ "resource-1-mock"
|
|
|
+ ]
|
|
|
+ @ResourceStateManager.checkProjectStateHashMatches = sinon.stub().callsArgWith(2, @error = new Error())
|
|
|
+ @ResourceWriter.syncResourcesToDisk({
|
|
|
+ project_id: @project_id,
|
|
|
+ syncType: "incremental",
|
|
|
+ syncState: @syncState = "1234567890abcdef",
|
|
|
+ resources: @resources
|
|
|
+ }, @basePath, @callback)
|
|
|
+
|
|
|
+ it "should check whether the sync state matches", ->
|
|
|
+ @ResourceStateManager.checkProjectStateHashMatches
|
|
|
+ .calledWith(@syncState, @basePath)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should call the callback with an error", ->
|
|
|
+ @callback.calledWith(@error).should.equal true
|
|
|
+
|
|
|
+
|
|
|
describe "_removeExtraneousFiles", ->
|
|
|
beforeEach ->
|
|
|
@output_files = [{
|