Skip to content

Commit

Permalink
feat(cli): expose dirname utility to test scripts (#2614)
Browse files Browse the repository at this point in the history
* refactor(cli): move processor path resolution to its own function

* feat(cli): expose  utility to test scripts

* test(cli): fix unit test

* refactor(cli): improve logic for dirname
  • Loading branch information
bernardobridge authored May 13, 2024
1 parent 6680314 commit fa6acff
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 46 deletions.
61 changes: 23 additions & 38 deletions packages/artillery/lib/cmds/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const {
addVariables,
addDefaultPlugins,
resolveConfigTemplates,
checkConfig
checkConfig,
resolveConfigPath
} = require('../../util');

const p = require('util').promisify;
Expand Down Expand Up @@ -490,6 +491,7 @@ function replaceProcessorIfTypescript(
}

async function prepareTestExecutionPlan(inputFiles, flags, args) {
const scriptPath = inputFiles[0];
let script1 = {};

for (const fn of inputFiles) {
Expand All @@ -498,69 +500,52 @@ async function prepareTestExecutionPlan(inputFiles, flags, args) {
script1 = _.merge(script1, parsedData);
}

script1 = await checkConfig(script1, inputFiles[0], flags);
script1 = await checkConfig(script1, scriptPath, flags);

if (flags.config) {
const absoluteConfigPath = path.resolve(process.cwd(), flags.config);
const script2 = await resolveConfigPath(script1, flags, scriptPath);

if (script1.config?.processor) {
const newPath = path.resolve(
path.dirname(absoluteConfigPath),
script1.config.processor
);

const stats = fs.statSync(newPath, { throwIfNoEntry: false });

if (typeof stats === 'undefined') {
// No file at that path - backwards compatibility mode:
console.log(
'WARNING - config.processor is now resolved relative to the config file'
);
console.log('Expected to find file at:', newPath);
} else {
script1.config.processor = newPath;
}
}
}

const script2 = await addOverrides(script1, flags);
const script3 = await addVariables(script2, flags);
const script4 = await resolveConfigTemplates(script3, flags);
const script3 = await addOverrides(script2, flags);
const script4 = await addVariables(script3, flags);
const script5 = await resolveConfigTemplates(
script4,
flags,
script4._configPath
);

if (!script4.config.target) {
if (!script5.config.target) {
throw new Error('No target specified and no environment chosen');
}

const validationError = validateScript(script4);
const validationError = validateScript(script5);

if (validationError) {
console.log(`Scenario validation error: ${validationError}`);

process.exit(1);
}

const script5 = await readPayload(script4);
const script6 = await readPayload(script5);

if (typeof script5.config.phases === 'undefined' || flags.solo) {
script5.config.phases = [
if (typeof script6.config.phases === 'undefined' || flags.solo) {
script6.config.phases = [
{
duration: 1,
arrivalCount: 1
}
];
}

script5.config.statsInterval = script5.config.statsInterval || 30;
script6.config.statsInterval = script6.config.statsInterval || 30;

const script6 = addDefaultPlugins(script5);
const script7 = replaceProcessorIfTypescript(
script6,
inputFiles[0],
const script7 = addDefaultPlugins(script5);
const script8 = replaceProcessorIfTypescript(
script7,
scriptPath,
flags.platform,
flags.container
);

return script7;
return script8;
}

async function readPayload(script) {
Expand Down
14 changes: 8 additions & 6 deletions packages/artillery/lib/platform/aws-ecs/legacy/bom.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ function isLocalModule(modName) {
}

function applyScriptChanges(context, next) {
resolveConfigTemplates(context.opts.scriptData, context.opts.flags).then(
(resolvedConfig) => {
context.opts.scriptData = resolvedConfig;
return next(null, context);
}
);
resolveConfigTemplates(
context.opts.scriptData,
context.opts.flags,
context.opts.absoluteScriptPath
).then((resolvedConfig) => {
context.opts.scriptData = resolvedConfig;
return next(null, context);
});
}

function getPlugins(context, next) {
Expand Down
37 changes: 36 additions & 1 deletion packages/artillery/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
addOverrides,
addVariables,
addDefaultPlugins,
resolveConfigPath,
resolveConfigTemplates,
checkConfig,
renderVariables,
Expand Down Expand Up @@ -97,11 +98,12 @@ function addDefaultPlugins(script) {
return finalScript;
}

async function resolveConfigTemplates(script, flags) {
async function resolveConfigTemplates(script, flags, configPath) {
const cliVariables = flags.variables ? JSON.parse(flags.variables) : {};

script.config = engineUtil.template(script.config, {
vars: {
$dirname: path.dirname(configPath),
$testId: global.artillery.testRunId,
$processEnvironment: process.env,
$env: process.env,
Expand Down Expand Up @@ -200,6 +202,39 @@ async function checkConfig(script, scriptPath, flags) {
return script;
}

async function resolveConfigPath(script, flags, scriptPath) {
if (!flags.config) {
script._configPath = scriptPath;
return script;
}

const absoluteConfigPath = path.resolve(process.cwd(), flags.config);
script._configPath = absoluteConfigPath;

if (!script.config.processor) {
return script;
}

const processorPath = path.resolve(
path.dirname(absoluteConfigPath),
script.config.processor
);

const stats = fs.statSync(processorPath, { throwIfNoEntry: false });

if (typeof stats === 'undefined') {
// No file at that path - backwards compatibility mode:
console.log(
'WARNING - config.processor is now resolved relative to the config file'
);
console.log('Expected to find file at:', processorPath);
} else {
script.config.processor = processorPath;
}

return script;
}

function formatDuration(durationInMs) {
const duration = moment.duration(durationInMs);

Expand Down
3 changes: 2 additions & 1 deletion packages/artillery/test/unit/before_after_hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const script = {
}
]
}
]
],
_configPath: 'fakepath.yml'
};

const authToken = 'abcdefg';
Expand Down
5 changes: 5 additions & 0 deletions packages/core/lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ function createContext(script, contextVars, additionalProperties = {}) {
...additionalPropertiesWithoutOverride
};

if (script._configPath) {
INITIAL_CONTEXT.vars.$dirname = path.dirname(script._configPath);
}

let result = INITIAL_CONTEXT;

// variables from payloads:
Expand All @@ -476,6 +480,7 @@ function createContext(script, contextVars, additionalProperties = {}) {

result._uid = uuidv4();
result.vars.$uuid = result._uid;

return result;
}

Expand Down

0 comments on commit fa6acff

Please sign in to comment.