Skip to content

Commit

Permalink
Fix call to undefined logging function in usb_connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Jul 17, 2015
1 parent 512320c commit 57a04bd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/usb_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ USBConnection.prototype._receiveMessages = function() {
return;
} else {
// Otherwise print the error
logs.error('Error reading USB message endpoint:', e);
logs.err('Error reading USB message endpoint:', e);
// Return a non-zero return code
process.exit(-5);
}
Expand Down Expand Up @@ -238,3 +238,8 @@ Scanner.prototype.stop = function() {

module.exports.startScan = startScan;
module.exports.stopScan = stopScan;

if (global.IS_TEST_ENV) {
module.exports.USBConnection = USBConnection;
module.exports.Scanner = Scanner;
}
93 changes: 93 additions & 0 deletions test/unit/usb_connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
var sinon = require('sinon');
var Emitter = require('events').EventEmitter;
var Duplex = require('stream').Duplex;
var USBConnection = require('../../lib/usb_connection').USBConnection;
var logs = require('../../lib/logs');

exports['USBConnection'] = {
setUp: function(done) {
this.usbConnection = new USBConnection({});
done();
},

tearDown: function(done) {
done();
},

duplexSubclass: function(test) {
test.expect(1);
test.ok(this.usbConnection instanceof Duplex);
test.done();
},

connectionType: function(test) {
test.expect(1);
test.equal(this.usbConnection.connectionType, 'USB');
test.done();
},
};

exports['USBConnection.prototype._receiveMessages'] = {
setUp: function(done) {
this.sinon = sinon.sandbox.create();
this.err = this.sinon.stub(logs, 'err');
this.processExit = this.sinon.stub(process, 'exit');
this.usbConnection = new USBConnection({});
this.usbConnection.epIn = new Emitter();
this.usbConnection.epIn.startPoll = this.sinon.spy();
done();
},

tearDown: function(done) {
this.sinon.restore();
done();
},

startPolling: function(test) {
test.expect(3);

this.usbConnection._receiveMessages();

test.equal(this.usbConnection.epIn.startPoll.callCount, 1);
test.equal(this.usbConnection.epIn.startPoll.lastCall.args[0], 2);
test.equal(this.usbConnection.epIn.startPoll.lastCall.args[1], 4096);
test.done();
},

pushReceivedData: function(test) {
test.expect(1);

this.usbConnection._receiveMessages();

this.usbConnection.epIn.emit('data', new Buffer([0xff, 0xff]));
test.equal(this.usbConnection._readableState.length, 2);
test.done();
},

errorWhenStillOpen: function(test) {
test.expect(2);

this.usbConnection._receiveMessages();

this.usbConnection.epIn.emit('error', 'oh no!');

test.equal(this.err.callCount, 1);
test.equal(this.processExit.callCount, 1);
test.done();
},

errorWhenClosed: function(test) {
test.expect(2);

this.usbConnection._receiveMessages();
this.usbConnection.closed = true;
this.usbConnection.epIn.emit('error', 'oh no!');

// The immediate return prevents these from being
// called from in the error handler
test.equal(this.err.callCount, 0);
test.equal(this.processExit.callCount, 0);
test.done();
},

};

0 comments on commit 57a04bd

Please sign in to comment.