Skip to content

Commit d230053

Browse files
committed
Add support for behat --tags and --name options
Covered by tests that use intensively the new ->lastCmd introduced by the previous commit. Also, added a TODO about something, pre-existing, that is an incorrect unit tests, some day will be investigated. Not today. Fixes #246
1 parent 9d682b5 commit d230053

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).
1010

1111
## [Unreleased]
12+
### Added
13+
- Added support for the `--tags` and `--name` options into the `behat` command.
14+
1215
### Changed
1316
- ACTION SUGGESTED: If you are using GitHub Actions, it's recomended to use `!cancelled()` instead of `always()` for moodle-plugin-ci tests. Adding a final step that always returns failure when the workflow is cancelled will ensure that cancelled workflows are not marked as successful. For a working example, please reference the updated `gha.dist.yml` file.
1417
- ACTION SUGGESTED: For some (unknown) reason, Travis environments with PHP 8.2 have started to fail with error:

docs/CLI.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ Run Behat on a plugin
241241

242242
### Usage
243243

244-
* `behat [-m|--moodle MOODLE] [-p|--profile PROFILE] [--suite SUITE] [--start-servers] [--auto-rerun AUTO-RERUN] [--dump] [--] <plugin>`
244+
* `behat [-m|--moodle MOODLE] [-p|--profile PROFILE] [--suite SUITE] [--tags TAGS] [--name NAME] [--start-servers] [--auto-rerun AUTO-RERUN] [--dump] [--] <plugin>`
245245

246246
Run Behat on a plugin
247247

@@ -269,7 +269,7 @@ Path to Moodle
269269

270270
#### `--profile|-p`
271271

272-
Behat profile to use
272+
Behat profile option to use
273273

274274
* Accept value: yes
275275
* Is value required: yes
@@ -279,14 +279,34 @@ Behat profile to use
279279

280280
#### `--suite`
281281

282-
Behat suite to use (Moodle theme)
282+
Behat suite option to use (Moodle theme)
283283

284284
* Accept value: yes
285285
* Is value required: yes
286286
* Is multiple: no
287287
* Is negatable: no
288288
* Default: `'default'`
289289

290+
#### `--tags`
291+
292+
Behat tags option to use. If not set, defaults to the component name
293+
294+
* Accept value: yes
295+
* Is value required: yes
296+
* Is multiple: no
297+
* Is negatable: no
298+
* Default: `''`
299+
300+
#### `--name`
301+
302+
Behat name option to use
303+
304+
* Accept value: yes
305+
* Is value required: yes
306+
* Is multiple: no
307+
* Is negatable: no
308+
* Default: `''`
309+
290310
#### `--start-servers`
291311

292312
Start Selenium and PHP servers

src/Command/BehatCommand.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ protected function configure(): void
5858
parent::configure();
5959

6060
$this->setName('behat')
61-
->addOption('profile', 'p', InputOption::VALUE_REQUIRED, 'Behat profile to use', 'default')
62-
->addOption('suite', null, InputOption::VALUE_REQUIRED, 'Behat suite to use (Moodle theme)', 'default')
61+
->addOption('profile', 'p', InputOption::VALUE_REQUIRED, 'Behat profile option to use', 'default')
62+
->addOption('suite', null, InputOption::VALUE_REQUIRED, 'Behat suite option to use (Moodle theme)', 'default')
63+
->addOption('tags', null, InputOption::VALUE_REQUIRED, 'Behat tags option to use. ' .
64+
'If not set, defaults to the component name', '')
65+
->addOption('name', null, InputOption::VALUE_REQUIRED, 'Behat name option to use', '')
6366
->addOption('start-servers', null, InputOption::VALUE_NONE, 'Start Selenium and PHP servers')
6467
->addOption('auto-rerun', null, InputOption::VALUE_REQUIRED, 'Number of times to rerun failures', 2)
6568
->addOption('dump', null, InputOption::VALUE_NONE, 'Print contents of Behat failure HTML files')
@@ -89,14 +92,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8992

9093
$cmd = [
9194
'php', 'admin/tool/behat/cli/run.php',
92-
'--tags=@' . $this->plugin->getComponent(),
9395
'--profile=' . $input->getOption('profile'),
9496
'--suite=' . $input->getOption('suite'),
97+
'--tags=' . ($input->getOption('tags') ?: '@' . $this->plugin->getComponent()),
9598
'--auto-rerun=' . $input->getOption('auto-rerun'),
9699
'--verbose',
97100
'-vvv',
98101
];
99102

103+
$name = $input->getOption('name');
104+
if (!empty($name) && is_string($name)) {
105+
$cmd[] = '--name=\'' . $name . '\'';
106+
}
107+
100108
if ($output->isDecorated()) {
101109
$cmd[] = '--colors';
102110
}

tests/Command/BehatCommandTest.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
class BehatCommandTest extends MoodleTestCase
2323
{
24-
protected function executeCommand($pluginDir = null, $moodleDir = null): CommandTester
24+
protected function executeCommand($pluginDir = null, $moodleDir = null, array $cmdOptions = []): CommandTester
2525
{
2626
if ($pluginDir === null) {
2727
$pluginDir = $this->pluginDir;
@@ -38,10 +38,15 @@ protected function executeCommand($pluginDir = null, $moodleDir = null): Command
3838
$application->add($command);
3939

4040
$commandTester = new CommandTester($application->find('behat'));
41-
$commandTester->execute([
42-
'plugin' => $pluginDir,
43-
'--moodle' => $moodleDir,
44-
]);
41+
$cmdOptions = array_merge(
42+
[
43+
'plugin' => $pluginDir,
44+
'--moodle' => $moodleDir,
45+
],
46+
$cmdOptions
47+
);
48+
$commandTester->execute($cmdOptions);
49+
$this->lastCmd = $command->execute->lastCmd; // We need this for assertions against the command run.
4550

4651
return $commandTester;
4752
}
@@ -50,6 +55,28 @@ public function testExecute()
5055
{
5156
$commandTester = $this->executeCommand();
5257
$this->assertSame(0, $commandTester->getStatusCode());
58+
$this->assertMatchesRegularExpression('/php.*admin.tool.behat.cli.run/', $this->lastCmd);
59+
$this->assertMatchesRegularExpression('/--profile=default.*--suite=default/', $this->lastCmd);
60+
$this->assertMatchesRegularExpression('/--tags=@local_ci/', $this->lastCmd);
61+
$this->assertMatchesRegularExpression('/--verbose.*-vvv/', $this->lastCmd);
62+
}
63+
64+
public function testExecuteWithTags()
65+
{
66+
$commandTester = $this->executeCommand(null, null, ['--tags' => '@tag1&&@tag2']);
67+
$this->assertSame(0, $commandTester->getStatusCode());
68+
$this->assertMatchesRegularExpression('/--tags=@tag1&&@tag2/', $this->lastCmd);
69+
$this->assertDoesNotMatchRegularExpression('/--tags=@local_ci/', $this->lastCmd);
70+
}
71+
72+
public function testExecuteWithName()
73+
{
74+
$featureName = 'With "double quotes" and \'single quotes\'';
75+
// Note that everything is escaped for shell execution, plus own regexp quoting.
76+
$expectedName = preg_quote(escapeshellarg("--name='$featureName'"));
77+
$commandTester = $this->executeCommand(null, null, ['--name' => $featureName]);
78+
$this->assertSame(0, $commandTester->getStatusCode());
79+
$this->assertMatchesRegularExpression("/{$expectedName}/", $this->lastCmd);
5380
}
5481

5582
public function testExecuteNoFeatures()
@@ -70,6 +97,7 @@ public function testExecuteNoPlugin()
7097
public function testExecuteNoMoodle()
7198
{
7299
$this->expectException(\InvalidArgumentException::class);
100+
// TODO: Check what's happening here. moodleDir should be the 2nd parameter, but then the test fails.
73101
$this->executeCommand($this->moodleDir . '/no/moodle');
74102
}
75103
}

0 commit comments

Comments
 (0)