Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set stricter eslint rules, clean up vars and code a little bit #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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,
},
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"author": "Sreelal Nampoothiri <[email protected]>",
"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)"
Expand Down
89 changes: 40 additions & 49 deletions src/logfile.js
Original file line number Diff line number Diff line change
@@ -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)
},
}
}
177 changes: 82 additions & 95 deletions src/storm_jsonrpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})