From 2d4474eb8b31af3c952bcb68543bb0540d849c5d Mon Sep 17 00:00:00 2001 From: Carl Henderson Date: Mon, 22 Feb 2016 15:13:38 -0500 Subject: [PATCH 1/6] search only new data for new lines rather than entire buffer --- src/buffered-process.coffee | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/buffered-process.coffee b/src/buffered-process.coffee index c7097d711..183a1d99a 100644 --- a/src/buffered-process.coffee +++ b/src/buffered-process.coffee @@ -111,11 +111,12 @@ class BufferedProcess stream.on 'data', (data) => return if @killed + bufferedLength = buffered.length buffered += data - lastNewlineIndex = buffered.lastIndexOf('\n') + lastNewlineIndex = data.lastIndexOf('\n') if lastNewlineIndex isnt -1 - onLines(buffered.substring(0, lastNewlineIndex + 1)) - buffered = buffered.substring(lastNewlineIndex + 1) + onLines(buffered.substring(0, lastNewlineIndex + bufferedLength + 1)) + buffered = buffered.substring(lastNewlineIndex + bufferedLength + 1) stream.on 'close', => return if @killed From d1c8fff86c89e326847385da623f13c64534c2b7 Mon Sep 17 00:00:00 2001 From: joshaber Date: Mon, 14 Mar 2016 13:42:05 -0400 Subject: [PATCH 2/6] Pull line length out into its own variable. --- src/buffered-process.coffee | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/buffered-process.coffee b/src/buffered-process.coffee index 183a1d99a..53934c02d 100644 --- a/src/buffered-process.coffee +++ b/src/buffered-process.coffee @@ -115,8 +115,9 @@ class BufferedProcess buffered += data lastNewlineIndex = data.lastIndexOf('\n') if lastNewlineIndex isnt -1 - onLines(buffered.substring(0, lastNewlineIndex + bufferedLength + 1)) - buffered = buffered.substring(lastNewlineIndex + bufferedLength + 1) + lineLength = lastNewlineIndex + bufferedLength + 1 + onLines(buffered.substring(0, lineLength)) + buffered = buffered.substring(lineLength) stream.on 'close', => return if @killed From 66971abe25e9ec1aed48d7bcbfabe4d8f0ba7bee Mon Sep 17 00:00:00 2001 From: joshaber Date: Mon, 14 Mar 2016 14:12:31 -0400 Subject: [PATCH 3/6] Don't need that trailing space. --- spec/buffered-process-spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/buffered-process-spec.coffee b/spec/buffered-process-spec.coffee index 04cff0b6d..dd166f0dc 100644 --- a/spec/buffered-process-spec.coffee +++ b/spec/buffered-process-spec.coffee @@ -97,7 +97,7 @@ describe "BufferedProcess", -> expect(ChildProcess.spawn.argsForCall[0][1][1]).toBe '/c' expect(ChildProcess.spawn.argsForCall[0][1][2]).toBe '"dir"' - it "calls the specified stdout, stderr, and exit callbacks ", -> + it "calls the specified stdout, stderr, and exit callbacks", -> stdout = '' stderr = '' exitCallback = jasmine.createSpy('exit callback') From d441da9c38a0629ce022f4e5e4013ad1b41daf9e Mon Sep 17 00:00:00 2001 From: joshaber Date: Mon, 14 Mar 2016 14:12:47 -0400 Subject: [PATCH 4/6] Test it with a lot of content. --- spec/buffered-process-spec.coffee | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/buffered-process-spec.coffee b/spec/buffered-process-spec.coffee index dd166f0dc..3d1a66bf8 100644 --- a/spec/buffered-process-spec.coffee +++ b/spec/buffered-process-spec.coffee @@ -114,3 +114,26 @@ describe "BufferedProcess", -> runs -> expect(stderr).toContain 'apm - Atom Package Manager' expect(stdout).toEqual '' + + it "calls the specified stdout callback only with whole lines", -> + exitCallback = jasmine.createSpy('exit callback') + baseContent = "There are dozens of us! Dozens! It's as Ann as the nose on Plain's face. Can you believe that the only reason the club is going under is because it's in a terrifying neighborhood? She calls it a Mayonegg. Waiting for the Emmys. BTW did you know won 6 Emmys and was still canceled early by Fox? COME ON. I'll buy you a hundred George Michaels that you can teach to drive! Never once touched my per diem. I'd go to Craft Service, get some raw veggies, bacon, Cup-A-Soup…baby, I got a stew goin'" + content = (baseContent for _ in [1..200]).join('\n') + outputAlwaysEndsWithStew = true + process = new BufferedProcess + command: '/bin/echo' + args: [content] + options: {} + stdout: (lines) -> + endLength = 10 + end = baseContent.substr(baseContent.length - endLength, endLength) + lineEndsWithStew = lines.substr(lines.length - endLength, endLength) is end + expect(lineEndsWithStew).toBeTrue + + outputAlwaysEndsWithStew = outputAlwaysEndsWithStew && lineEndsWithStew + exit: exitCallback + + waitsFor -> exitCallback.callCount is 1 + + runs -> + expect(outputAlwaysEndsWithStew).toBeTrue From 3601c1c2a869ac1d1d5fc73e9873384c6dde0b86 Mon Sep 17 00:00:00 2001 From: joshaber Date: Mon, 14 Mar 2016 14:18:06 -0400 Subject: [PATCH 5/6] Test the whole output too. --- spec/buffered-process-spec.coffee | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/buffered-process-spec.coffee b/spec/buffered-process-spec.coffee index 3d1a66bf8..fbaee801f 100644 --- a/spec/buffered-process-spec.coffee +++ b/spec/buffered-process-spec.coffee @@ -119,13 +119,16 @@ describe "BufferedProcess", -> exitCallback = jasmine.createSpy('exit callback') baseContent = "There are dozens of us! Dozens! It's as Ann as the nose on Plain's face. Can you believe that the only reason the club is going under is because it's in a terrifying neighborhood? She calls it a Mayonegg. Waiting for the Emmys. BTW did you know won 6 Emmys and was still canceled early by Fox? COME ON. I'll buy you a hundred George Michaels that you can teach to drive! Never once touched my per diem. I'd go to Craft Service, get some raw veggies, bacon, Cup-A-Soup…baby, I got a stew goin'" content = (baseContent for _ in [1..200]).join('\n') + stdout = '' + endLength = 10 outputAlwaysEndsWithStew = true process = new BufferedProcess command: '/bin/echo' args: [content] options: {} stdout: (lines) -> - endLength = 10 + stdout += lines + end = baseContent.substr(baseContent.length - endLength, endLength) lineEndsWithStew = lines.substr(lines.length - endLength, endLength) is end expect(lineEndsWithStew).toBeTrue @@ -137,3 +140,4 @@ describe "BufferedProcess", -> runs -> expect(outputAlwaysEndsWithStew).toBeTrue + expect(stdout).toBe content += '\n' From 0c67fd61ffb0250f556a317496c6a2588aeae1fe Mon Sep 17 00:00:00 2001 From: joshaber Date: Mon, 14 Mar 2016 14:57:58 -0400 Subject: [PATCH 6/6] Hi lint. --- spec/buffered-process-spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/buffered-process-spec.coffee b/spec/buffered-process-spec.coffee index fbaee801f..1f524d66a 100644 --- a/spec/buffered-process-spec.coffee +++ b/spec/buffered-process-spec.coffee @@ -133,7 +133,7 @@ describe "BufferedProcess", -> lineEndsWithStew = lines.substr(lines.length - endLength, endLength) is end expect(lineEndsWithStew).toBeTrue - outputAlwaysEndsWithStew = outputAlwaysEndsWithStew && lineEndsWithStew + outputAlwaysEndsWithStew = outputAlwaysEndsWithStew and lineEndsWithStew exit: exitCallback waitsFor -> exitCallback.callCount is 1