Skip to content

Commit

Permalink
fix(core): allow scenario name exact match without using regex
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardobridge committed May 7, 2024
1 parent 02da550 commit 73b1dfe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
33 changes: 31 additions & 2 deletions packages/artillery/test/cli/command-run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,49 @@ tap.test('Can specify scenario to run by name', async (t) => {
);
});

tap.test('Can specify scenario to run by name', async (t) => {
const [exitCode, output] = await execute([
'run',
'--scenario-name',
'Test Scenario (4)',
'-o',
`${reportFilePath}`,
'test/scripts/scenario-named/scenario.yml'
]);

t.equal(exitCode, 0, 'CLI should exit with code 0');
t.ok(
output.stdout.includes('Successfully running scenario 4'),
'Should log success'
);
const json = JSON.parse(fs.readFileSync(reportFilePath, 'utf8'));

t.equal(
json.aggregate.counters['vusers.created_by_name.Test Scenario (4)'],
6,
'Should have created 6 vusers for the right scenario'
);
t.type(
json.aggregate.counters['vusers.created_by_name.Test Scenario 1'],
'undefined',
'Should not have created vusers for the wrong scenario'
);
});

tap.test(
'Errors correctly when specifying a non-existing scenario by name',
async (t) => {
const [exitCode, output] = await execute([
'run',
'--scenario-name',
'Test Scenario 4',
'Test Scenario 5',
'test/scripts/scenario-named/scenario.yml'
]);

t.equal(exitCode, 11);
t.ok(
output.stdout.includes(
'Error: Scenario Test Scenario 4 not found in script. Make sure your chosen scenario matches the one in your script exactly.'
'Error: Scenario Test Scenario 5 not found in script. Make sure your chosen scenario matches the one in your script exactly.'
),
'Should log error when scenario not found'
);
Expand Down
5 changes: 4 additions & 1 deletion packages/artillery/test/scripts/scenario-named/scenario.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ scenarios:
- log: "Successfully running scenario 2"
- name: Test Scenario 3
flow:
- log: "Successfully running scenario 3"
- log: "Successfully running scenario 3"
- name: Test Scenario (4)
flow:
- log: "Successfully running scenario 4"
6 changes: 5 additions & 1 deletion packages/core/lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ function runScenario(script, metrics, runState, contextVars, options) {
if (options.scenarioName) {
let foundIndex;
const foundScenario = script.scenarios.filter((scenario, index) => {
const hasScenario = new RegExp(options.scenarioName).test(scenario.name);
const hasScenarioByRegex = new RegExp(options.scenarioName).test(
scenario.name
);
const hasScenarioByName = scenario.name === options.scenarioName;
const hasScenario = hasScenarioByRegex || hasScenarioByName;

if (hasScenario) {
foundIndex = index;
Expand Down

0 comments on commit 73b1dfe

Please sign in to comment.