Skip to content

Commit c069eca

Browse files
authored
Merge pull request #556 from magento/MQE-1959
MQE-1959: Static-checks command can be configured
2 parents fa4cbe2 + d419049 commit c069eca

File tree

2 files changed

+98
-13
lines changed

2 files changed

+98
-13
lines changed

docs/commands/mftf.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,18 +463,39 @@ The example parameters are taken from the `etc/config/.env.example` file.
463463

464464
### `static-checks`
465465

466-
Runs all MFTF static-checks on the test codebase that MFTF is currently attached to.
466+
Runs all or specific MFTF static-checks on the test codebase that MFTF is currently attached to.
467+
If no script name argument is specified, all existing static check scripts will run.
467468

468-
#### Existing static checks
469+
#### Usage
469470

470-
* Test Dependency: Checks that test dependencies do not violate Magento module's composer dependencies.
471+
```bash
472+
vendor/bin/mftf static-checks [<names>]...
473+
```
471474

472-
#### Usage
475+
#### Examples
476+
477+
To check what existing static check scripts are available
478+
479+
```bash
480+
vendor/bin/mftf static-checks --help
481+
```
482+
483+
To run all existing static check scripts
473484

474485
```bash
475486
vendor/bin/mftf static-checks
476487
```
477488

489+
To run specific static check scripts
490+
491+
```bash
492+
vendor/bin/mftf static-checks testDependencies
493+
```
494+
495+
#### Existing static checks
496+
497+
* Test Dependency: Checks that test dependencies do not violate Magento module's composer dependencies.
498+
478499
### `upgrade:tests`
479500

480501
Applies all the MFTF major version upgrade scripts to test components in the given path (`test.xml`, `data.xml`, etc).

src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,31 @@
88

99
namespace Magento\FunctionalTestingFramework\Console;
1010

11+
use Magento\FunctionalTestingFramework\StaticCheck\StaticCheckInterface;
1112
use Magento\FunctionalTestingFramework\StaticCheck\StaticChecksList;
12-
use Magento\FunctionalTestingFramework\StaticCheck\StaticCheckListInterface;
1313
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Exception\InvalidArgumentException;
16+
use Symfony\Component\Console\Input\InputArgument;
1517
use Symfony\Component\Console\Input\InputInterface;
1618
use Symfony\Component\Console\Output\OutputInterface;
1719
use Exception;
1820

1921
class StaticChecksCommand extends Command
2022
{
2123
/**
22-
* Pool of static check scripts to run
24+
* Pool of all existing static check objects
2325
*
24-
* @var StaticCheckListInterface
26+
* @var StaticCheckInterface[]
2527
*/
26-
private $staticChecksList;
28+
private $allStaticCheckObjects;
29+
30+
/**
31+
* Static checks to run
32+
*
33+
* @var StaticCheckInterface[]
34+
*/
35+
private $staticCheckObjects;
2736

2837
/**
2938
* Configures the current command.
@@ -32,13 +41,22 @@ class StaticChecksCommand extends Command
3241
*/
3342
protected function configure()
3443
{
44+
$list = new StaticChecksList();
45+
$this->allStaticCheckObjects = $list->getStaticChecks();
46+
$staticCheckNames = implode(', ', array_keys($this->allStaticCheckObjects));
47+
$description = "This command will run all static checks on xml test materials. "
48+
. "Available static check scripts are:\n{$staticCheckNames}";
3549
$this->setName('static-checks')
36-
->setDescription('This command will run all static checks on xml test materials.');
37-
$this->staticChecksList = new StaticChecksList();
50+
->setDescription($description)
51+
->addArgument(
52+
'names',
53+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
54+
'name(s) of specific static check script(s) to run'
55+
);
3856
}
3957

4058
/**
41-
*
59+
* Run required static check scripts
4260
*
4361
* @param InputInterface $input
4462
* @param OutputInterface $output
@@ -47,11 +65,23 @@ protected function configure()
4765
*/
4866
protected function execute(InputInterface $input, OutputInterface $output)
4967
{
50-
$staticCheckObjects = $this->staticChecksList->getStaticChecks();
68+
try {
69+
$this->validateInputArguments($input, $output);
70+
} catch (InvalidArgumentException $e) {
71+
LoggingUtil::getInstance()->getLogger(StaticChecksCommand::class)->error($e->getMessage());
72+
$output->writeln($e->getMessage() . " Please fix input arguments and rerun.");
73+
return 1;
74+
}
5175

5276
$errors = [];
77+
foreach ($this->staticCheckObjects as $name => $staticCheck) {
78+
LoggingUtil::getInstance()->getLogger(get_class($staticCheck))->info(
79+
"\nRunning static check script for: " . $name
80+
);
81+
$output->writeln(
82+
"\nRunning static check script for: " . $name
83+
);
5384

54-
foreach ($staticCheckObjects as $staticCheck) {
5585
$staticCheck->execute($input);
5686

5787
$staticOutput = $staticCheck->getOutput();
@@ -66,4 +96,38 @@ protected function execute(InputInterface $input, OutputInterface $output)
6696
return 1;
6797
}
6898
}
99+
100+
/**
101+
* Validate input arguments
102+
*
103+
* @param InputInterface $input
104+
* @return void
105+
* @throws InvalidArgumentException
106+
*/
107+
private function validateInputArguments(InputInterface $input)
108+
{
109+
$this->staticCheckObjects = [];
110+
$requiredChecksNames = $input->getArgument('names');
111+
$invalidCheckNames = [];
112+
// Found user required static check script(s) to run,
113+
// If no static check name is supplied, run all static check scripts
114+
if (empty($requiredChecksNames)) {
115+
$this->staticCheckObjects = $this->allStaticCheckObjects;
116+
} else {
117+
for ($index = 0; $index < count($requiredChecksNames); $index++) {
118+
if (in_array($requiredChecksNames[$index], array_keys($this->allStaticCheckObjects))) {
119+
$this->staticCheckObjects[$requiredChecksNames[$index]] =
120+
$this->allStaticCheckObjects[$requiredChecksNames[$index]];
121+
} else {
122+
$invalidCheckNames[] = $requiredChecksNames[$index];
123+
}
124+
}
125+
}
126+
127+
if (!empty($invalidCheckNames)) {
128+
throw new InvalidArgumentException(
129+
"Invalid static check script(s): " . implode(', ', $invalidCheckNames) . "."
130+
);
131+
}
132+
}
69133
}

0 commit comments

Comments
 (0)