From ef681a0ab5448f773ca0fad22bf041787d12a8d3 Mon Sep 17 00:00:00 2001 From: Nicolas Oliver Date: Wed, 16 Aug 2017 07:56:02 -0700 Subject: [PATCH] add initial jsonlint and mocha test for json files [ci skip] Signed-off-by: Nicolas Oliver Signed-off-by: Abhishek Malik --- tests/node/jsonlint.js | 61 +++++++++++++++++++++++++++++++++++++++++ tests/node/package.json | 19 +++++++++++++ tests/node/test.js | 57 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 tests/node/jsonlint.js create mode 100644 tests/node/package.json create mode 100644 tests/node/test.js diff --git a/tests/node/jsonlint.js b/tests/node/jsonlint.js new file mode 100644 index 00000000..0ccfa210 --- /dev/null +++ b/tests/node/jsonlint.js @@ -0,0 +1,61 @@ +var shell = require('shelljs'); +var path = require('path'); + +var rootPath = path.resolve(__dirname, '../../'); +var srcPath = path.resolve(rootPath, 'src'); + +var jsonlintCmd = path.resolve(__dirname, 'node_modules/.bin/jsonlint'); +var jsonlintOpts = ' --quiet '; + +var failures = []; + +function getRelativePath(filePath) { + return path.relative(rootPath, filePath); +} + +function printSummaryAndExit() { + var exitCode = 0; + + if (failures.length > 0) { + console.error(); + console.error('Failures:'); + failures.forEach(function (file) { + console.error(' ', getRelativePath(file)); + }); + exitCode = 1; + } + else { + console.log(); + console.log('Success'); + } + + process.exit(exitCode); +} + +var pending = 0; + +shell.find(srcPath) + .filter(function (file) { + return file.match(/\.json$/); + }) + .forEach(function (jsonFile) { + pending++; + var relativePath = getRelativePath(jsonFile); + shell.exec(jsonlintCmd + jsonlintOpts + jsonFile, {silent: true}, function (code, stdout, stderr) { + if (code) { + console.error('Failed', relativePath); + console.error(stderr); + failures.push(jsonFile); + } + else { + console.log('Success', relativePath); + } + + pending--; + + if (pending == 0) { + printSummaryAndExit(); + } + }); + }); + diff --git a/tests/node/package.json b/tests/node/package.json new file mode 100644 index 00000000..376a61e7 --- /dev/null +++ b/tests/node/package.json @@ -0,0 +1,19 @@ +{ + "name": "upm-node-tests", + "version": "1.0.0", + "description": "Node.js tests for UPM", + "main": "index.js", + "scripts": { + "test": "npm run jsonlint && npm run mocha", + "mocha": "mocha test.js", + "jsonlint": "node jsonlint.js" + }, + "author": "", + "license": "ISC", + "devDependencies": { + "chai": "^4.1.1", + "jsonlint": "^1.6.2", + "mocha": "^3.5.0", + "shelljs": "^0.7.8" + } +} diff --git a/tests/node/test.js b/tests/node/test.js new file mode 100644 index 00000000..487ee4c6 --- /dev/null +++ b/tests/node/test.js @@ -0,0 +1,57 @@ +var shell = require('shelljs'); +var path = require('path'); +var expect = require('chai').expect; + +var rootPath = path.resolve(__dirname, '../../'); +var srcPath = path.resolve(rootPath, 'src'); +var examplePath = path.resolve(rootPath, 'examples'); + +shell.find(srcPath) + .filter(function (file) { + return file.match(/\.json$/); + }) + .forEach(function (jsonFile) { + var relativePath = path.relative(rootPath, jsonFile); + + describe(relativePath, function () { + // Check required fields + [ + 'Library', + 'Description', + 'Sensor Class' + ].forEach(function (fieldName) { + it('should have a ' + fieldName + ' property', function () { + var parsedJsonFile = require(jsonFile); + expect(parsedJsonFile).to.have.property(fieldName); + }); + }); + + // Check Sensor Class required fields + [ + 'Name', + 'Description', + 'Aliases', + 'Categories', + 'Connections', + 'Project Type', + 'Manufacturers', + 'Image', + 'Examples', + 'Specifications', + 'Platforms', + 'Urls' + ].forEach(function (fieldName) { + var parsedJsonFile = require(jsonFile); + sensorClass = parsedJsonFile['Sensor Class']; + + for(var className in sensorClass) { + it(className + ' should have a ' + fieldName + ' property', function () { + var parsedJsonFile = require(jsonFile); + sensorClass = parsedJsonFile['Sensor Class']; + expect(sensorClass[className]).to.have.property(fieldName); + }); + } + }); + }); + }); +