From b822721855d95b6f71dc5f63e9efb366ebdad83f Mon Sep 17 00:00:00 2001 From: Francis Gulotta Date: Sun, 16 Jul 2017 15:56:35 -0400 Subject: [PATCH] refactor some test logic --- .eslintrc | 4 ++++ test-config.json | 3 ++- test/arduinoTest/stress.js | 1 - test/bindings-linux-list.js | 1 - test/bindings.js | 16 +++------------- test/initializers/assert.js | 1 + test/initializers/index.js | 4 ++++ test/initializers/test-config.js | 11 +++++++++++ test/integration.js | 30 +++++++++++++----------------- test/mocha.opts | 1 + test/parser-byte-length.js | 1 - test/parser-delimiter.js | 1 - test/parser-readline.js | 1 - test/parser-ready.js | 1 - test/parser-regex.js | 1 - 15 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 test/initializers/assert.js create mode 100644 test/initializers/index.js create mode 100644 test/initializers/test-config.js diff --git a/.eslintrc b/.eslintrc index 7e713d0..3332763 100644 --- a/.eslintrc +++ b/.eslintrc @@ -14,6 +14,10 @@ "parserOptions": { "ecmaVersion": 2017 }, + "globals": { + "assert": false, + "makeTestFeature": false + }, "rules": { "no-unused-vars": [2, { "vars": "all", "args": "after-used" }], "no-var": 2, diff --git a/test-config.json b/test-config.json index b82d385..5a89872 100644 --- a/test-config.json +++ b/test-config.json @@ -2,6 +2,7 @@ "all": {}, "win32": { "open.unlock": false, - "baudrate.25000": false + "baudrate.25000": false, + "port.update-baudrate": false } } diff --git a/test/arduinoTest/stress.js b/test/arduinoTest/stress.js index 4491075..bf448fa 100644 --- a/test/arduinoTest/stress.js +++ b/test/arduinoTest/stress.js @@ -4,7 +4,6 @@ // `npm run stress` to run these tests const Buffer = require('safe-buffer').Buffer; -const assert = require('chai').assert; const util = require('util'); const SerialPort = require('../../'); require('colors'); // this modifies String.prototype diff --git a/test/bindings-linux-list.js b/test/bindings-linux-list.js index 837dbc3..eaf9803 100644 --- a/test/bindings-linux-list.js +++ b/test/bindings-linux-list.js @@ -1,6 +1,5 @@ 'use strict'; -const assert = require('chai').assert; const listLinux = require('./mocks/linux-list'); const ports = { diff --git a/test/bindings.js b/test/bindings.js index 2d1a282..289b80d 100644 --- a/test/bindings.js +++ b/test/bindings.js @@ -1,9 +1,6 @@ 'use strict'; /* eslint-disable no-new */ - -const assert = require('chai').assert; const Buffer = require('safe-buffer').Buffer; -const testConfig = require('../test-config.json'); let platform; switch (process.platform) { @@ -56,7 +53,6 @@ const readyData = Buffer.from('READY'); bindingsToTest.forEach((bindingName) => { const binding = require(`../lib/bindings/${bindingName}`); let testPort = process.env.TEST_PORT; - const localTestConfig = testConfig[bindingName] || {}; if (bindingName === 'mock') { testPort = '/dev/exists'; @@ -64,17 +60,11 @@ bindingsToTest.forEach((bindingName) => { } // eslint-disable-next-line no-use-before-define - testBinding(bindingName, binding, testPort, localTestConfig); + testBinding(bindingName, binding, testPort); }); -function testBinding(bindingName, Binding, testPort, localTestConfig) { - function testFeature(feature, description, callback) { - if (localTestConfig[feature] === false) { - return it(`Feature "${feature}" is disabled. "${description}"`); - } - it(description, callback); - } - +function testBinding(bindingName, Binding, testPort) { + const testFeature = makeTestFeature(bindingName); describe(`bindings/${bindingName}`, () => { describe('static method', () => { describe('.list', () => { diff --git a/test/initializers/assert.js b/test/initializers/assert.js new file mode 100644 index 0000000..a722977 --- /dev/null +++ b/test/initializers/assert.js @@ -0,0 +1 @@ +global.assert = require('chai').assert; diff --git a/test/initializers/index.js b/test/initializers/index.js new file mode 100644 index 0000000..ac22c88 --- /dev/null +++ b/test/initializers/index.js @@ -0,0 +1,4 @@ +'use strict'; +const fs = require('fs'); +const files = fs.readdirSync(__dirname).filter(file => file !== 'index.js' && file.match(/\.js$/)); +files.forEach(file => require(`./${file}`)); diff --git a/test/initializers/test-config.js b/test/initializers/test-config.js new file mode 100644 index 0000000..a9b1920 --- /dev/null +++ b/test/initializers/test-config.js @@ -0,0 +1,11 @@ +const testConfig = require('../../test-config.json'); + +global.makeTestFeature = function makeTestFeature(envName) { + const config = Object.assign({}, testConfig.all, testConfig[envName]); + return function testFeature(feature, description, callback) { + if (config[feature] === false) { + return it(`Feature "${feature}" is disabled in "${envName}. "${description}"`); + } + it(description, callback); + }; +}; diff --git a/test/integration.js b/test/integration.js index d76b597..f3d6dc9 100644 --- a/test/integration.js +++ b/test/integration.js @@ -1,7 +1,6 @@ 'use strict'; const Buffer = require('safe-buffer').Buffer; const crypto = require('crypto'); -const assert = require('chai').assert; const SerialPort = require('../'); let platform; @@ -33,6 +32,8 @@ integrationTest(platform, process.env.TEST_PORT, defaultBinding); // Be careful to close the ports when you're done with them // Ports are by default exclusively locked so a failure fails all tests function integrationTest(platform, testPort, binding) { + const testFeature = makeTestFeature(platform); + describe(`${platform} SerialPort Integration Tests`, () => { if (!testPort) { it(`${platform} tests requires an Arduino loaded with the arduinoEcho program on a serialport set to the TEST_PORT env var`); @@ -161,11 +162,7 @@ function integrationTest(platform, testPort, binding) { }); describe('#update', () => { - if (platform === 'win32') { - return it("Isn't supported on windows yet"); - } - - it('allows changing the baud rate of an open port', (done) => { + testFeature('port.update-baudrate', 'allows changing the baud rate of an open port', (done) => { const port = new SerialPort(testPort, () => { port.update({ baudRate: 57600 }, (err) => { assert.isNull(err); @@ -179,22 +176,21 @@ function integrationTest(platform, testPort, binding) { it('2k test', function(done) { this.timeout(20000); // 2k of random data - const output = crypto.randomBytes(1024 * 2); - const expectedInput = Buffer.concat([readyData, output]); + const input = crypto.randomBytes(1024 * 2); const port = new SerialPort(testPort); - + const ready = port.pipe(new SerialPort.parsers.Ready({ delimiter: readyData })); // this will trigger from the "READY" the arduino sends when it's... ready - port.once('data', () => { - port.write(output); + ready.on('ready', () => { + port.write(input); }); - let input = Buffer.alloc(0); - port.on('data', (data) => { - input = Buffer.concat([input, data]); - if (input.length >= expectedInput.length) { + let output = Buffer.alloc(0); + ready.on('data', (data) => { + output = Buffer.concat([output, data]); + if (output.length >= input.length) { try { - assert.equal(input.length, expectedInput.length, 'write length matches'); - assert.deepEqual(input, expectedInput, 'read data matches expected input'); + assert.equal(output.length, input.length, 'write length matches'); + assert.deepEqual(output, input, 'read data matches expected output'); port.close(done); } catch (e) { done(e); diff --git a/test/mocha.opts b/test/mocha.opts index 8640eee..c86b59c 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1 +1,2 @@ --bail +--require ./test/initializers diff --git a/test/parser-byte-length.js b/test/parser-byte-length.js index ac12006..ef2814f 100644 --- a/test/parser-byte-length.js +++ b/test/parser-byte-length.js @@ -1,7 +1,6 @@ 'use strict'; /* eslint-disable no-new */ -const assert = require('chai').assert; const Buffer = require('safe-buffer').Buffer; const sinon = require('sinon'); const ByteLengthParser = require('../lib/parsers/byte-length'); diff --git a/test/parser-delimiter.js b/test/parser-delimiter.js index aca25a3..96047c1 100644 --- a/test/parser-delimiter.js +++ b/test/parser-delimiter.js @@ -2,7 +2,6 @@ /* eslint-disable no-new */ const Buffer = require('safe-buffer').Buffer; -const assert = require('chai').assert; const sinon = require('sinon'); const DelimiterParser = require('../lib/parsers/delimiter'); diff --git a/test/parser-readline.js b/test/parser-readline.js index 8c17e91..e49b1d9 100644 --- a/test/parser-readline.js +++ b/test/parser-readline.js @@ -2,7 +2,6 @@ /* eslint-disable no-new */ const Buffer = require('safe-buffer').Buffer; -const assert = require('chai').assert; const sinon = require('sinon'); const ReadlineParser = require('../lib/parsers/readline'); diff --git a/test/parser-ready.js b/test/parser-ready.js index 512ce74..7388ad0 100644 --- a/test/parser-ready.js +++ b/test/parser-ready.js @@ -2,7 +2,6 @@ /* eslint-disable no-new */ const Buffer = require('safe-buffer').Buffer; -const assert = require('chai').assert; const sinon = require('sinon'); const ReadyParser = require('../lib/parsers/ready'); diff --git a/test/parser-regex.js b/test/parser-regex.js index f0a077b..b8bcdec 100644 --- a/test/parser-regex.js +++ b/test/parser-regex.js @@ -1,7 +1,6 @@ 'use strict'; /* eslint-disable no-new */ -const assert = require('chai').assert; const Buffer = require('safe-buffer').Buffer; const sinon = require('sinon');