From 7dd329b5ee589f9477e7a2e838e3cdbb066b24a9 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Tue, 22 May 2018 17:26:52 +0100 Subject: [PATCH] Add basic loading tests for GPIO nodes --- nodes/core/hardware/36-rpi-gpio.js | 26 +++--- nodes/core/locales/en-US/messages.json | 2 +- test/nodes/core/hardware/36-rpi-gpio_spec.js | 90 ++++++++++++++++++++ 3 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 test/nodes/core/hardware/36-rpi-gpio_spec.js diff --git a/nodes/core/hardware/36-rpi-gpio.js b/nodes/core/hardware/36-rpi-gpio.js index 40c3d0f9b..0a21f578a 100644 --- a/nodes/core/hardware/36-rpi-gpio.js +++ b/nodes/core/hardware/36-rpi-gpio.js @@ -340,20 +340,22 @@ module.exports = function(RED) { RED.nodes.registerType("rpi-keyboard",PiKeyboardNode); var pitype = { type:"" }; - exec(gpioCommand+" info", function(err,stdout,stderr) { - if (err) { - RED.log.info(RED._("rpi-gpio.errors.version")); - } - else { - try { - var info = JSON.parse( stdout.trim().replace(/\'/g,"\"") ); - pitype.type = info["TYPE"]; + if (allOK === true) { + exec(gpioCommand+" info", function(err,stdout,stderr) { + if (err) { + RED.log.info(RED._("rpi-gpio.errors.version")); } - catch(e) { - RED.log.info(RED._("rpi-gpio.errors.sawpitype"),stdout.trim()); + else { + try { + var info = JSON.parse( stdout.trim().replace(/\'/g,"\"") ); + pitype.type = info["TYPE"]; + } + catch(e) { + RED.log.info(RED._("rpi-gpio.errors.sawpitype"),stdout.trim()); + } } - } - }); + }); + } RED.httpAdmin.get('/rpi-gpio/:id', RED.auth.needsPermission('rpi-gpio.read'), function(req,res) { res.json(pitype); diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index 15d66c9dc..d59f3d1bf 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -786,7 +786,7 @@ "na": "N/A : __value__" }, "errors": { - "ignorenode": "Ignoring Raspberry Pi specific node", + "ignorenode": "Raspberry Pi specific node set inactive", "version": "Failed to get version from Pi", "sawpitype": "Saw Pi Type", "libnotfound": "Cannot find Pi RPi.GPIO python library", diff --git a/test/nodes/core/hardware/36-rpi-gpio_spec.js b/test/nodes/core/hardware/36-rpi-gpio_spec.js new file mode 100644 index 000000000..3933154c0 --- /dev/null +++ b/test/nodes/core/hardware/36-rpi-gpio_spec.js @@ -0,0 +1,90 @@ +/** + * Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +var should = require("should"); +var rpi = require("../../../../nodes/core/hardware/36-rpi-gpio.js"); +var helper = require("node-red-node-test-helper"); +var fs = require("fs"); + +describe('RPI GPIO Node', function() { + + before(function(done) { + helper.startServer(done); + }); + + after(function(done) { + helper.stopServer(done); + }); + + afterEach(function() { + helper.unload(); + }); + + var checkIgnore = function(done) { + setTimeout(function() { + try { + var logEvents = helper.log().args.filter(function(evt) { + return ((evt[0].level == 30) && (evt[0].msg.indexOf("rpi-gpio")===0)); + }); + logEvents.should.have.length(1); + logEvents[0][0].should.have.a.property('msg'); + logEvents[0][0].msg.toString().should.startWith("rpi-gpio : rpi-gpio.errors.ignorenode"); + done(); + } catch(err) { + done(err); + } + },25); + } + + it('should load Input node', function(done) { + var flow = [{id:"n1", type:"rpi-gpio in", name:"rpi-gpio in" }]; + helper.load(rpi, flow, function() { + var n1 = helper.getNode("n1"); + n1.should.have.property('name', 'rpi-gpio in'); + try { + var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString(); + if (cpuinfo.indexOf(": BCM") === 1) { + done(); // It's ON a PI ... should really do more tests ! + } else { + checkIgnore(done); + } + } + catch(e) { + checkIgnore(done); + } + }); + }); + + it('should load Output node', function(done) { + var flow = [{id:"n1", type:"rpi-gpio out", name:"rpi-gpio out" }]; + helper.load(rpi, flow, function() { + var n1 = helper.getNode("n1"); + n1.should.have.property('name', 'rpi-gpio out'); + try { + var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString(); + if (cpuinfo.indexOf(": BCM") === 1) { + done(); // It's ON a PI ... should really do more tests ! + } else { + checkIgnore(done); + } + } + catch(e) { + checkIgnore(done); + } + }); + }); + +});