From aeb01a4781a204332e5bdcbe9b7c37612ead49d8 Mon Sep 17 00:00:00 2001 From: Erik Kallen Date: Tue, 29 Jan 2019 14:34:26 +0100 Subject: [PATCH 1/5] Update vid/pid check to check for function Fixes #33 by checking if vid or pid are functions --- index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 6e9dcaf..b07b5f1 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,10 @@ var index = require('./package.json'); +function isFunction(functionToCheck) { + return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]'; +} + if(global[index.name] && global[index.name].version === index.version) { module.exports = global[index.name]; } else { @@ -18,16 +22,14 @@ if(global[index.name] && global[index.name].version === index.version) { //detector.find = detection.find; detector.find = function(vid, pid, callback) { // Suss out the optional parameters - if(!pid && !callback) { + if (isFunction(vid) && !pid && !callback) { callback = vid; vid = undefined; - } - else if(!callback) { + } else if(isFunction(pid) && !callback) { callback = pid; pid = undefined; } - return new Promise(function(resolve, reject) { // Assemble the optional args into something we can use with `apply` var args = []; From 9f6ae5d5aaf2bb20f81eca8b0943bee4fce9ab8e Mon Sep 17 00:00:00 2001 From: Erik Kallen Date: Tue, 5 Mar 2019 09:12:23 +0100 Subject: [PATCH 2/5] applied changes as requested --- index.js | 10 ++++++---- test/test.js | 25 +++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 6e9dcaf..acca685 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,10 @@ var index = require('./package.json'); +function isFunction(functionToCheck) { + return typeof functionToCheck === 'function'; +} + if(global[index.name] && global[index.name].version === index.version) { module.exports = global[index.name]; } else { @@ -18,16 +22,14 @@ if(global[index.name] && global[index.name].version === index.version) { //detector.find = detection.find; detector.find = function(vid, pid, callback) { // Suss out the optional parameters - if(!pid && !callback) { + if(isFunction(vid) && !pid && !callback) { callback = vid; vid = undefined; - } - else if(!callback) { + } else if(isFunction(pid) && !callback) { callback = pid; pid = undefined; } - return new Promise(function(resolve, reject) { // Assemble the optional args into something we can use with `apply` var args = []; diff --git a/test/test.js b/test/test.js index 8a1ad6d..ce92539 100644 --- a/test/test.js +++ b/test/test.js @@ -11,12 +11,14 @@ var getSetTimeoutPromise = require('./lib/set-timeout-promise-helper'); var usbDetect = require('../'); const MANUAL_INTERACTION_TIMEOUT = 10000; +const USB_TEST_VID = process.env.TEST_VID || 5824; // VID of the tevice used to test +const USB_TEST_PID = process.env.TEST_PID || 1155; // We just look at the keys of this device object var DEVICE_OBJECT_FIXTURE = { locationId: 0, - vendorId: 5824, - productId: 1155, + vendorId: USB_TEST_VID, + productId: USB_TEST_PID, deviceName: 'Teensy USB Serial (COM3)', manufacturer: 'PJRC.COM, LLC.', serialNumber: '', @@ -63,6 +65,25 @@ describe('usb-detection', function() { }); }); + it('should return a promise when vid and pid are given', function(done) { + usbDetect.find(USB_TEST_VID, USB_TEST_PID) + .then(function(devices) { + testArrayOfDevicesShape(devices); + }) + .then(done) + .catch(done.fail); + }); + + it('should return a promise when vid is given', function(done) { + usbDetect.find(USB_TEST_VID) + .then(function(devices) { + testArrayOfDevicesShape(devices); + }) + .then(done) + .catch(done.fail); + }); + + it('should return a promise', function(done) { usbDetect.find() .then(function(devices) { From 1b7224007cf489cad8abfa5da533b74c67339a02 Mon Sep 17 00:00:00 2001 From: Erik Kallen Date: Wed, 6 Mar 2019 08:59:35 +0100 Subject: [PATCH 3/5] more hands off approach for testing --- test/test.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index ce92539..f0359c1 100644 --- a/test/test.js +++ b/test/test.js @@ -65,8 +65,9 @@ describe('usb-detection', function() { }); }); - it('should return a promise when vid and pid are given', function(done) { - usbDetect.find(USB_TEST_VID, USB_TEST_PID) + it('should return a promise when vid and pid are given', async function(done) { + const devices = await usbDetect.find(); + usbDetect.find(devices[0].vendorId, devices[0].productId) .then(function(devices) { testArrayOfDevicesShape(devices); }) @@ -74,8 +75,9 @@ describe('usb-detection', function() { .catch(done.fail); }); - it('should return a promise when vid is given', function(done) { - usbDetect.find(USB_TEST_VID) + it('should return a promise when vid is given', async function(done) { + const devices = await usbDetect.find(); + usbDetect.find(devices[0].vendorId) .then(function(devices) { testArrayOfDevicesShape(devices); }) From 98b3883320bb6661e8d87c7dbad66426b3cc5b21 Mon Sep 17 00:00:00 2001 From: Erik Kallen Date: Wed, 6 Mar 2019 10:08:34 +0100 Subject: [PATCH 4/5] check if the returned devices are actually filtered --- test/test.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index f0359c1..de46e44 100644 --- a/test/test.js +++ b/test/test.js @@ -68,8 +68,11 @@ describe('usb-detection', function() { it('should return a promise when vid and pid are given', async function(done) { const devices = await usbDetect.find(); usbDetect.find(devices[0].vendorId, devices[0].productId) - .then(function(devices) { - testArrayOfDevicesShape(devices); + .then(function(devicesFromTestedFunction) { + testArrayOfDevicesShape(devicesFromTestedFunction); + expect(devicesFromTestedFunction.length).to.be.greaterThan(0); + // Should find a subset of USB devices. We assume you have many USB devices connected + expect(devicesFromTestedFunction.length).to.be.lessThan(devices.length); }) .then(done) .catch(done.fail); @@ -78,8 +81,11 @@ describe('usb-detection', function() { it('should return a promise when vid is given', async function(done) { const devices = await usbDetect.find(); usbDetect.find(devices[0].vendorId) - .then(function(devices) { - testArrayOfDevicesShape(devices); + .then(function(devicesFromTestedFunction) { + testArrayOfDevicesShape(devicesFromTestedFunction); + expect(devicesFromTestedFunction.length).to.be.greaterThan(0); + // Should find a subset of USB devices. We assume you have many USB devices connected + expect(devicesFromTestedFunction.length).to.be.lessThan(devices.length); }) .then(done) .catch(done.fail); From b1e1b6ffa777dad77259690f145b96fc94aa80eb Mon Sep 17 00:00:00 2001 From: Erik Kallen Date: Thu, 7 Mar 2019 13:08:20 +0100 Subject: [PATCH 5/5] remove passing of vid/pid --- test/test.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index de46e44..faf45f3 100644 --- a/test/test.js +++ b/test/test.js @@ -11,14 +11,12 @@ var getSetTimeoutPromise = require('./lib/set-timeout-promise-helper'); var usbDetect = require('../'); const MANUAL_INTERACTION_TIMEOUT = 10000; -const USB_TEST_VID = process.env.TEST_VID || 5824; // VID of the tevice used to test -const USB_TEST_PID = process.env.TEST_PID || 1155; // We just look at the keys of this device object var DEVICE_OBJECT_FIXTURE = { locationId: 0, - vendorId: USB_TEST_VID, - productId: USB_TEST_PID, + vendorId: 5824, + productId: 1155, deviceName: 'Teensy USB Serial (COM3)', manufacturer: 'PJRC.COM, LLC.', serialNumber: '',