diff --git a/.travis.yml b/.travis.yml index a3a219c..676590b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ --- -sudo: false language: cpp addons: apt: @@ -20,6 +19,7 @@ addons: - libnss3:i386 - libasound2:i386 - libxss1:i386 + - socat # Build matrix os: @@ -82,6 +82,13 @@ before_install: - npm config set progress false - npm config set spin false +# setup virtual serialports +# - > + # socat -d -d -unlink-close pty,raw,nonblock,echo=0,link=ttyV0 pty,raw,nonblock,echo=0,link=ttyV1 & + # ./bin/echo.js --port ttyV0 & + # export TEST_PORT=ttyV1 + # export DISABLE_PORT_FEATURE=baudrate.25000,set.set,get.get + - > if [[ ! -z $TRAVIS_ELECTRON_VERSION ]]; then if [[ $TRAVIS_OS_NAME == "linux" ]]; then diff --git a/appveyor.yml b/appveyor.yml index cfafd68..da7133a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -92,6 +92,10 @@ install: } true; +# Disable windows incompatible port tests +- ps: > + $env:DISABLE_PORT_FEATURE = "open.unlock,baudrate.25000" + build_script: - npm install --build-from-source --msvs_version=2013 diff --git a/bin/echo.js b/bin/echo.js new file mode 100755 index 0000000..fcd8f44 --- /dev/null +++ b/bin/echo.js @@ -0,0 +1,32 @@ +#!/usr/bin/env node +'use strict'; + +const SerialPort = require('../'); +const version = require('../package.json').version; +const args = require('commander'); + +const readyData = new Buffer('READY'); + +args + .version(version) + .usage('-p ') + .description('A basic terminal interface for communicating over a serial port. Pressing ctrl+c exits.') + .option('-p, --port ', 'Path or Name of serial port') + .parse(process.argv); + +if (!args.port) { + args.outputHelp(); + args.missingArgument('port'); + process.exit(-1); +} + +const port = new SerialPort(args.port); + +port.on('open', () => { + console.log(`echo: Port open: ${args.port}`); + setTimeout(() => { + console.log('echo: READY!'); + port.on('data', data => port.write(data)); + port.write(readyData); + }, 250); +}); diff --git a/lib/bindings/mock.js b/lib/bindings/mock.js index d8252f2..a3f1523 100644 --- a/lib/bindings/mock.js +++ b/lib/bindings/mock.js @@ -121,6 +121,7 @@ MockBindings.prototype.open = function(path, opt, cb) { } port.openOpt = opt; + port.openOpt = Object.assign({}, opt); process.nextTick(() => { this.isOpen = true; cb(null); diff --git a/test/bindings.js b/test/bindings.js index fe3087f..f509e66 100644 --- a/test/bindings.js +++ b/test/bindings.js @@ -14,7 +14,7 @@ switch (process.platform) { throw new Error(`Unknown platform "${process.platform}"`); } -const defaultOpenOptions = { +const defaultOpenOptions = Object.freeze({ baudRate: 9600, dataBits: 8, hupcl: true, @@ -25,21 +25,28 @@ const defaultOpenOptions = { xany: false, xoff: false, xon: false -}; +}); -const defaultSetFlags = { +const defaultSetFlags = Object.freeze({ brk: false, cts: false, dtr: true, dts: false, rts: true -}; +}); const bindingsToTest = [ 'mock', platform ]; +function parseDisabled(envStr) { + return (envStr || '').split(',').reduce((disabled, feature) => { + disabled[feature] = true; + return disabled; + }, {}); +} + function disconnect(err) { throw (err || new Error('Unknown disconnection')); } @@ -54,16 +61,26 @@ const readyData = new Buffer('READY'); bindingsToTest.forEach((bindingName) => { const binding = require(`../lib/bindings/${bindingName}`); let testPort = process.env.TEST_PORT; + let disabledFeatures = parseDisabled(process.env.DISABLE_PORT_FEATURE); + if (bindingName === 'mock') { testPort = '/dev/exists'; binding.createPort(testPort, { echo: true, readyData }); + disabledFeatures = {}; } // eslint-disable-next-line no-use-before-define - testBinding(bindingName, binding, testPort); + testBinding(bindingName, binding, testPort, disabledFeatures); }); -function testBinding(bindingName, Binding, testPort) { +function testBinding(bindingName, Binding, testPort, disabledFeatures) { + function testFeature(feature, description, callback) { + if (disabledFeatures[feature]) { + return it(`Feature "${feature}" has been disabled. "${description}"`); + } + it(description, callback); + } + describe(`bindings/${bindingName}`, () => { describe('static method', () => { describe('.list', () => { @@ -227,18 +244,14 @@ function testBinding(bindingName, Binding, testPort) { }); }); - if (platform === 'win32') { - it('doesn\'t supports a custom baudRates of 25000'); - } else { - it('supports a custom baudRate of 25000', (done) => { - const customRates = Object.assign({}, defaultOpenOptions, { baudRate: 25000 }); - binding.open(testPort, customRates, (err) => { - assert.isNull(err); - assert.equal(binding.isOpen, true); - binding.close(done); - }); + testFeature('baudrate.25000', 'supports a custom baudRate of 25000', (done) => { + const customRates = Object.assign({}, defaultOpenOptions, { baudRate: 25000 }); + binding.open(testPort, customRates, (err) => { + assert.isNull(err); + assert.equal(binding.isOpen, true); + binding.close(done); }); - } + }); describe('optional locking', () => { // Ensure that if we fail, we still close the port @@ -265,31 +278,27 @@ function testBinding(bindingName, Binding, testPort) { }); }); - if (platform === 'win32') { - it('Ports currently cannot be unlocked on windows'); - } else { - it('can unlock the port', (done) => { - const noLock = Object.assign({}, defaultOpenOptions, { lock: false }); - const binding2 = new Binding({ - disconnect - }); + testFeature('open.unlock', 'can unlock the port', (done) => { + const noLock = Object.assign({}, defaultOpenOptions, { lock: false }); + const binding2 = new Binding({ + disconnect + }); - binding.open(testPort, noLock, (err) => { + binding.open(testPort, noLock, (err) => { + assert.isNull(err); + assert.equal(binding.isOpen, true); + + binding2.open(testPort, noLock, (err) => { assert.isNull(err); - assert.equal(binding.isOpen, true); + assert.equal(binding2.isOpen, true); - binding2.open(testPort, noLock, (err) => { + binding.close((err) => { assert.isNull(err); - assert.equal(binding2.isOpen, true); - - binding.close((err) => { - assert.isNull(err); - binding2.close(done); - }); + binding2.close(done); }); }); }); - } + }); }); }); @@ -390,18 +399,12 @@ function testBinding(bindingName, Binding, testPort) { binding.update({ baudRate: 57600 }, done); }); - // if (platform === 'win32') { - // it("doesn't yet support custom rates"); - // return; - // } - // - // breaks testing for unknown reasons - // it('updates baudRate to a custom rate', function(done) { - // binding.update({baudRate: 25000}, function(err) { - // assert.isNull(err); - // binding.update({baudRate: defaultOpenOptions.baudRate}, done); - // }); - // }); + testFeature('baudrate.25000', 'updates baudRate to a custom rate', (done) => { + binding.update({ baudRate: 25000 }, (err) => { + assert.isNull(err); + binding.update({ baudRate: defaultOpenOptions.baudRate }, done); + }); + }); }); describe('#write', () => { @@ -564,7 +567,6 @@ function testBinding(bindingName, Binding, testPort) { zalgo = true; }); if (zalgo) { done(new Error('Zalgo is here')) } - // console.log(zalgo); }); it('throws when not called with options', (done) => { @@ -596,7 +598,7 @@ function testBinding(bindingName, Binding, testPort) { binding.close(done); }); - it('sets flags on the port', (done) => { + testFeature('set.set', 'sets flags on the port', (done) => { binding.set(defaultSetFlags, done); }); }); @@ -688,7 +690,7 @@ function testBinding(bindingName, Binding, testPort) { binding.close(done); }); - it('gets modem line status from the port', (done) => { + testFeature('get.get', 'gets modem line status from the port', (done) => { binding.get((err, status) => { assert.isNull(err); assert.isObject(status);