Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stderr bug #121 #128

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
],
"author": "Quentin Rossetti <quentin.rossetti@gmail.com>",
"contributors": [
{"name": "Jessé LIBEN", "email": "jliben@gmail.com"}
{"name": "Jessé LIBEN", "email": "jliben@gmail.com"},
{"name": "Daniel Amado", "email": "velkan14@gmail.com"}
],
"license": "ISC",
"bugs": {
Expand Down
12 changes: 11 additions & 1 deletion src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ export const assign = (stream, err) => {
return stream
}

// Just append the buffer to the stream. The error is created before emitting on close
export const append = (stream, buffer) => {
if (stream.stderr) {
stream.stderr += buffer
} else {
stream.stderr = buffer
}
return stream
}

export const fromBuffer = chunk => {
const stderr = chunk.toString()
const match = stderr.match(ERROR)
Expand All @@ -36,4 +46,4 @@ export const fromBuffer = chunk => {
return err
}

export default { assign, fromBuffer }
export default { append, assign, fromBuffer }
12 changes: 8 additions & 4 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ export const onErrorFactory = ({ Err }) => (stream, err) => {
}

export const onStderrFactory = ({ Err }) => (stream, buffer) => {
const err = Err.fromBuffer(buffer)
Err.assign(stream, err)
debug('error: from stderr: %O', err)
Err.append(stream, buffer)
debug('error: append from stderr: %O', buffer)
return stream
}

Expand Down Expand Up @@ -88,7 +87,12 @@ export const onStdoutFactory = ({ Maybe }) => (stream, chunk) => {
return stream
}

export const onEndFactory = () => (stream) => {
export const onEndFactory = ({ Err }) => (stream) => {
if (stream.stderr) {
const err = Err.fromBuffer(stream.stderr)
Err.assign(stream, err)
debug('error: from stderr: %O', err)
}
if (stream.err) {
stream.emit('error', stream.err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const listenFactory = ({ Lifecycle, Err, Maybe }) => seven => {
errorHandler: onErrorFactory({ Err }),
stderrHandler: onStderrFactory({ Err }),
stdoutHandler: onStdoutFactory({ Maybe }),
endHandler: onEndFactory()
endHandler: onEndFactory({ Err })
})(seven)
return seven
}
Expand Down
33 changes: 22 additions & 11 deletions test/unit/events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ describe('Unit: events.js', function () {

describe('onStderrFactory()', function () {
it('should operate on stream and error', function () {
const sevenFake = { testprop: null }
const sevenFake = {}
const bufferFake = Buffer.from('42')
const ErrFake = {
assign: (stream, err) => {
stream.testprop = err
},
fromBuffer: (buffer) => {
return buffer.toString()
append: (stream, err) => {
stream.stderr = err
}
}
const onError = Events.onStderrFactory({ Err: ErrFake })
onError(sevenFake, bufferFake)
expect(sevenFake.testprop).to.eql('42')
expect(sevenFake.stderr).to.eql(bufferFake)
})
})

Expand Down Expand Up @@ -71,7 +68,8 @@ describe('Unit: events.js', function () {
it('should flow the normal way', function () {
// 1. setup
const sevenFake = {
_stage: STAGE_HEADERS
_stage: STAGE_HEADERS,
_isProgressFlag: true
}
// 2. run
const onStdout = Events.onStdoutFactory({ Maybe })
Expand All @@ -98,7 +96,8 @@ describe('Unit: events.js', function () {
// 1. setup
const sevenFake = {
_stage: STAGE_HEADERS,
_dataType: 'symbol'
_dataType: 'symbol',
_isProgressFlag: true
}
// 2. run
const onStdout = Events.onStdoutFactory({ Maybe })
Expand All @@ -124,8 +123,20 @@ describe('Unit: events.js', function () {
})

describe('onEndFactory()', function () {
const ErrFake = {
append: (stream, err) => {
stream.stderr = err
},
assign: (stream, err) => {
stream.testprop = err
},
fromBuffer: (buffer) => {
return buffer.toString()
}
}

it('should emit error and close given error in process', function (done) {
const onEnd = Events.onEndFactory()
const onEnd = Events.onEndFactory({ ErrFake })
const sevenFake = new Readable({ read () {} })
const errFake = new Error('FakeError')
let counterError = 0
Expand All @@ -142,7 +153,7 @@ describe('Unit: events.js', function () {
})

it('should emit end event', function (done) {
const onEnd = Events.onEndFactory()
const onEnd = Events.onEndFactory({ ErrFake })
const sevenFake = new Readable({ read () {} })
let counterClose = 0
sevenFake.on('end', () => { ++counterClose })
Expand Down