From 6ee5c8471fd1e041ebfba736f1eb708d2764b63e Mon Sep 17 00:00:00 2001 From: Martijn Swaagman Date: Thu, 7 May 2020 05:38:58 +0200 Subject: [PATCH] fix: reject on non-zero exit codes (#2046) * fix: reject on non-zero exit codes * fix: use more descriptive error message --- packages/bindings/lib/linux-list.js | 1 + packages/bindings/lib/linux-list.test.js | 14 ++++++++++++++ packages/bindings/lib/mocks/linux-list.js | 7 ++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/bindings/lib/linux-list.js b/packages/bindings/lib/linux-list.js index 6985eed..63cb95a 100644 --- a/packages/bindings/lib/linux-list.js +++ b/packages/bindings/lib/linux-list.js @@ -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) diff --git a/packages/bindings/lib/linux-list.test.js b/packages/bindings/lib/linux-list.test.js index 6de5214..ec58f85 100644 --- a/packages/bindings/lib/linux-list.test.js +++ b/packages/bindings/lib/linux-list.test.js @@ -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')) + } + ) + }) }) diff --git a/packages/bindings/lib/mocks/linux-list.js b/packages/bindings/lib/mocks/linux-list.js index 537fce7..0ac81f0 100644 --- a/packages/bindings/lib/mocks/linux-list.js +++ b/packages/bindings/lib/mocks/linux-list.js @@ -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) @@ -24,6 +25,10 @@ listLinux.setPorts = ports => { mockPorts = ports } +listLinux.emit = function() { + event.emit.apply(event, arguments) +} + listLinux.reset = () => { mockPorts = {} }