From 925caf9a78b79a34d7cfea47d4f435bc80bb6f9a Mon Sep 17 00:00:00 2001 From: hortinstein Date: Sat, 17 Oct 2015 14:34:04 +0000 Subject: [PATCH 1/2] fixed linting issues --- index.js | 11 ++++++----- package.json | 9 ++++----- test/test-live.js | 6 +++--- test/test.js | 20 ++++++++++---------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index c7eec7d..4fc5085 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +"use strict"; + // converts a string: "8f:3f:20:33:54:44" // to a numeric array: [ 143, 63, 32, 51, 84, 68 ] // for comparison @@ -9,8 +11,7 @@ var hex_to_int_array = function(hex){ } //console.log(hex,int_array) return int_array; - -} +}; // converts a numeric array: [ 143, 63, 32, 51, 84, 68 ] // to a string: "8f:3f:20:33:54:44"= @@ -24,7 +25,7 @@ var int_array_to_hex = function (int_array) { hex += h; } return hex.slice(1);//slice is to get rid of the leading : -} +}; var pcap = require('pcap'); @@ -43,7 +44,7 @@ var create_session = function () { process.exit(1); } return session; -} +}; //Function to register the node button var register = function(mac_addresses) { @@ -51,7 +52,7 @@ var register = function(mac_addresses) { //console.log("array detected") } else { //console.log("single element detected") - mac_addresses = [mac_addresses]//cast to array + mac_addresses = [mac_addresses];//cast to array } var pcap_session = create_session(); var readStream = new stream.Readable({ diff --git a/package.json b/package.json index 179627b..dbf2c38 100644 --- a/package.json +++ b/package.json @@ -35,11 +35,10 @@ "arpjs": "^1.0.0", "mocha": "^2.3.0", "should": "^7.1.0", - "sinon": "*", - "mockery": "*", - "ip":"*", - "istanbul": "*", - "coveralls": "*" + "sinon": "^1.17.1", + "mockery": "^1.4.0", + "istanbul": "0.3.22", + "coveralls": "^2.11.4" } } diff --git a/test/test-live.js b/test/test-live.js index d7c3024..115fd84 100644 --- a/test/test-live.js +++ b/test/test-live.js @@ -3,7 +3,7 @@ process.env.NODE_ENV = 'test'; var should = require('should'); var assert = require('assert'); var dash_button = require('../index.js'); -var pcap = require('pcap') +var pcap = require('pcap'); var hex = '8f:3f:20:33:54:44'; var hex2 = '8f:3f:20:33:54:43'; var hex3 = '8f:3f:20:33:54:42'; @@ -43,7 +43,7 @@ startTests = function() { done(); }); it('should correctly convert string hex to decimal array', function(done) { - int_array = dash_button.hex_to_int_array(hex) + int_array = dash_button.hex_to_int_array(hex); done(); }); it('should correctly convert a decimal array to string hex', function(done) { @@ -80,6 +80,6 @@ startTests = function() { if (mac_address === hex3) done(); }); }); -} +}; startTests(); diff --git a/test/test.js b/test/test.js index 1d72c88..e4b3ef7 100644 --- a/test/test.js +++ b/test/test.js @@ -1,5 +1,5 @@ process.env.NODE_ENV = 'test'; -require('buffer') +require('buffer'); ///http://bulkan-evcimen.com/using_mockery_to_mock_modules_nodejs.html //this should be an effective way to mock functions var should = require('should'); @@ -43,7 +43,7 @@ var mock_pcap = { } } } - } + }; return mock_packet; } } @@ -64,7 +64,7 @@ startTests = function() { done(); }); it('should correctly convert string hex to decimal array', function(done) { - int_array = dash_button.hex_to_int_array(hex) + int_array = dash_button.hex_to_int_array(hex); done(); }); it('should correctly convert a decimal array to string hex', function(done) { @@ -75,18 +75,18 @@ startTests = function() { dash_button.register(hex).on('detected', function() { done(); }); - fake_session.emit('packet', packet1) + fake_session.emit('packet', packet1); }); it('should not fire with more than 2 arp requests in 2 seconds', function(done) { dash_button.register(hex2).on('detected', function() { setTimeout(function() { - done() + done(); }, 50); //console.log("should only see this once") }); for(count = 0; count < 10; count++) { - //console.log("firing packet!") - fake_session.emit('packet', packet2) + //console.log("firing packet!"); + fake_session.emit('packet', packet2); } }); it('should recognize first of two arp requests', function(done) { @@ -94,13 +94,13 @@ startTests = function() { two_tester.on('detected', function(mac_address) { if(mac_address === hex2) done(); }); - fake_session.emit('packet', packet2) + fake_session.emit('packet', packet2); }); it('should recognize second of two arp requests', function(done) { two_tester.on('detected', function(mac_address) { if(mac_address === hex3) done(); }); - fake_session.emit('packet', packet3) + fake_session.emit('packet', packet3); }); -} +}; startTests(); \ No newline at end of file From 06a1f341b47843bf0307b537c6c71910d6993152 Mon Sep 17 00:00:00 2001 From: hortinstein Date: Sat, 17 Oct 2015 15:19:01 +0000 Subject: [PATCH 2/2] increased code coverage and linting --- index.js | 11 ++++------- package.json | 11 +++++------ test/test.js | 14 +++++++++++++- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 4fc5085..d987868 100644 --- a/index.js +++ b/index.js @@ -20,8 +20,8 @@ var int_array_to_hex = function (int_array) { var hex = ""; for (var i in int_array){ var h = int_array[i].toString(16); // converting to hex - if (h.length < 2) h = '0' + h; //adding a 0 for non 2 digit numbers - if (i !== int_array.length) hex+=":"; //adding a : for all but the last group + if (h.length < 2) {h = '0' + h}; //adding a 0 for non 2 digit numbers + if (i !== int_array.length) {hex+=":"}; //adding a : for all but the last group hex += h; } return hex.slice(1);//slice is to get rid of the leading : @@ -37,11 +37,8 @@ var create_session = function () { var session = pcap.createSession(); } catch (err) { console.error(err); - if (err == "Error: pcap_findalldevs didn't find any devs") { - console.log("Failed to create pcap session: couldn't find devices to listen on.\n" + - "Try running with elevated privileges via 'sudo'"); - } - process.exit(1); + console.error("Failed to create pcap session: couldn't find devices to listen on.\n" + "Try running with elevated privileges via 'sudo'"); + throw new Error('Error: No devices to listen'); } return session; }; diff --git a/package.json b/package.json index dbf2c38..b8442ab 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "livetest": "istanbul cover ./node_modules/mocha/bin/_mocha test/test-live.js --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", "test": "istanbul cover ./node_modules/mocha/bin/_mocha test/test.js --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, - "bin":{ + "bin": { "findbutton": "./bin/findbutton" }, "repository": { @@ -33,12 +33,11 @@ }, "devDependencies": { "arpjs": "^1.0.0", + "coveralls": "^2.11.4", + "istanbul": "0.3.22", "mocha": "^2.3.0", - "should": "^7.1.0", - "sinon": "^1.17.1", "mockery": "^1.4.0", - "istanbul": "0.3.22", - "coveralls": "^2.11.4" + "should": "^7.1.0", + "sinon": "^1.17.1" } - } diff --git a/test/test.js b/test/test.js index e4b3ef7..b2952bc 100644 --- a/test/test.js +++ b/test/test.js @@ -34,7 +34,7 @@ var mock_pcap = { }, decode: { packet: function(packet) { - mock_packet = { + var mock_packet = { "payload": { "ethertype": packet.packet_payload_ethertype, "payload": { @@ -48,6 +48,8 @@ var mock_pcap = { } } }; +var pcap = ""; //for linting purps +var dash_button = ""; //for linting purps startTests = function() { before(function() { mockery.enable({ @@ -102,5 +104,15 @@ startTests = function() { }); fake_session.emit('packet', packet3); }); + it('should throw an error if no interfaces are available', function(done) { + mock_pcap.createSession = function() { + throw new Error("Error: pcap_findalldevs didn't find any devs"); + }; + try { + dash_button.register('bullshit'); + } catch(err) { + done() + } + }); }; startTests(); \ No newline at end of file