From 562344f34f5c5b832c789fc9729186d03dc08087 Mon Sep 17 00:00:00 2001 From: Bernardo Guerreiro Date: Tue, 7 May 2024 12:45:08 +0100 Subject: [PATCH 1/2] fix(core): allow scenario name exact match without using regex --- .../artillery/test/cli/command-run.test.js | 33 +++++++++++++++++-- .../test/scripts/scenario-named/scenario.yml | 5 ++- packages/core/lib/runner.js | 6 +++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/artillery/test/cli/command-run.test.js b/packages/artillery/test/cli/command-run.test.js index e5a15a6650..42900b7d57 100644 --- a/packages/artillery/test/cli/command-run.test.js +++ b/packages/artillery/test/cli/command-run.test.js @@ -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' ); diff --git a/packages/artillery/test/scripts/scenario-named/scenario.yml b/packages/artillery/test/scripts/scenario-named/scenario.yml index af24bbd9d3..a710f4ef20 100644 --- a/packages/artillery/test/scripts/scenario-named/scenario.yml +++ b/packages/artillery/test/scripts/scenario-named/scenario.yml @@ -13,4 +13,7 @@ scenarios: - log: "Successfully running scenario 2" - name: Test Scenario 3 flow: - - log: "Successfully running scenario 3" \ No newline at end of file + - log: "Successfully running scenario 3" + - name: Test Scenario (4) + flow: + - log: "Successfully running scenario 4" \ No newline at end of file diff --git a/packages/core/lib/runner.js b/packages/core/lib/runner.js index a61fa40d95..a12a95bd77 100644 --- a/packages/core/lib/runner.js +++ b/packages/core/lib/runner.js @@ -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; From dd63f781f034dbc8424d5d5f4b0a7c32c7f22c7a Mon Sep 17 00:00:00 2001 From: Bernardo Guerreiro Date: Mon, 13 May 2024 15:20:14 +0100 Subject: [PATCH 2/2] refactor(core): switch order of regex vs exact match --- packages/core/lib/runner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/lib/runner.js b/packages/core/lib/runner.js index a12a95bd77..b4eddf1c72 100644 --- a/packages/core/lib/runner.js +++ b/packages/core/lib/runner.js @@ -341,7 +341,7 @@ function runScenario(script, metrics, runState, contextVars, options) { scenario.name ); const hasScenarioByName = scenario.name === options.scenarioName; - const hasScenario = hasScenarioByRegex || hasScenarioByName; + const hasScenario = hasScenarioByName || hasScenarioByRegex; if (hasScenario) { foundIndex = index;