Skip to content
Permalink
Browse files

refactor: Extract utility functions from run into a util module

  • Loading branch information...
hassy committed Feb 26, 2018
1 parent c097407 commit dafeb62b5e5d0cb7f1fab42c20dec8677bbd839d
Showing with 204 additions and 178 deletions.
  1. +1 −0 console-reporter.js
  2. +9 −178 lib/commands/run.js
  3. +193 −0 lib/util.js
  4. +1 −0 util.js
@@ -0,0 +1 @@
module.exports = require('./lib/console-reporter');
@@ -19,7 +19,15 @@ const debug = require('debug')('commands:run');
const defaultOptions = require('rc')('artillery');
const validate = require('../dispatcher').validate;
const createRunner = process.env.MULTICORE ? require('../runner') : require('../runner-sp');
const createConsoleReporter = require('../console-reporter');
const createConsoleReporter = require('../../console-reporter');

const {
readScript,
parseScript,
prepareConfig,
addOverrides,
checkConfig
} = require('../../util');

module.exports = run;

@@ -212,183 +220,6 @@ function run(scriptPath, options) {
);
}

function readScript(scriptPath, callback) {
fs.readFile(scriptPath, 'utf-8', function(err, data) {
if (err) {
const msg = util.format('File not found: %s', scriptPath);
return callback(new Error(msg), null);
}

return callback(null, data, scriptPath);
});
}

function parseScript(data, scriptPath, callback) {
let script;
let fileFormat;

try {
if (/\.ya?ml$/.test(path.extname(scriptPath))) {
fileFormat = 'YAML';
script = YAML.load(data);
} else {
fileFormat = 'JSON';
script = JSON.parse(data);
}
} catch (e) {
const msg2 = `File ${scriptPath} does not appear to be valid ${fileFormat}: (${e.message})`;
return callback(new Error(msg2), null);
}

return callback(null, script);
}

function prepareConfig(script, scriptPath, options, callback) {
if (!options.config) {
return callback(null, script, scriptPath, options);
}

fs.readFile(options.config, 'utf-8', function(err, data) {
if (err) {
return callback(err, script, scriptPath, options);
}

let config;
try {
config = YAML.load(data);
} catch (e) {
return callback(e, script, scriptPath, options);
}

script = _.merge(script, config);

return callback(null, script, scriptPath, options);
});
}

function addOverrides(script, scriptPath, options, callback) {
if (options.overrides) {
let o = null;

try {
o = JSON.parse(options.overrides);
} catch (err) {}

if (!o) {
return callback(
new Error(
`Error: The values of --overrides does not seem to be valid JSON.`
)
);
}

script = _.mergeWith(script, o, function customizer(
objValue,
srcValue,
key,
object,
source,
stack
) {
if (_.isArray(srcValue)) {
return srcValue;
} else {
return undefined;
}
});
}

return callback(null, script, scriptPath, options);
}

function checkConfig(script, scriptPath, options, callback) {
if (options.environment) {
debug('environment specified: %s', options.environment);
if (script.config.environments[options.environment]) {
_.merge(script.config, script.config.environments[options.environment]);
script._environment = options.environment;
} else {
console.log(
`WARNING: environment ${options.environment} is set but is not defined in the script`
);
}
}

if (options.target && script.config) {
script.config.target = options.target;
}

if (!script.config.target && !options.environment) {
const msg4 = 'No target specified and no environment chosen';
return callback(new Error(msg4), null);
}

//
// Override/set config.tls if needed:
//
if (options.insecure) {
if (script.config.tls) {
if (script.config.tls.rejectUnauthorized) {
console.log(
'WARNING: TLS certificate validation enabled in the ' +
'test script, but explicitly disabled with ' +
'-k/--insecure.'
);
}
script.config.tls.rejectUnauthorized = false;
} else {
script.config.tls = { rejectUnauthorized: false };
}
}

//
// Turn config.payload into an array:
//
if (_.get(script, 'config.payload')) {
// Is it an object or an array?
if (_.isArray(script.config.payload)) {
// an array - nothing to do
} else if (_.isObject(script.config.payload)) {
if (options.payload && !_.get(script.config.payload, 'path')) {
script.config.payload.path = path.resolve(
process.cwd(),
options.payload
);
} else if (!options.payload && !_.get(script.config.payload, 'path')) {
console.log(
'WARNING: config.payload.path not set and payload file not specified with -p'
);
} else if (options.payload && _.get(script.config.payload, 'path')) {
console.log(
'WARNING - both -p and config.payload.path are set, config.payload.path will be ignored.'
);
script.config.payload.path = options.payload;
} else {
// no -p but config.payload.path is set - nothing to do
}

// Make it an array
script.config.payload = [script.config.payload];
} else {
console.log('Ignoring config.payload, not an object or an array.');
}
}

//
// Resolve all payload paths to absolute paths now:
//
_.forEach(script.config.payload, function(payloadSpec) {
const absoluteScriptPath = path.resolve(process.cwd(), scriptPath);
const resolvedPathToPayload = path.resolve(
path.dirname(absoluteScriptPath),
payloadSpec.path
);
payloadSpec.path = resolvedPathToPayload;
});

return callback(null, script);
}

function validateSchema(script, callback) {
let validation = validate(script);
if (!validation.valid) {
@@ -0,0 +1,193 @@
'use strict';

const fs = require('fs');
const path = require('path');
const util = require('util');
const YAML = require('yaml-js');
const debug = require('debug')('util');
const _ = require('lodash');

module.exports = {
readScript,
parseScript,
prepareConfig,
addOverrides,
checkConfig
};

function readScript(scriptPath, callback) {
fs.readFile(scriptPath, 'utf-8', function(err, data) {
if (err) {
const msg = util.format('File not found: %s', scriptPath);
return callback(new Error(msg), null);
}

return callback(null, data, scriptPath);
});
}

function parseScript(data, scriptPath, callback) {
let script;
let fileFormat;

try {
if (/\.ya?ml$/.test(path.extname(scriptPath))) {
fileFormat = 'YAML';
script = YAML.load(data);
} else {
fileFormat = 'JSON';
script = JSON.parse(data);
}
} catch (e) {
const msg2 = `File ${scriptPath} does not appear to be valid ${fileFormat}: (${e.message})`;
return callback(new Error(msg2), null);
}

return callback(null, script);
}

function prepareConfig(script, scriptPath, options, callback) {
if (!options.config) {
return callback(null, script, scriptPath, options);
}

fs.readFile(options.config, 'utf-8', function(err, data) {
if (err) {
return callback(err, script, scriptPath, options);
}

let config;
try {
config = YAML.load(data);
} catch (e) {
return callback(e, script, scriptPath, options);
}

script = _.merge(script, config);

return callback(null, script, scriptPath, options);
});
}

function addOverrides(script, scriptPath, options, callback) {
if (options.overrides) {
let o = null;

try {
o = JSON.parse(options.overrides);
} catch (err) {}

if (!o) {
return callback(
new Error(
`Error: The values of --overrides does not seem to be valid JSON.`
)
);
}

script = _.mergeWith(script, o, function customizer(
objValue,
srcValue,
key,
object,
source,
stack
) {
if (_.isArray(srcValue)) {
return srcValue;
} else {
return undefined;
}
});
}

return callback(null, script, scriptPath, options);
}

function checkConfig(script, scriptPath, options, callback) {
if (options.environment) {
debug('environment specified: %s', options.environment);
if (script.config.environments[options.environment]) {
_.merge(script.config, script.config.environments[options.environment]);
script._environment = options.environment;
} else {
console.log(
`WARNING: environment ${options.environment} is set but is not defined in the script`
);
}
}

if (options.target && script.config) {
script.config.target = options.target;
}

if (!script.config.target && !options.environment) {
const msg4 = 'No target specified and no environment chosen';
return callback(new Error(msg4), null);
}

//
// Override/set config.tls if needed:
//
if (options.insecure) {
if (script.config.tls) {
if (script.config.tls.rejectUnauthorized) {
console.log(
'WARNING: TLS certificate validation enabled in the ' +
'test script, but explicitly disabled with ' +
'-k/--insecure.'
);
}
script.config.tls.rejectUnauthorized = false;
} else {
script.config.tls = { rejectUnauthorized: false };
}
}

//
// Turn config.payload into an array:
//
if (_.get(script, 'config.payload')) {
// Is it an object or an array?
if (_.isArray(script.config.payload)) {
// an array - nothing to do
} else if (_.isObject(script.config.payload)) {
if (options.payload && !_.get(script.config.payload, 'path')) {
script.config.payload.path = path.resolve(
process.cwd(),
options.payload
);
} else if (!options.payload && !_.get(script.config.payload, 'path')) {
console.log(
'WARNING: config.payload.path not set and payload file not specified with -p'
);
} else if (options.payload && _.get(script.config.payload, 'path')) {
console.log(
'WARNING - both -p and config.payload.path are set, config.payload.path will be ignored.'
);
script.config.payload.path = options.payload;
} else {
// no -p but config.payload.path is set - nothing to do
}

// Make it an array
script.config.payload = [script.config.payload];
} else {
console.log('Ignoring config.payload, not an object or an array.');
}
}

//
// Resolve all payload paths to absolute paths now:
//
_.forEach(script.config.payload, function(payloadSpec) {
const absoluteScriptPath = path.resolve(process.cwd(), scriptPath);
const resolvedPathToPayload = path.resolve(
path.dirname(absoluteScriptPath),
payloadSpec.path
);
payloadSpec.path = resolvedPathToPayload;
});

return callback(null, script);
}
@@ -0,0 +1 @@
module.exports = require('./lib/util');

0 comments on commit dafeb62

Please sign in to comment.
You can’t perform that action at this time.