diff --git a/docs/commands/mftf.md b/docs/commands/mftf.md index a4d77ad7a..55b488e52 100644 --- a/docs/commands/mftf.md +++ b/docs/commands/mftf.md @@ -430,8 +430,12 @@ The example parameters are taken from the `etc/config/.env.example` file. ### `static-checks` -Runs all or specific MFTF static-checks on the test codebase that MFTF is currently attached to. -If no script name argument is specified, all existing static check scripts will run. +Runs all or specific MFTF static-checks on the test codebase that MFTF is currently attached to. +Behavior for determining what tests to run is as follows: + +* If test names are specified, only those tests are run. +* If no test names are specified, tests are run according to `staticRuleset.json`. +* If no `staticRuleset.json` is found, all tests are run. #### Usage @@ -489,6 +493,25 @@ vendor/bin/mftf static-checks testDependencies actionGroupArguments |`actionGroupArguments` | Checks that action groups do not have unused arguments.| |`deprecatedEntityUsage`| Checks that deprecated test entities are not being referenced.| +#### Defining ruleset + +The `static-checks` command will look for a `staticRuleset.json` file under either: + +* `dev/tests/acceptance/staticRuleset.json`, if embedded with Magento2 +* `dev/staticRuleset.json`, if standalone + +This file works as the default configuration to easily allow for the integration of `static-checks` in a CI environment. +Currently, the ruleset only defines the tests to run. Here is an example of the expected format: + +```json +{ + "tests": [ + "actionGroupArguments", + "anotherTest" + ] +} +``` + ### `upgrade:tests` When the path argument is specified, this `upgrade` command applies all the major version MFTF upgrade scripts to a `Test Module` in the given path. diff --git a/src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php b/src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php index c8242c8b1..fe07dbaaf 100644 --- a/src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php +++ b/src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php @@ -22,6 +22,13 @@ class StaticChecksCommand extends Command { + /** + * Associative array containing static ruleset properties. + * + * @var array + */ + private $ruleSet; + /** * Pool of all existing static check objects * @@ -132,26 +139,15 @@ private function validateInput(InputInterface $input) { $this->staticCheckObjects = []; $requiredChecksNames = $input->getArgument('names'); - $invalidCheckNames = []; - // Found user required static check script(s) to run, - // If no static check name is supplied, run all static check scripts + // Build list of static check names to run. + if (empty($requiredChecksNames)) { + $this->parseRulesetJson(); + $requiredChecksNames = $this->ruleSet['tests'] ?? null; + } if (empty($requiredChecksNames)) { $this->staticCheckObjects = $this->allStaticCheckObjects; } else { - for ($index = 0; $index < count($requiredChecksNames); $index++) { - if (in_array($requiredChecksNames[$index], array_keys($this->allStaticCheckObjects))) { - $this->staticCheckObjects[$requiredChecksNames[$index]] = - $this->allStaticCheckObjects[$requiredChecksNames[$index]]; - } else { - $invalidCheckNames[] = $requiredChecksNames[$index]; - } - } - } - - if (!empty($invalidCheckNames)) { - throw new InvalidArgumentException( - 'Invalid static check script(s): ' . implode(', ', $invalidCheckNames) . '.' - ); + $this->validateTestNames($requiredChecksNames); } if ($input->getOption('path')) { @@ -164,4 +160,48 @@ private function validateInput(InputInterface $input) ); } } + + /** + * Validates that all passed in static-check names match an existing static check + * @param string[] $requiredChecksNames + * @return void + */ + private function validateTestNames($requiredChecksNames) + { + $invalidCheckNames = []; + for ($index = 0; $index < count($requiredChecksNames); $index++) { + if (in_array($requiredChecksNames[$index], array_keys($this->allStaticCheckObjects))) { + $this->staticCheckObjects[$requiredChecksNames[$index]] = + $this->allStaticCheckObjects[$requiredChecksNames[$index]]; + } else { + $invalidCheckNames[] = $requiredChecksNames[$index]; + } + } + + if (!empty($invalidCheckNames)) { + throw new InvalidArgumentException( + 'Invalid static check script(s): ' . implode(', ', $invalidCheckNames) . '.' + ); + } + } + + /** + * Parses and sets local ruleSet. If not found, simply returns and lets script continue. + * @return void; + */ + private function parseRulesetJson() + { + $pathAddition = "/dev/tests/acceptance/"; + // MFTF is both NOT attached and no MAGENTO_BP defined in .env + if (MAGENTO_BP === FW_BP) { + $pathAddition = "/dev/"; + } + $pathToRuleset = MAGENTO_BP . $pathAddition . "staticRuleset.json"; + if (!file_exists($pathToRuleset)) { + $this->ioStyle->text("No ruleset under $pathToRuleset" . PHP_EOL); + return; + } + $this->ioStyle->text("Using ruleset under $pathToRuleset" . PHP_EOL); + $this->ruleSet = json_decode(file_get_contents($pathToRuleset), true); + } }