From cf41ef76bd2dd70ef38fb821a5ff96548b808b05 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Fri, 6 May 2016 23:38:17 -0400 Subject: [PATCH] Update the errors for unix to always include the err string and tests for close (#790) --- src/serialport.cpp | 5 +---- src/serialport_unix.cpp | 6 +++--- test/serialport-bindings.js | 29 ++++++++++++++++++++++++++++- test/serialport-integration.js | 2 +- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/serialport.cpp b/src/serialport.cpp index 4a1d62d..08be8e8 100644 --- a/src/serialport.cpp +++ b/src/serialport.cpp @@ -386,17 +386,15 @@ NAN_METHOD(Close) { void EIO_AfterClose(uv_work_t* req) { Nan::HandleScope scope; - CloseBaton* data = static_cast(req->data); v8::Local argv[1]; if (data->errorString[0]) { argv[0] = v8::Exception::Error(Nan::New(data->errorString).ToLocalChecked()); } else { - argv[0] = Nan::Undefined(); + argv[0] = Nan::Null(); // We don't have an error, so clean up the write queue for that fd - _WriteQueue *q = qForFD(data->fd); if (q) { q->lock(); @@ -407,7 +405,6 @@ void EIO_AfterClose(uv_work_t* req) { del_q->remove(); } q->unlock(); - deleteQForFD(data->fd); } } diff --git a/src/serialport_unix.cpp b/src/serialport_unix.cpp index d076f03..0bf5e89 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -129,7 +129,7 @@ void EIO_Open(uv_work_t* req) { int fd = open(data->path, flags); if (-1 == fd) { - snprintf(data->errorString, sizeof(data->errorString), "Cannot open %s", data->path); + snprintf(data->errorString, sizeof(data->errorString), "Error: %s, cannot open %s", strerror(errno), data->path); return; } @@ -353,7 +353,7 @@ void EIO_Write(uv_work_t* req) { // The write call might be interrupted, if it is we just try again immediately. if (errno != EINTR) { - snprintf(data->errorString, sizeof(data->errorString), "Error %s calling write(...)", strerror(errno) ); + snprintf(data->errorString, sizeof(data->errorString), "Error: %s, calling write", strerror(errno)); return; } @@ -372,7 +372,7 @@ void EIO_Write(uv_work_t* req) { void EIO_Close(uv_work_t* req) { CloseBaton* data = static_cast(req->data); if (-1 == close(data->fd)) { - snprintf(data->errorString, sizeof(data->errorString), "Unable to close fd %d, errno: %d", data->fd, errno); + snprintf(data->errorString, sizeof(data->errorString), "Error: %s, unable to close fd %d", strerror(errno), data->fd); } } diff --git a/test/serialport-bindings.js b/test/serialport-bindings.js index 57f9e90..20a7a2a 100644 --- a/test/serialport-bindings.js +++ b/test/serialport-bindings.js @@ -15,6 +15,8 @@ switch (process.platform) { platform = 'unix'; } +console.log('Testing on ' + platform); + var defaultPortOpenOptions = { baudRate: 9600, parity: 'none', @@ -47,7 +49,7 @@ describe('SerialPortBinding', function () { }); if (!testPort) { - it('Cannot be tested as we have no test ports on ' + platform); + it('Cannot be tested further. Set the TEST_PORT env var with an available serialport for more testing.'); return; } @@ -60,6 +62,31 @@ describe('SerialPortBinding', function () { }); }); + describe('#close', function() { + it('errors when providing a bad file descriptor', function(done) { + SerialPortBinding.close(-1, function(err) { + assert.instanceOf(err, Error); + done(); + }); + }); + + if (!testPort) { + it('Cannot be tested further. Set the TEST_PORT env var with an available serialport for more testing.'); + return; + } + + it('closes an open file descriptor', function(done) { + SerialPortBinding.open(testPort, defaultPortOpenOptions, function(err, fd) { + assert.isNull(err); + assert.isNumber(fd); + SerialPortBinding.close(fd, function(err) { + assert.isNull(err); + done(); + }); + }); + }); + }); + describe('#list', function() { it('returns an array', function(done) { SerialPortBinding.list(function(err, data) { diff --git a/test/serialport-integration.js b/test/serialport-integration.js index 4a2fccd..a6989d9 100644 --- a/test/serialport-integration.js +++ b/test/serialport-integration.js @@ -31,7 +31,7 @@ describe('SerialPort', function () { var testPort = process.env.TEST_PORT; if (!testPort) { - it('Cannot be tested as we have no test ports'); + it('Cannot be tested further. Set the TEST_PORT env var with an available serialport for more testing.'); return; }