Просмотр исходного кода

Split lines on Windows line endings too

James Allen 12 лет назад
Родитель
Сommit
c2ebaaa338

+ 1 - 4
services/document-updater/app/coffee/ShareJsUpdateManager.coffee

@@ -44,10 +44,7 @@ module.exports = ShareJsUpdateManager =
 				if error?
 				if error?
 					@_sendError(project_id, doc_id, error)
 					@_sendError(project_id, doc_id, error)
 					return callback(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)
 				callback(null, docLines, data.v)
 
 
 	_listenForOps: (model) ->
 	_listenForOps: (model) ->

+ 67 - 106
services/document-updater/test/unit/coffee/ShareJsUpdateManagerTests.coffee

@@ -29,113 +29,74 @@ describe "ShareJsUpdateManager", ->
 			@ShareJsUpdateManager.getNewShareJsModel = sinon.stub().returns(@model)
 			@ShareJsUpdateManager.getNewShareJsModel = sinon.stub().returns(@model)
 			@ShareJsUpdateManager._listenForOps = sinon.stub()
 			@ShareJsUpdateManager._listenForOps = sinon.stub()
 			@ShareJsUpdateManager.removeDocFromCache = sinon.stub().callsArg(1)
 			@ShareJsUpdateManager.removeDocFromCache = sinon.stub().callsArg(1)
+			@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()
+
+			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
 
 
-		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()
-
-				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, @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()
-
-				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 "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 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 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, @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()
+
+			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 "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 the callback with the error", ->
+				@callback.calledWith(@error).should.equal true
 
 
 	describe "_listenForOps", ->
 	describe "_listenForOps", ->
 		beforeEach ->
 		beforeEach ->