Skip to content

Commit

Permalink
fix: reject on non-zero exit codes (serialport#2046)
Browse files Browse the repository at this point in the history
* fix: reject on non-zero exit codes
* fix: use more descriptive error message
  • Loading branch information
Swaagie authored and bailli committed Aug 10, 2021
1 parent 5a98496 commit e417bf5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/bindings/lib/linux-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function listLinux() {
const ports = []
const ude = childProcess.spawn('udevadm', ['info', '-e'])
const lines = ude.stdout.pipe(new Readline())
ude.on('close', code => code && reject(new Error(`Error listing ports udevadm exited with error code: ${code}`)))
ude.on('error', reject)
lines.on('error', reject)

Expand Down
14 changes: 14 additions & 0 deletions packages/bindings/lib/linux-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,18 @@ describe('listLinux', () => {
assert.containSubset(ports, portOutput)
})
})

it('rejects on non-zero exit codes', () => {
const list = listLinux()
listLinux.emit('close', 1)

list.then(
() => {
assert.fail('should not resolve for non-zero exit codes')
},
error => {
assert(error, new Error('Error listing ports udevadm exited with error code: 1'))
}
)
})
})
7 changes: 6 additions & 1 deletion packages/bindings/lib/mocks/linux-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ const EventEmitter = require('events')
const proxyquire = require('proxyquire')
const Readable = require('stream').Readable
let mockPorts
let event

proxyquire.noPreserveCache()
const listLinux = proxyquire('../linux-list', {
child_process: {
spawn() {
const event = new EventEmitter()
const stream = new Readable()
event = new EventEmitter()
event.stdout = stream
stream.push(mockPorts)
stream.push(null)
Expand All @@ -24,6 +25,10 @@ listLinux.setPorts = ports => {
mockPorts = ports
}

listLinux.emit = function() {
event.emit.apply(event, arguments)
}

listLinux.reset = () => {
mockPorts = {}
}
Expand Down

0 comments on commit e417bf5

Please sign in to comment.