From dcfe2ab4ee42a680c36aedae4492f7bf4e90c67c Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Sun, 24 Jul 2016 22:20:23 -0400 Subject: [PATCH] Switch up testing infrastructure (#881) - npm test now runs everything but stress tests - TEST_PORT is optional but now requires an arduino, the travis provided OS X serial port wasn't worth it - do some improvements to coverage - Drop grunt for file size and complexity - Eslint has dropped 0.10 I don't think Istanbul can detect coverage on sandboxed modules like our mocked bindings. Moving to an isolated binding will improve this situation. I also don't think coveralls is working on travis --- .eslintrc.js | 2 +- .gitignore | 2 +- .istanbul.yml | 2 ++ .npmignore | 2 +- .travis.yml | 9 +++----- Gruntfile.js | 27 ---------------------- appveyor.yml | 14 ++++++++--- bin/at-least-node-4.0.js | 5 ++++ lib/bindings.js | 9 +------- lib/{list-unix.js => list-linux.js} | 4 ++-- lib/serialport.js | 4 ++++ package.json | 15 ++++-------- test/arduinoTest/integration.js | 9 ++++---- test/{list-unix.js => list-linux.js} | 20 ++++++++-------- test/mocks/darwin-hardware.js | 6 ++--- test/mocks/{list-unix.js => list-linux.js} | 14 +++++------ test/serialport.js | 14 ++++++++++- 17 files changed, 74 insertions(+), 84 deletions(-) create mode 100644 .istanbul.yml delete mode 100644 Gruntfile.js create mode 100644 bin/at-least-node-4.0.js rename lib/{list-unix.js => list-linux.js} (97%) rename test/{list-unix.js => list-linux.js} (88%) rename test/mocks/{list-unix.js => list-linux.js} (79%) diff --git a/.eslintrc.js b/.eslintrc.js index f9d825e..ff28349 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -12,7 +12,7 @@ module.exports = { "curly": 2, "eqeqeq": 2, "max-depth": 2, - "max-statements": [2, 34], + "max-statements": [2, 36], "new-cap": 2, "no-caller": 2, "no-cond-assign": 2, diff --git a/.gitignore b/.gitignore index d313257..41829a0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ npm-debug.log gyp-mac-tool .node_pre_gyprc /.idea -.nyc_output +coverage diff --git a/.istanbul.yml b/.istanbul.yml new file mode 100644 index 0000000..9d038c1 --- /dev/null +++ b/.istanbul.yml @@ -0,0 +1,2 @@ +instrumentation: + root: lib diff --git a/.npmignore b/.npmignore index fa98077..ee411a4 100644 --- a/.npmignore +++ b/.npmignore @@ -21,4 +21,4 @@ npm-debug.log gyp-mac-tool .node_pre_gyprc /.idea -.nyc_output +coverage diff --git a/.travis.yml b/.travis.yml index 032db48..d8de512 100644 --- a/.travis.yml +++ b/.travis.yml @@ -71,16 +71,13 @@ before_install: - if [[ $TRAVIS_BRANCH == `git describe --tags --always HEAD` ]]; then PUBLISH_BINARY=true; fi; - echo "Publishing native platform Binary Package? ->" $PUBLISH_BINARY -# Set a test serialport for integration testing when we run our tests if we have one -- if [[ $TRAVIS_OS_NAME == "osx" ]]; then export TEST_PORT='/dev/cu.serial1'; fi - install: # ensure source install works - npm install --build-from-source script: -# test our module -- npm run lint + # linting no longer works on node 0.10 +- node bin/at-least-node-4.0.js && npm run lint || true - node ./ - npm test @@ -95,4 +92,4 @@ script: - if [[ $PUBLISH_BINARY == true ]]; then npm install --fallback-to-build=false; fi; after_success: -- npm run coverage +- npm run report-coverage diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100644 index 3d5cde4..0000000 --- a/Gruntfile.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -module.exports = function(grunt) { - grunt.initConfig({ - mochaTest: { - test: { - options: { reporter: 'spec' }, - src: ['test/*.js'] - } - }, - eslint: { - src: [ - '*.js', - 'lib/**/*.js', - 'test/**/*.js', - 'bin/**/*.js', - 'examples/**/*.js' - ] - } - }); - - grunt.loadNpmTasks('grunt-mocha-test'); - grunt.loadNpmTasks('gruntify-eslint'); - grunt.registerTask('lint', ['eslint']); - grunt.registerTask('test', ['mochaTest']); - grunt.registerTask('default', ['lint', 'test']); -}; diff --git a/appveyor.yml b/appveyor.yml index 04ecc0f..353c89d 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -69,7 +69,13 @@ build_script: - npm install --build-from-source --msvs_version=2013 test_script: - - npm run lint + - ps: > + if (node bin/at-least-node-4.0.js) { + npm run lint; + } else { + "Not linting because the linter doesn't run on this version of node" | Write-Host + } + true; - node ./ - npm test - IF %PUBLISH_BINARY% == 1 (node-pre-gyp package 2>&1) @@ -78,7 +84,9 @@ test_script: - IF %PUBLISH_BINARY% == 1 (npm install --fallback-to-build=false) on_success: - - npm run coverage - + - npm run report-coverage deploy: OFF + +matrix: + fast_finish: true diff --git a/bin/at-least-node-4.0.js b/bin/at-least-node-4.0.js new file mode 100644 index 0000000..12ceb73 --- /dev/null +++ b/bin/at-least-node-4.0.js @@ -0,0 +1,5 @@ +'use strict'; + +if (process.versions.node.split('.')[0] < 4) { + process.exit(1); +} diff --git a/lib/bindings.js b/lib/bindings.js index 9a1adcd..66da6a2 100644 --- a/lib/bindings.js +++ b/lib/bindings.js @@ -1,17 +1,10 @@ 'use strict'; var bindings = require('bindings')('serialport.node'); -var listUnix = require('./list-unix'); +var listLinux = require('./list-linux'); var linux = process.platform !== 'win32' && process.platform !== 'darwin'; -function listLinux(callback) { - callback = callback || function(err) { - if (err) { this.emit('error', err) } - }.bind(this); - return listUnix(callback); -}; - var platformOptions = {}; if (process.platform !== 'win32') { platformOptions = { diff --git a/lib/list-unix.js b/lib/list-linux.js similarity index 97% rename from lib/list-unix.js rename to lib/list-linux.js index e42df22..f9af4ac 100644 --- a/lib/list-unix.js +++ b/lib/list-linux.js @@ -92,7 +92,7 @@ function lookupPort(file) { return execAsync(udevadm).then(udevParser); } -function listUnix(callback) { +function listLinux(callback) { var dirName = '/dev'; readdirAsync(dirName) .catch(function(err) { @@ -109,4 +109,4 @@ function listUnix(callback) { .then(function(data) { callback(null, data) }, function(err) { callback(err) }); } -module.exports = listUnix; +module.exports = listLinux; diff --git a/lib/serialport.js b/lib/serialport.js index 90ba557..5a97f2b 100644 --- a/lib/serialport.js +++ b/lib/serialport.js @@ -75,6 +75,10 @@ function correctOptions(options) { } function SerialPort(path, options, callback) { + if (!(this instanceof SerialPort)) { + return new SerialPort(path, options, callback); + } + if (typeof callback === 'boolean') { throw new TypeError('`openImmediately` is now called `autoOpen` and is a property of options'); } diff --git a/package.json b/package.json index 8326bdb..4ac326c 100644 --- a/package.json +++ b/package.json @@ -64,15 +64,13 @@ "chai": "^3.5.0", "chai-subset": "^1.2.2", "coveralls": "^2.11.9", + "eslint": "^3.1.1", "eslint-config-standard": "^5.1.0", "eslint-plugin-promise": "^1.1.0", "eslint-plugin-standard": "^1.3.2", - "grunt": "^1.0.0", - "grunt-mocha-test": "^0.12.7", - "gruntify-eslint": "^2.0.0", + "istanbul": "^0.4.4", "mocha": "^2.4.5", "node-pre-gyp-github": "^1.1.2", - "nyc": "^6.4.4", "sandboxed-module": "^2.0.3", "sinon": "^1.17.3" }, @@ -90,13 +88,10 @@ "scripts": { "install": "node-pre-gyp install --fallback-to-build", "rebuild-all": "npm rebuild && node-gyp rebuild", - "gyp-rebuild": "node-gyp rebuild", "stress": "mocha --no-timeouts test/arduinoTest/stress.js", - "integration": "mocha test/arduinoTest/integration.js test/integration-lite.js", - "grunt": "grunt", - "lint": "grunt --verbose lint", - "test": "nyc grunt --verbose test", - "coverage": "nyc report --reporter=text-lcov | coveralls", + "lint": "eslint *.js lib/**/*.js test/**/*.js bin/**/*.js examples/**/*.js", + "test": "istanbul cover ./node_modules/mocha/bin/_mocha test/*.js test/arduinoTest/integration.js test/integration-lite.js", + "report-coverage": "cat ./coverage/lcov.info | coveralls", "valgrind": "valgrind --leak-check=full --show-possibly-lost=no node --expose-gc --trace-gc node_modules/.bin/grunt test" }, "gypfile": true diff --git a/test/arduinoTest/integration.js b/test/arduinoTest/integration.js index 65d088d..7b87f46 100644 --- a/test/arduinoTest/integration.js +++ b/test/arduinoTest/integration.js @@ -17,11 +17,12 @@ switch (process.platform) { var testPort = process.env.TEST_PORT; -if (!testPort) { - throw 'These test require an arduino loaded with the arduinoEcho program on a serialport set to the TEST_PORT env var'; -} +describe('SerialPort Integration Tests', function() { + if (!testPort) { + it('Requires an Arduino loaded with the arduinoEcho program on a serialport set to the TEST_PORT env var'); + return; + } -describe('SerialPort Integration tests', function() { it('.list', function(done) { SerialPort.list(function(err, ports) { var foundPort = false; diff --git a/test/list-unix.js b/test/list-linux.js similarity index 88% rename from test/list-unix.js rename to test/list-linux.js index 5c08bae..85e8ed9 100644 --- a/test/list-unix.js +++ b/test/list-linux.js @@ -1,7 +1,7 @@ 'use strict'; var assert = require('chai').assert; -var listUnix = require('./mocks/list-unix'); +var listLinux = require('./mocks/list-linux'); var ports = { 'ttyS0': @@ -86,14 +86,14 @@ var portOutput = [ } ]; -describe('listUnix', function() { +describe('listLinux', function() { beforeEach(function() { - listUnix.reset(); + listLinux.reset(); }); it('lists available serialports', function(done) { - listUnix.setPorts(ports); - listUnix(function(err, ports) { + listLinux.setPorts(ports); + listLinux(function(err, ports) { assert.isNull(err); assert.deepEqual(ports, portOutput); done(); @@ -101,9 +101,9 @@ describe('listUnix', function() { }); it('does not list non character devices', function(done) { - listUnix.setCharacterDevice(false); - listUnix.setPorts(ports); - listUnix(function(err, ports) { + listLinux.setCharacterDevice(false); + listLinux.setPorts(ports); + listLinux(function(err, ports) { assert.isNull(err); assert.deepEqual(ports, []); done(); @@ -111,8 +111,8 @@ describe('listUnix', function() { }); it('returns an error to callback', function(done) { - listUnix.error(true); - listUnix(function(err) { + listLinux.error(true); + listLinux(function(err) { assert.instanceOf(err, Error); done(); }); diff --git a/test/mocks/darwin-hardware.js b/test/mocks/darwin-hardware.js index 256b2d0..1fe841c 100644 --- a/test/mocks/darwin-hardware.js +++ b/test/mocks/darwin-hardware.js @@ -192,7 +192,7 @@ var hardware = new Hardware(); var SandboxedModule = require('sandboxed-module'); -var serialPort = SandboxedModule.require('../../', { +var SerialPort = SandboxedModule.require('../../', { requires: { fs: { read: hardware.fakeRead.bind(hardware) @@ -208,6 +208,6 @@ var serialPort = SandboxedModule.require('../../', { } }); -serialPort.hardware = hardware; +SerialPort.hardware = hardware; -module.exports = serialPort; +module.exports = SerialPort; diff --git a/test/mocks/list-unix.js b/test/mocks/list-linux.js similarity index 79% rename from test/mocks/list-unix.js rename to test/mocks/list-linux.js index d68a668..f5bb41e 100644 --- a/test/mocks/list-unix.js +++ b/test/mocks/list-linux.js @@ -1,4 +1,4 @@ -// Mocks fs.read for listUnix +// Mocks fs.read for listLinux 'use strict'; @@ -8,7 +8,7 @@ var mockPorts = {}; var characterDevice = true; var error = false; -var listUnix = SandboxedModule.require('../../lib/list-unix', { +var listLinux = SandboxedModule.require('../../lib/list-linux', { requires: { fs: { readdir: function(path, cb) { @@ -44,22 +44,22 @@ var listUnix = SandboxedModule.require('../../lib/list-unix', { } }); -listUnix.setCharacterDevice = function(isCharacterDevice) { +listLinux.setCharacterDevice = function(isCharacterDevice) { characterDevice = isCharacterDevice; }; -listUnix.setPorts = function(ports) { +listLinux.setPorts = function(ports) { mockPorts = ports; }; -listUnix.error = function(err) { +listLinux.error = function(err) { error = err; }; -listUnix.reset = function() { +listLinux.reset = function() { error = false; mockPorts = {}; characterDevice = true; }; -module.exports = listUnix; +module.exports = listLinux; diff --git a/test/serialport.js b/test/serialport.js index c10ab30..9311042 100644 --- a/test/serialport.js +++ b/test/serialport.js @@ -37,9 +37,21 @@ describe('SerialPort', function() { done(); } }); + + it('takes lowercase options', function(done) { + var port = new SerialPort.SerialPort('/dev/exists', { baudrate: 14400, autoOpen: false }); + assert.equal(port.options.baudRate, 14400); + done(); + }); + }); describe('Constructor', function() { + it('provides auto construction', function(done) { + var serialPort = SerialPort; + this.port = serialPort('/dev/exists', done); + }); + describe('autoOpen', function() { it('opens the port automatically', function(done) { this.port = new SerialPort('/dev/exists', function(err) { @@ -161,7 +173,7 @@ describe('SerialPort', function() { }); }); - describe('Functions', function() { + describe('Methods', function() { describe('#open', function() { it('passes the port to the bindings', function(done) { var openSpy = sandbox.spy(bindings, 'open');