Skip to content

Commit

Permalink
server: fixes #977, capture stdout in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mann committed Dec 25, 2017
1 parent d54156e commit b6d1481
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
14 changes: 13 additions & 1 deletion packages/server/lib/stdout.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
_write = process.stdout.write
_log = process.log

module.exports = {
capture: ->
Expand All @@ -7,6 +8,16 @@ module.exports = {
## lazily backup write to enable
## injection
write = process.stdout.write
log = process.log

## electron adds a new process.log
## method for windows instead of process.stdout.write
## https://github.com/cypress-io/cypress/issues/977
if log
process.log = (str) ->
logs.push(str)

log.apply(@, arguments)

process.stdout.write = (str) ->
logs.push(str)
Expand All @@ -20,6 +31,7 @@ module.exports = {
}

restore: ->
## restore to the original write
## restore to the originals
process.stdout.write = _write
process.log = _log
}
62 changes: 44 additions & 18 deletions packages/server/test/unit/stdout_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,53 @@ require("../spec_helper")
stdout = require("#{root}lib/stdout")

describe "lib/stdout", ->
beforeEach ->
@write = @sandbox.spy(process.stdout, "write")
@captured = stdout.capture()

afterEach ->
stdout.restore()

it "slurps up stdout", ->
console.log("foo")
console.log("bar")
process.stdout.write("baz")
context "process.stdout.write", ->
beforeEach ->
@write = @sandbox.spy(process.stdout, "write")
@captured = stdout.capture()

it "slurps up stdout", ->
console.log("foo")
console.log("bar")
process.stdout.write("baz")

expect(@captured.data).to.deep.eq([
"foo\n"
"bar\n"
"baz"
])

expect(@captured.toString()).to.eq("foo\nbar\nbaz")

## should still call through to write
expect(@write).to.be.calledWith("foo\n")
expect(@write).to.be.calledWith("bar\n")
expect(@write).to.be.calledWith("baz")

context "process.log", ->
beforeEach ->
@log = process.log
@logStub = process.log = @sandbox.stub()

@captured = stdout.capture()

afterEach ->
process.log = @log

it "slurps up logs", ->
process.log("foo\n")
process.log("bar\n")

expect(@captured.data).to.deep.eq([
"foo\n"
"bar\n"
"baz"
])
expect(@captured.data).to.deep.eq([
"foo\n"
"bar\n"
])

expect(@captured.toString()).to.eq("foo\nbar\nbaz")
expect(@captured.toString()).to.eq("foo\nbar\n")

## should still call through to write
expect(@write).to.be.calledWith("foo\n")
expect(@write).to.be.calledWith("bar\n")
expect(@write).to.be.calledWith("baz")
## should still call through to write
expect(@logStub).to.be.calledWith("foo\n")
expect(@logStub).to.be.calledWith("bar\n")

0 comments on commit b6d1481

Please sign in to comment.