diff --git a/spec/buffered-process-spec.coffee b/spec/buffered-process-spec.coffee index 04cff0b6d..1f524d66a 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') @@ -114,3 +114,30 @@ 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') + stdout = '' + endLength = 10 + outputAlwaysEndsWithStew = true + process = new BufferedProcess + command: '/bin/echo' + args: [content] + options: {} + stdout: (lines) -> + stdout += lines + + end = baseContent.substr(baseContent.length - endLength, endLength) + lineEndsWithStew = lines.substr(lines.length - endLength, endLength) is end + expect(lineEndsWithStew).toBeTrue + + outputAlwaysEndsWithStew = outputAlwaysEndsWithStew and lineEndsWithStew + exit: exitCallback + + waitsFor -> exitCallback.callCount is 1 + + runs -> + expect(outputAlwaysEndsWithStew).toBeTrue + expect(stdout).toBe content += '\n' diff --git a/src/buffered-process.coffee b/src/buffered-process.coffee index c7097d711..53934c02d 100644 --- a/src/buffered-process.coffee +++ b/src/buffered-process.coffee @@ -111,11 +111,13 @@ 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) + lineLength = lastNewlineIndex + bufferedLength + 1 + onLines(buffered.substring(0, lineLength)) + buffered = buffered.substring(lineLength) stream.on 'close', => return if @killed