Remove lowercase options and properly define .path (#898)

This commit is contained in:
Francis Gulotta 2016-08-06 20:26:49 -04:00 • committed by GitHub
parent ffd549ee69
commit 3aafa77e2c
6 changed files with 45 additions and 40 deletions

View file

@ -71,6 +71,10 @@ before_install:
- if [[ $TRAVIS_BRANCH == `git describe --tags --always HEAD` ]]; then PUBLISH_BINARY=true; fi;
- echo "Publishing native platform Binary Package? ->" $PUBLISH_BINARY
# Cleanup the output of npm
- npm config set progress false
- npm config set spin false
install:
# ensure source install works
- npm install --build-from-source

View file

@ -294,6 +294,12 @@ Enjoy and do cool things with this code.
### SerialPort ⏏
**Kind**: Exported class
**Emits**: <code>[open](#module_serialport--SerialPort+event_open)</code>, <code>[data](#module_serialport--SerialPort+event_data)</code>, <code>[close](#module_serialport--SerialPort+event_close)</code>, <code>[error](#module_serialport--SerialPort+event_error)</code>, <code>[disconnect](#module_serialport--SerialPort+event_disconnect)</code>
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| path | <code>string</code> | The system path or name of the serial port. Read Only. |
-

View file

@ -62,6 +62,11 @@ install:
}
true;
# Cleanup the output of npm
- ps: >
npm config set progress false
npm config set spin false
# We don't currently have a port to test on windows
# - ps: $env:TEST_PORT = "COM1";

View file

@ -56,27 +56,6 @@ var defaultSetFlags = {
rts: true
};
// deprecate the lowercase version of these options next major release
var LOWERCASE_OPTIONS = [
'baudRate',
'dataBits',
'stopBits',
'bufferSize',
'platformOptions'
];
function correctOptions(options) {
LOWERCASE_OPTIONS.forEach(function(name) {
var lowerName = name.toLowerCase();
if (options.hasOwnProperty(lowerName)) {
var value = options[lowerName];
delete options[lowerName];
options[name] = value;
}
});
return options;
}
/**
* A callback called with an error or null.
* @typedef {function} errorCallback
@ -110,6 +89,7 @@ function correctOptions(options) {
* @param {module:serialport~openOptions=} options - Port configuration options
* @param {module:serialport~errorCallback=} openCallback - Called when a connection has been opened. If this is not provided and an error occurs, it will be emitted on the ports `error` event. The callback will NOT be called if autoOpen is set to false in the openOptions as the open will not be performed.
* @throws {TypeError} When given invalid arguments a TypeError will be thrown.
* @property {string} path The system path or name of the serial port. Read Only.
* @emits module:serialport#open
* @emits module:serialport#data
* @emits module:serialport#close
@ -122,6 +102,8 @@ function SerialPort(path, options, callback) {
return new SerialPort(path, options, callback);
}
stream.Stream.call(this);
if (typeof callback === 'boolean') {
throw new TypeError('`openImmediately` is now called `autoOpen` and is a property of options');
}
@ -131,19 +113,19 @@ function SerialPort(path, options, callback) {
options = {};
}
options = options || {};
var settings = assign({}, defaultSettings, options);
stream.Stream.call(this);
Object.defineProperty(this, 'path', {
enumerable: true,
writable: false,
value: path
});
if (!path) {
if (!this.path) {
throw new TypeError('No path specified');
}
this.path = path;
var correctedOptions = correctOptions(options);
var settings = assign({}, defaultSettings, correctedOptions);
if (typeof settings.baudRate !== 'number') {
throw new TypeError('Invalid "baudRate" must be a number got: ' + settings.baudRate);
}
@ -269,8 +251,7 @@ SerialPort.prototype.update = function(options, callback) {
return this._error(new Error('Port is not open'), callback);
}
var correctedOptions = correctOptions(options);
var settings = assign({}, defaultSettings, correctedOptions);
var settings = assign({}, defaultSettings, options);
this.options.baudRate = settings.baudRate;
SerialPortBinding.update(this.fd, this.options, function(err) {

View file

@ -94,6 +94,7 @@
"stress": "mocha --no-timeouts test/arduinoTest/stress.js",
"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",
"test:unit": "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"
},

View file

@ -37,13 +37,6 @@ 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() {
@ -118,7 +111,7 @@ describe('SerialPort', function() {
it('errors with invalid databits', function(done) {
try {
this.port = new SerialPort('/dev/exists', { databits: 19 });
this.port = new SerialPort('/dev/exists', { dataBits: 19 });
} catch(err){
assert.instanceOf(err, Error);
done();
@ -127,7 +120,7 @@ describe('SerialPort', function() {
it('errors with invalid stopbits', function(done) {
try {
this.port = new SerialPort('/dev/exists', { stopbits: 19 });
this.port = new SerialPort('/dev/exists', { stopBits: 19 });
} catch(err){
assert.instanceOf(err, Error);
done();
@ -173,6 +166,21 @@ describe('SerialPort', function() {
});
});
describe('Properties', function() {
describe('.path', function(){
it('is a read only property set during construction', function(){
var port = new SerialPort('/dev/exists', {autoOpen: false});
assert.equal(port.path, '/dev/exists');
try {
port.path = 'foo';
} catch(e) {
assert.instanceOf(e, TypeError);
}
assert.equal(port.path, '/dev/exists');
});
});
});
describe('Methods', function() {
describe('#open', function() {
it('passes the port to the bindings', function(done) {