From 0f6422a055d41d47dc1aac0876b0f88158d7d0fa Mon Sep 17 00:00:00 2001 From: wouterlucas Date: Tue, 28 Apr 2020 08:46:46 -0700 Subject: [PATCH] Set stricter eslint rules, clean up vars and code a little bit --- .eslintrc.js | 28 +++++-- package.json | 2 +- src/logfile.js | 89 ++++++++++------------ src/storm_jsonrpc.js | 177 ++++++++++++++++++++----------------------- 4 files changed, 144 insertions(+), 152 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 0ad5676..6aad658 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,14 +3,19 @@ module.exports = { env: { node: true, }, + plugins: ['prettier'], extends: [ 'eslint:recommended', - ], + 'plugin:vue/recommended', + 'plugin:prettier/recommended', + 'prettier', + 'prettier/vue' + ], rules: { 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off', - 'no-mixed-spaces-and-tabs': 0, - semi: [2, 'never'], + quotes: [2, 'single', 'avoid-escape'], + semi: [2, 'never'], // 'comma-dangle': ['error', 'always-multiline'], 'no-extra-boolean-cast': 'off', "no-unused-vars": [ @@ -20,10 +25,19 @@ module.exports = { "argsIgnorePattern": "res|next|^err" } ], - }, + 'prettier/prettier': [ + 'error', + { + trailingComma: 'es5', + singleQuote: true, + tabWidth: 2, + semi: false, + printWidth: 100, + }, + ], + }, parserOptions: { parser: 'babel-eslint', - "ecmaVersion": 11, - sourceType:"module", + ecmaVersion: 2017, }, -} \ No newline at end of file +} diff --git a/package.json b/package.json index f852fb8..d0f07c1 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.1", "author": "Sreelal Nampoothiri ", "scripts": { - "start":"node -r esm src/storm_jsonrpc.js", + "start": "node -r esm src/storm_jsonrpc.js", "lint": "eslint '**/*.js'", "lint:fix": "eslint '**/*.js' --fix", "postinstall": "(test -e testcases/Storm-Testcases || git clone https://github.com/WebPlatformForEmbedded/Storm-Testcases.git testcases/Storm-Testcases && cd testcases/Storm-Testcases && npm install)" diff --git a/src/logfile.js b/src/logfile.js index 56063fa..37161e7 100644 --- a/src/logfile.js +++ b/src/logfile.js @@ -1,55 +1,46 @@ import fs from 'fs' -import path from 'path' function writeFile(data, testCaseLogFile, serverLogFile) { - fs.appendFile(testCaseLogFile, data+'\n', (err) => { - if (err) throw err; - }); - process.stdout.write = process.stderr.write = serverLogFile.write.bind(serverLogFile); + fs.appendFile(testCaseLogFile, data + '\n', err => { + if (err) throw err + }) + process.stdout.write = process.stderr.write = serverLogFile.write.bind(serverLogFile) } -export default (testCaseLog, stormJsonrpcLog) =>{ -var testCaseLogFile = testCaseLog; -var serverLogFile = stormJsonrpcLog; -return { - init(test) { - var data = 'Starting test `' + test.title + '`' - writeFile(data, testCaseLogFile, serverLogFile) - }, - step(test, step) { - var data = 'Starting step `' + step.description + '`' - writeFile(data, testCaseLogFile, serverLogFile) - }, - log() { - var data =[' ', ...arguments] - data = JSON.stringify(data); - data = data.substring(data.indexOf(',')+1).split(']') - data = ' ' + data[0] - writeFile(data, testCaseLogFile, serverLogFile) - }, - sleep(milliseconds) { - var data = 'Sleeping for ' + milliseconds / 1000 + ' seconds' - writeFile(data, testCaseLogFile, serverLogFile) - }, - pass(test, step) { - var data ='Step `' + step.description + '` passed' - writeFile(data, testCaseLogFile, serverLogFile) - }, - fail(test, step, err) { - var data ='Step `' + step.description + '` failed ' + err - writeFile(data, testCaseLogFile, serverLogFile) - }, - success(test) { - var data = 'Success ' + test.title - writeFile(data, testCaseLogFile, serverLogFile) - }, - error(test, err) { - var data = 'Error ' + test.title + err - writeFile(data, testCaseLogFile, serverLogFile) - }, - finished(test) { - var data = 'Test ' + test.title + ' finished running' - writeFile(data, testCaseLogFile, serverLogFile) - }, -} +export default (testCaseLog, stormJsonrpcLog) => { + let testCaseLogFile = testCaseLog + let serverLogFile = stormJsonrpcLog + return { + init(test) { + writeFile('Starting test `' + test.title + '`', testCaseLogFile, serverLogFile) + }, + step(test, step) { + writeFile('Starting step `' + step.description + '`', testCaseLogFile, serverLogFile) + }, + log() { + let data = [' ', ...arguments] + data = JSON.stringify(data) + data = data.substring(data.indexOf(',') + 1).split(']') + data = ' ' + data[0] + writeFile(data, testCaseLogFile, serverLogFile) + }, + sleep(milliseconds) { + writeFile('Sleeping for ' + milliseconds / 1000 + ' seconds', testCaseLogFile, serverLogFile) + }, + pass(test, step) { + writeFile('Step `' + step.description + '` passed', testCaseLogFile, serverLogFile) + }, + fail(test, step, err) { + writeFile('Step `' + step.description + '` failed ' + err, testCaseLogFile, serverLogFile) + }, + success(test) { + writeFile('Success ' + test.title, testCaseLogFile, serverLogFile) + }, + error(test, err) { + writeFile('Error ' + test.title + err, testCaseLogFile, serverLogFile) + }, + finished(test) { + writeFile('Test ' + test.title + ' finished running', testCaseLogFile, serverLogFile) + }, + } } diff --git a/src/storm_jsonrpc.js b/src/storm_jsonrpc.js index 0c5c4ee..47957c5 100644 --- a/src/storm_jsonrpc.js +++ b/src/storm_jsonrpc.js @@ -2,122 +2,109 @@ import fs from 'fs' import path from 'path' import StormRunner from 'storm' -const RpcServer = require('http-jsonrpc-server'); +const RpcServer = require('http-jsonrpc-server') import log from './logfile.js' -var testCasePath; +var testCasePath // To get the location of the testcase within testcases folder which needs to executed function getTestcasePath(dirPath, testCase, arrayOfFiles) { + let files = fs.readdirSync(dirPath) + arrayOfFiles = arrayOfFiles || [] - var files = fs.readdirSync(dirPath) - arrayOfFiles = arrayOfFiles || [] - - files.forEach(function(file) - { - if (fs.statSync(dirPath + "/" + file).isDirectory()) - { - arrayOfFiles = getTestcasePath(dirPath + "/" + file, testCase, arrayOfFiles) - } - else - { - arrayOfFiles.push(path.join(__dirname, dirPath, "/", file)) - if (file == testCase) - { - testCasePath = path.join(__dirname, dirPath, "/", file) - } - } - }) - return arrayOfFiles + files.forEach(function(file) { + if (fs.statSync(dirPath + '/' + file).isDirectory()) { + arrayOfFiles = getTestcasePath(dirPath + '/' + file, testCase, arrayOfFiles) + } else { + arrayOfFiles.push(path.join(__dirname, dirPath, '/', file)) + if (file == testCase) { + testCasePath = path.join(__dirname, dirPath, '/', file) + } + } + }) + return arrayOfFiles } // To start the test case execution function execute_testcase(params) { - process.chdir(__dirname); - var log_dir = './logs'; - if (!fs.existsSync(log_dir)) - { - //Modified for solving permission issue while writing logs - fs.mkdirSync(log_dir,'766'); - } - testCasePath = "" - const testCase = params.test - const deviceIp = params.device - const execId = params.execid - getTestcasePath("../testcases/Storm-Testcases/src/tests/", testCase) + process.chdir(__dirname) + let log_dir = './logs' + if (!fs.existsSync(log_dir)) { + //Modified for solving permission issue while writing logs + fs.mkdirSync(log_dir, '766') + } + testCasePath = '' + const testCase = params.test + const deviceIp = params.device + const execId = params.execid + getTestcasePath('../testcases/Storm-Testcases/src/tests/', testCase) - var stormJsonrpcLog = fs.createWriteStream(__dirname+'/logs/'+testCase+'_'+execId+'_Serverconsole.log'); + let stormJsonrpcLog = fs.createWriteStream( + __dirname + '/logs/' + testCase + '_' + execId + '_Serverconsole.log' + ) - if (!testCase || !fs.existsSync(testCasePath)) - { - process.stdout.write = process.stderr.write = stormJsonrpcLog.write.bind(stormJsonrpcLog); - console.log('Test case ' + testCase + ' not found') - result = 'Test case ' + testCase + ' not found' - return result; - } - if (!deviceIp) - { - process.stdout.write = process.stderr.write = stormJsonrpcLog.write.bind(stormJsonrpcLog); - console.log('Device IP is not available') - result = 'Device IP is not available' - return result; - } - else - { - var testCaseLog = fs.createWriteStream(__dirname+'/logs/'+testCase+'_'+execId+'_Execution.log'); - process.stdout.write = process.stderr.write = stormJsonrpcLog.write.bind(stormJsonrpcLog); - import(testCasePath) - .then(testCase => { - StormRunner(testCase.default, log(testCaseLog.path,stormJsonrpcLog), deviceIp) - .then(console.log) - .catch(console.error) - }) - .catch(console.error) - } + if (!testCase || !fs.existsSync(testCasePath)) { + process.stdout.write = process.stderr.write = stormJsonrpcLog.write.bind(stormJsonrpcLog) + console.log('Test case ' + testCase + ' not found') + result = 'Test case ' + testCase + ' not found' + return result + } - var result = {'ExecutionLog':testCaseLog.path,'ServerLog':stormJsonrpcLog.path} + if (!deviceIp) { + process.stdout.write = process.stderr.write = stormJsonrpcLog.write.bind(stormJsonrpcLog) + console.log('Device IP is not available') + result = 'Device IP is not available' return result + } + + let testCaseLog = fs.createWriteStream( + __dirname + '/logs/' + testCase + '_' + execId + '_Execution.log' + ) + process.stdout.write = process.stderr.write = stormJsonrpcLog.write.bind(stormJsonrpcLog) + import(testCasePath) + .then(testCase => { + StormRunner(testCase.default, log(testCaseLog.path, stormJsonrpcLog), deviceIp) + .then(console.log) + .catch(console.error) + }) + .catch(console.error) + + var result = { ExecutionLog: testCaseLog.path, ServerLog: stormJsonrpcLog.path } + return result } // To check the json-rpc server status -function check_rpcserver_status() -{ - if (rpcServer.server.listening) - { - console.log('JSON-RPC server is in listening mode'); - return 'LISTEN' - } - else - { - console.log('JSON-RPC server is not running'); - return 'DOWN' - } +function check_rpcserver_status() { + if (rpcServer.server.listening) { + console.log('JSON-RPC server is in listening mode') + return 'LISTEN' + } else { + console.log('JSON-RPC server is not running') + return 'DOWN' + } } // To Stop the json-rpc server -function stop_rpcserver() -{ - rpcServer.close().then(() => { - console.log('JSON-RPC server stopped'); - return 'STOPPED'; - - }) +function stop_rpcserver() { + rpcServer.close().then(() => { + console.log('JSON-RPC server stopped') + return 'STOPPED' + }) } // Registering json-rpc methods and start the server const rpcServer = new RpcServer({ - path: '/storm-jsonrpc' , - methods: { - execute_testcase, - check_rpcserver_status, - stop_rpcserver, - } -}); - rpcServer.setMethod('executeTest',execute_testcase); - rpcServer.setMethod('checkRpcServer',check_rpcserver_status); - rpcServer.setMethod('stopRpcServer',stop_rpcserver); - - rpcServer.listen(9091, '127.0.0.1').then(() => { - console.log('JSON RPC server is listening at http://127.0.0.1:9091/storm-jsonrpc'); - -}); + path: '/storm-jsonrpc', + methods: { + execute_testcase, + check_rpcserver_status, + stop_rpcserver, + }, +}) +rpcServer.setMethod('executeTest', execute_testcase) +rpcServer.setMethod('checkRpcServer', check_rpcserver_status) +rpcServer.setMethod('stopRpcServer', stop_rpcserver) + +rpcServer.listen(9091, '127.0.0.1').then(() => { + console.log('JSON RPC server is listening at http://127.0.0.1:9091/storm-jsonrpc') +})