From c2ebaaa338b3d546bf422ddb37058f3f505f85ef Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 10 Apr 2014 12:44:46 +0100 Subject: [PATCH] Split lines on Windows line endings too --- .../app/coffee/ShareJsUpdateManager.coffee | 5 +- .../coffee/ShareJsUpdateManagerTests.coffee | 151 +++++++----------- 2 files changed, 57 insertions(+), 99 deletions(-) diff --git a/services/document-updater/app/coffee/ShareJsUpdateManager.coffee b/services/document-updater/app/coffee/ShareJsUpdateManager.coffee index 9cde95492b..5f3cba4fbc 100644 --- a/services/document-updater/app/coffee/ShareJsUpdateManager.coffee +++ b/services/document-updater/app/coffee/ShareJsUpdateManager.coffee @@ -44,10 +44,7 @@ module.exports = ShareJsUpdateManager = if error? @_sendError(project_id, doc_id, error) return callback(error) - if typeof data.snapshot == "string" - docLines = data.snapshot.split("\n") - else - docLines = data.snapshot.lines + docLines = data.snapshot.split(/\r\n|\n|\r/) callback(null, docLines, data.v) _listenForOps: (model) -> diff --git a/services/document-updater/test/unit/coffee/ShareJsUpdateManagerTests.coffee b/services/document-updater/test/unit/coffee/ShareJsUpdateManagerTests.coffee index af5a475836..20e737fc97 100644 --- a/services/document-updater/test/unit/coffee/ShareJsUpdateManagerTests.coffee +++ b/services/document-updater/test/unit/coffee/ShareJsUpdateManagerTests.coffee @@ -29,113 +29,74 @@ describe "ShareJsUpdateManager", -> @ShareJsUpdateManager.getNewShareJsModel = sinon.stub().returns(@model) @ShareJsUpdateManager._listenForOps = sinon.stub() @ShareJsUpdateManager.removeDocFromCache = sinon.stub().callsArg(1) + @updates = [ + {p: 4, t: "foo"} + {p: 6, t: "bar"} + ] + @updatedDocLines = ["one", "two"] - describe "with a text document", -> - beforeEach -> - @updates = [ - {p: 4, t: "foo"} - {p: 6, t: "bar"} - ] - @updatedDocLines = ["one", "two"] + describe "successfully", -> + beforeEach (done) -> + @model.getSnapshot.callsArgWith(1, null, {snapshot: @updatedDocLines.join("\n"), v: @version}) + @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) => + @callback(err, docLines, version) + done() - describe "successfully", -> - beforeEach (done) -> - @model.getSnapshot.callsArgWith(1, null, {snapshot: @updatedDocLines.join("\n"), v: @version}) - @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) => - @callback(err, docLines, version) - done() + it "should create a new ShareJs model", -> + @ShareJsUpdateManager.getNewShareJsModel + .called.should.equal true - it "should create a new ShareJs model", -> - @ShareJsUpdateManager.getNewShareJsModel - .called.should.equal true + it "should listen for ops on the model", -> + @ShareJsUpdateManager._listenForOps + .calledWith(@model) + .should.equal true - it "should listen for ops on the model", -> - @ShareJsUpdateManager._listenForOps - .calledWith(@model) - .should.equal true + it "should send each update to ShareJs", -> + for update in @updates + @model.applyOp + .calledWith("#{@project_id}:#{@doc_id}", update).should.equal true - it "should send each update to ShareJs", -> - for update in @updates - @model.applyOp - .calledWith("#{@project_id}:#{@doc_id}", update).should.equal true + it "should get the updated doc lines", -> + @model.getSnapshot + .calledWith("#{@project_id}:#{@doc_id}") + .should.equal true - it "should get the updated doc lines", -> - @model.getSnapshot - .calledWith("#{@project_id}:#{@doc_id}") - .should.equal true + it "should return the updated doc lines", -> + @callback.calledWith(null, @updatedDocLines, @version).should.equal true - it "should return the updated doc lines", -> - @callback.calledWith(null, @updatedDocLines, @version).should.equal true + describe "when applyOp fails", -> + beforeEach (done) -> + @error = new Error("Something went wrong") + @ShareJsUpdateManager._sendError = sinon.stub() + @model.applyOp = sinon.stub().callsArgWith(2, @error) + @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) => + @callback(err, docLines, version) + done() - describe "when applyOp fails", -> - beforeEach (done) -> - @error = new Error("Something went wrong") - @ShareJsUpdateManager._sendError = sinon.stub() - @model.applyOp = sinon.stub().callsArgWith(2, @error) - @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) => - @callback(err, docLines, version) - done() + it "should call sendError with the error", -> + @ShareJsUpdateManager._sendError + .calledWith(@project_id, @doc_id, @error) + .should.equal true - it "should call sendError with the error", -> - @ShareJsUpdateManager._sendError - .calledWith(@project_id, @doc_id, @error) - .should.equal true + it "should call the callback with the error", -> + @callback.calledWith(@error).should.equal true - it "should call the callback with the error", -> - @callback.calledWith(@error).should.equal true + describe "when getSnapshot fails", -> + beforeEach (done) -> + @error = new Error("Something went wrong") + @ShareJsUpdateManager._sendError = sinon.stub() + @model.getSnapshot.callsArgWith(1, @error) + @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) => + @callback(err, docLines, version) + done() - describe "when getSnapshot fails", -> - beforeEach (done) -> - @error = new Error("Something went wrong") - @ShareJsUpdateManager._sendError = sinon.stub() - @model.getSnapshot.callsArgWith(1, @error) - @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) => - @callback(err, docLines, version) - done() + it "should call sendError with the error", -> + @ShareJsUpdateManager._sendError + .calledWith(@project_id, @doc_id, @error) + .should.equal true - it "should call sendError with the error", -> - @ShareJsUpdateManager._sendError - .calledWith(@project_id, @doc_id, @error) - .should.equal true - - it "should call the callback with the error", -> - @callback.calledWith(@error).should.equal true - - describe "with a JSON document", -> - beforeEach -> - @updates = [ - {p: ["lines", 0], dl: { foo: "bar "}} - ] - @docLines = [text: "one", text: "two"] - - describe "successfully", -> - beforeEach (done) -> - @model.getSnapshot.callsArgWith(1, null, {snapshot: {lines: @docLines}, v: @version}) - @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) => - @callback(err, docLines, version) - done() - - it "should create a new ShareJs model", -> - @ShareJsUpdateManager.getNewShareJsModel - .called.should.equal true - - it "should listen for ops on the model", -> - @ShareJsUpdateManager._listenForOps - .calledWith(@model) - .should.equal true - - it "should send each update to ShareJs", -> - for update in @updates - @model.applyOp - .calledWith("#{@project_id}:#{@doc_id}", update).should.equal true - - it "should get the updated doc lines", -> - @model.getSnapshot - .calledWith("#{@project_id}:#{@doc_id}") - .should.equal true - - it "should return the updated doc lines", -> - @callback.calledWith(null, @docLines, @version).should.equal true + it "should call the callback with the error", -> + @callback.calledWith(@error).should.equal true describe "_listenForOps", -> beforeEach ->