From c9495a713c3db0aba972fb727d219648e68b71c6 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Fri, 1 Nov 2013 00:10:39 -0400 Subject: [PATCH 1/5] Move the read test into serialport-basic. Make use of sinon sandboxes, refactor the mock serial-port-binding into linux hardware for "remote control" --- test/read.js | 60 ---------------- test/serialport-basic.js | 67 +++++++++-------- test_mocks/linux-hardware.js | 115 ++++++++++++++++++++++++++++++ test_mocks/serial-port-binding.js | 67 ----------------- 4 files changed, 154 insertions(+), 155 deletions(-) delete mode 100644 test/read.js create mode 100644 test_mocks/linux-hardware.js delete mode 100644 test_mocks/serial-port-binding.js diff --git a/test/read.js b/test/read.js deleted file mode 100644 index b74f2bf..0000000 --- a/test/read.js +++ /dev/null @@ -1,60 +0,0 @@ -"use strict"; - -var serialPort = require('../serialport'); -var sinon = require("sinon"); -var chai = require('chai'); -var mockBinding = require('../test_mocks/serial-port-binding'); - -var expect = chai.expect; -var SerialPort = serialPort.SerialPort; -serialPort.SerialPortBinding = mockBinding; - -describe('SerialPort', function () { - - var port; - - describe('opening a port and writing some bytes', function () { - - it("it should emit the expected answer bytes", function (done) { - port = new SerialPort('/dev/fun', {}, false); - port.open(function() { - - var data = new Buffer([1, 2, 3, 4]); - - port.once('data', function(res) { - expect(res).to.eql(data); - done(); - }); - - port.write(data, function() { - }); - }); - }); - - describe('closing and reopening the previously used port and writing some bytes', function () { - - before(function(done) { - port.close(done); - }); - - it("it should emit the expected answer bytes again", function (done) { - port.open(function() { - - var data = new Buffer([5, 6, 7, 8]); - - port.once('data', function(res) { - expect(res).to.eql(data); - done(); - }); - - port.write(data, function() { - }); - }); - }); - - }); - - }); - -}); - diff --git a/test/serialport-basic.js b/test/serialport-basic.js index 307fe7d..87fd043 100644 --- a/test/serialport-basic.js +++ b/test/serialport-basic.js @@ -1,49 +1,52 @@ "use strict"; -var serialPort = require('../serialport'); var sinon = require("sinon"); var chai = require('chai'); -var mockBinding = require('../test_mocks/serial-port-binding'); - var expect = chai.expect; -var SerialPort = serialPort.SerialPort; -serialPort.SerialPortBinding = mockBinding; + +var MockedSerialPort = require('../test_mocks/linux-hardware'); +var SerialPort = MockedSerialPort.SerialPort; +var hardware = MockedSerialPort.hardware; + +// Create a port for fun and profit +hardware.createPort('/dev/exists'); describe('SerialPort', function () { + var sandbox; + + beforeEach(function () { + sandbox = sinon.sandbox.create(); + }); + + afterEach(function () { + sandbox.restore(); + }); describe('Constructor', function () { it("opens the port immediately", function (done) { - var port = new SerialPort('/dev/fun', function (err) { + var port = new SerialPort('/dev/exists', function (err) { expect(err).to.not.be.ok; done(); }); }); - it('emits an error on the factory when erroring without a callback', function (done) { - // setup the bindings to report an error during open - var stubOpen = sinon.stub(mockBinding, 'open', function (path, opt, cb) { - cb('fakeErrr!'); - }); - // finish the test on error - serialPort.once('error', function (err) { + MockedSerialPort.once('error', function (err) { chai.assert.isDefined(err, "didn't get an error"); - stubOpen.restore(); done(); }); - var port = new SerialPort('johnJacobJingleheimerSchmidt'); + var port = new SerialPort('/dev/johnJacobJingleheimerSchmidt'); }); it('errors with invalid databits', function (done) { - var errorCallback = function (err) { chai.assert.isDefined(err, 'err is not defined'); done(); }; - var port = new SerialPort('johnJacobJingleheimerSchmidt', { databits : 19 }, false, errorCallback); + var port = new SerialPort('/dev/exists', { databits : 19 }, false, errorCallback); }); }); @@ -51,31 +54,39 @@ describe('SerialPort', function () { describe('#open', function () { it('passes the port to the bindings', function (done) { - var openSpy = sinon.spy(mockBinding, 'open'); - var port = new SerialPort('/dev/happyPort', {}, false); + var openSpy = sandbox.spy(MockedSerialPort.SerialPortBinding, 'open'); + var port = new SerialPort('/dev/exists', {}, false); port.open(function (err) { expect(err).to.not.be.ok; - expect(openSpy.calledWith('/dev/happyPort')); - openSpy.restore(); + expect(openSpy.calledWith('/dev/exists')); done(); }); - }); it('calls back an error when opening an invalid port', function (done) { - var stubOpen = sinon.stub(mockBinding, 'open', function (path, opt, cb) { - cb('fakeErrr!'); - }); - var port = new SerialPort('johnJacobJingleheimerSchmidt', {}, false); + var port = new SerialPort('/dev/unhappy', {}, false); port.open(function (err) { expect(err).to.be.ok; done(); }); - - stubOpen.restore(); }); + it("emits data after being reopened", function (done) { + hardware.createPort('/dev/fun'); + + var data = new Buffer("Howdy!"); + var port = new SerialPort('/dev/fun', function () { + port.close(); + port.open(function () { + port.once('data', function (res) { + expect(res).to.eql(data); + done(); + }); + hardware.emitData('/dev/fun', data); + }); + }); + }); }); diff --git a/test_mocks/linux-hardware.js b/test_mocks/linux-hardware.js new file mode 100644 index 0000000..3c8da27 --- /dev/null +++ b/test_mocks/linux-hardware.js @@ -0,0 +1,115 @@ +// This takes a serial-port factory and mocks the shit out of it in complete isolation +"use strict"; +var rewire = require('rewire'); +var serialPort = require('../serialport'); + +var mockSerialportPoller = function (hardware) { + var Poller = function (path, cb) { + this.port = hardware.ports[path]; + if (!this.port) { + throw new Error(path + " does not exist - please call hardware.createPort(path) first"); + } + this.port.poller = this; + this.polling = null; + this.cb = cb; + }; + Poller.prototype.start = function () { + this.polling = true; + }; + Poller.prototype.close = function () { + this.polling = false; + }; + Poller.prototype.detectRead = function () { + this.cb(); + }; + return Poller; +}; + +var Hardware = function () { + this.ports = {}; + this.mockBinding = { + list: this.list.bind(this), + open: this.open.bind(this), + write: this.write.bind(this), + close: this.close.bind(this), + flush: this.flush.bind(this), + SerialportPoller: mockSerialportPoller(this) + }; +}; + +Hardware.prototype.createPort = function (path) { + this.ports[path] = { + data: new Buffer(0), + lastWrite: null, + open: false, + poller: null, + info: { + comName: path, + manufacturer: '', + serialNumber: '', + pnpId: '', + locationId: '', + vendorId: '', + productId: '' + } + }; +}; + +Hardware.prototype.emitData = function (path, data) { + var port = this.ports[path]; + if(!port) { + throw new Error(path + " does not exist - please call hardware.createPort(path) first"); + } + port.data = Buffer.concat([port.data, data]); + port.poller && port.poller.detectRead(); +}; + +Hardware.prototype.list = function (cb) { + var ports = this.ports; + var info = Object.keys(ports).map(function (path) { + return ports[path].info; + }); + cb && cb(info); +}; + +Hardware.prototype.open = function (path, opt, cb) { + var port = this.ports[path]; + if (!port) { + return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + } + port.open = true; + port.openOpt = opt; + cb && cb(null, path); // using path as the fd for convience +}; + +Hardware.prototype.write = function (path, buffer, cb) { + var port = this.ports[path]; + if (!port) { + return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + } + port.lastWrite = new Buffer(buffer); //copy + cb && cb(null, buffer.length); +}; + +Hardware.prototype.close = function (path, cb) { + var port = this.ports[path]; + if (!port) { + return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + } + port.open = false; + cb && cb(null); +}; + +Hardware.prototype.flush = function (path, cb) { + var port = this.ports[path]; + if (!port) { + return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + } + cb && cb(null, undefined); +}; + +var hardware = new Hardware(); +serialPort.hardware = hardware; +serialPort.SerialPortBinding = hardware.mockBinding; + +module.exports = serialPort; diff --git a/test_mocks/serial-port-binding.js b/test_mocks/serial-port-binding.js deleted file mode 100644 index 6fea663..0000000 --- a/test_mocks/serial-port-binding.js +++ /dev/null @@ -1,67 +0,0 @@ -// This exports a SerialPortBinding like object that has functions -// that will emulate a happy object. These functions can be spied on -// mocked or otherwise abused for testing. - -"use strict"; - -var serialPortBinding; -module.exports = serialPortBinding = { - open: function (path, opt, cb) { - this.path = path; - this.options = opt; - this.readable = true; - cb(null, 'fakeFileDescriptor'); - }, - write: function (fd, buffer, cb) { - this.lastWrite = buffer; - cb(null, buffer.length); - }, - _read: function() { - - var echo = this.lastWrite; - - if (!this.readable || this.paused || this.reading) return; - - this.reading = true; - - this.reading = false; - - if (echo && echo.length > 0) { - this.lastWrite = null; - this.options.dataCallback(echo); - } - - // do not emit events anymore after we declared the stream unreadable - if (!this.readable) return; - - var self = this; - setTimeout(function() { - self._read(); - }, 20); - }, - close: function (fd, cb) { - cb(null); - }, - list: function (cb) { - var fakeSerialPort = { - comName: '/dev/really-cool-serialport', - manufacturer: '', - serialNumber: '', - pnpId: '', - locationId: '', - vendorId: '', - productId: '' - }; - cb([fakeSerialPort]); - }, - flush: function (fd, cb) { - cb(null, undefined); - }, - SerialportPoller: function (fd, cb) { - serialPortBinding.currentSerialPoller = this; - this.start = function () { serialPortBinding._read(); }; - this.close = function () { }; - this.cb = cb; - }, - path: '' -}; From fa1621579c376f4cd400b09f002fdbe687a03d66 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Tue, 12 Nov 2013 15:37:16 -0500 Subject: [PATCH 2/5] Use rewire to mock fs.read and write a nice little mock fs.read --- package.json | 5 +++-- test_mocks/linux-hardware.js | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 81ab04d..5c60653 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "grunt": "*", "grunt-cli": "*", "grunt-mocha-test": "*", - "grunt-contrib-jshint": "*" + "grunt-contrib-jshint": "*", + "rewire": "~2.0.0" }, "engines": { "node": "0.10.x" @@ -56,4 +57,4 @@ "scripts": { "test": "grunt --verbose" } -} \ No newline at end of file +} diff --git a/test_mocks/linux-hardware.js b/test_mocks/linux-hardware.js index 3c8da27..ba49ae1 100644 --- a/test_mocks/linux-hardware.js +++ b/test_mocks/linux-hardware.js @@ -1,7 +1,8 @@ -// This takes a serial-port factory and mocks the shit out of it in complete isolation +// This takes a serial-port factory and mocks the shit out of it in complete isolation per require of this file + "use strict"; var rewire = require('rewire'); -var serialPort = require('../serialport'); +var serialPort = rewire('../serialport'); var mockSerialportPoller = function (hardware) { var Poller = function (path, cb) { @@ -112,4 +113,24 @@ var hardware = new Hardware(); serialPort.hardware = hardware; serialPort.SerialPortBinding = hardware.mockBinding; +serialPort.__set__('fs', { + read: function (path, buffer, offset, length, position, cb) { + var port = hardware.ports[path]; + if (!port) { + return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + } + if (port.data.length === 0) { + return cb(null, 0, buffer); + } + if ((offset + length) > buffer.length) { + throw new Error("Length extends beyond buffer"); + } + var read = port.data.slice(0, length); + port.data = port.data.slice(length); + read.copy(buffer, offset); + cb(null, read.length, buffer); + } +}); + + module.exports = serialPort; From 1b831f079fd5faa108451e85ffe911f5ef869fa4 Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Tue, 12 Nov 2013 15:37:32 -0500 Subject: [PATCH 3/5] Add some basic read tests --- test/serialport-basic.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/serialport-basic.js b/test/serialport-basic.js index 87fd043..c764537 100644 --- a/test/serialport-basic.js +++ b/test/serialport-basic.js @@ -51,6 +51,36 @@ describe('SerialPort', function () { }); + describe('reading data', function () { + + it('emits data events by default', function (done) { + hardware.createPort('/dev/exists'); // clears out any previously written data + var testData = new Buffer("I am a really short string"); + var port = new SerialPort('/dev/exists', function () { + port.once('data', function(recvData) { + expect(recvData).to.eql(testData); + done(); + }); + hardware.emitData('/dev/exists', testData); + }); + }); + + it('calls the dataCallback if set', function (done) { + hardware.createPort('/dev/exists'); // clears out any previously written data + var testData = new Buffer("I am a really short string"); + var opt = { + dataCallback: function (recvData) { + expect(recvData).to.eql(testData); + done(); + } + }; + var port = new SerialPort('/dev/exists', opt, function () { + hardware.emitData('/dev/exists', testData); + }); + }); + + }); + describe('#open', function () { it('passes the port to the bindings', function (done) { From d792fee6f2469d3102478aa319e2f4df825ce02c Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Tue, 12 Nov 2013 15:38:13 -0500 Subject: [PATCH 4/5] Remove a needless require and an reference to an undefined property used in reading off the serial port --- serialport.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/serialport.js b/serialport.js index b1ef1ec..66a1e38 100644 --- a/serialport.js +++ b/serialport.js @@ -10,8 +10,6 @@ var fs = require('fs'); var stream = require('stream'); var path = require('path'); var async = require('async'); -var child_process = require('child_process'); - function SerialPortFactory() { @@ -203,6 +201,7 @@ function SerialPortFactory() { util.inherits(SerialPort, stream.Stream); + SerialPort.prototype.open = function (callback) { var self = this; this.paused = true; @@ -336,7 +335,7 @@ function SerialPortFactory() { // console.log(self.path + ' >> POOL OFFSET: ', self.pool.used); // console.log(">>REQUEST READ: ", toRead); - fs.read(self.fd, self.pool, self.pool.used, toRead, self.pos, function(err, bytesRead){ + fs.read(self.fd, self.pool, self.pool.used, toRead, 0, function(err, bytesRead){ var readPool = self.pool; var bytesRequested = toRead; afterRead(err, bytesRead, readPool, bytesRequested);} From 244e4a717d2dc8167fd81d753b690eb38f70453d Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Tue, 12 Nov 2013 17:28:41 -0500 Subject: [PATCH 5/5] Lets pretend we're osx --- test_mocks/linux-hardware.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test_mocks/linux-hardware.js b/test_mocks/linux-hardware.js index ba49ae1..4f26e46 100644 --- a/test_mocks/linux-hardware.js +++ b/test_mocks/linux-hardware.js @@ -2,6 +2,7 @@ "use strict"; var rewire = require('rewire'); +process.platform = 'darwin'; var serialPort = rewire('../serialport'); var mockSerialportPoller = function (hardware) {