Skip to content

Commit 846f9c6

Browse files
authored
Merge pull request #697 from magento/MQE-2047
MQE-2047: Jenkins Pipeline - Static Check Options
2 parents 1de7456 + 6d40406 commit 846f9c6

File tree

2 files changed

+82
-19
lines changed

2 files changed

+82
-19
lines changed

docs/commands/mftf.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,12 @@ The example parameters are taken from the `etc/config/.env.example` file.
430430

431431
### `static-checks`
432432

433-
Runs all or specific MFTF static-checks on the test codebase that MFTF is currently attached to.
434-
If no script name argument is specified, all existing static check scripts will run.
433+
Runs all or specific MFTF static-checks on the test codebase that MFTF is currently attached to.
434+
Behavior for determining what tests to run is as follows:
435+
436+
* If test names are specified, only those tests are run.
437+
* If no test names are specified, tests are run according to `staticRuleset.json`.
438+
* If no `staticRuleset.json` is found, all tests are run.
435439

436440
#### Usage
437441

@@ -489,6 +493,25 @@ vendor/bin/mftf static-checks testDependencies actionGroupArguments
489493
|`actionGroupArguments` | Checks that action groups do not have unused arguments.|
490494
|`deprecatedEntityUsage`| Checks that deprecated test entities are not being referenced.|
491495
496+
#### Defining ruleset
497+
498+
The `static-checks` command will look for a `staticRuleset.json` file under either:
499+
500+
* `dev/tests/acceptance/staticRuleset.json`, if embedded with Magento2
501+
* `dev/staticRuleset.json`, if standalone
502+
503+
This file works as the default configuration to easily allow for the integration of `static-checks` in a CI environment.
504+
Currently, the ruleset only defines the tests to run. Here is an example of the expected format:
505+
506+
```json
507+
{
508+
"tests": [
509+
"actionGroupArguments",
510+
"anotherTest"
511+
]
512+
}
513+
```
514+
492515
### `upgrade:tests`
493516

494517
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.

src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
class StaticChecksCommand extends Command
2424
{
25+
/**
26+
* Associative array containing static ruleset properties.
27+
*
28+
* @var array
29+
*/
30+
private $ruleSet;
31+
2532
/**
2633
* Pool of all existing static check objects
2734
*
@@ -132,26 +139,15 @@ private function validateInput(InputInterface $input)
132139
{
133140
$this->staticCheckObjects = [];
134141
$requiredChecksNames = $input->getArgument('names');
135-
$invalidCheckNames = [];
136-
// Found user required static check script(s) to run,
137-
// If no static check name is supplied, run all static check scripts
142+
// Build list of static check names to run.
143+
if (empty($requiredChecksNames)) {
144+
$this->parseRulesetJson();
145+
$requiredChecksNames = $this->ruleSet['tests'] ?? null;
146+
}
138147
if (empty($requiredChecksNames)) {
139148
$this->staticCheckObjects = $this->allStaticCheckObjects;
140149
} else {
141-
for ($index = 0; $index < count($requiredChecksNames); $index++) {
142-
if (in_array($requiredChecksNames[$index], array_keys($this->allStaticCheckObjects))) {
143-
$this->staticCheckObjects[$requiredChecksNames[$index]] =
144-
$this->allStaticCheckObjects[$requiredChecksNames[$index]];
145-
} else {
146-
$invalidCheckNames[] = $requiredChecksNames[$index];
147-
}
148-
}
149-
}
150-
151-
if (!empty($invalidCheckNames)) {
152-
throw new InvalidArgumentException(
153-
'Invalid static check script(s): ' . implode(', ', $invalidCheckNames) . '.'
154-
);
150+
$this->validateTestNames($requiredChecksNames);
155151
}
156152

157153
if ($input->getOption('path')) {
@@ -164,4 +160,48 @@ private function validateInput(InputInterface $input)
164160
);
165161
}
166162
}
163+
164+
/**
165+
* Validates that all passed in static-check names match an existing static check
166+
* @param string[] $requiredChecksNames
167+
* @return void
168+
*/
169+
private function validateTestNames($requiredChecksNames)
170+
{
171+
$invalidCheckNames = [];
172+
for ($index = 0; $index < count($requiredChecksNames); $index++) {
173+
if (in_array($requiredChecksNames[$index], array_keys($this->allStaticCheckObjects))) {
174+
$this->staticCheckObjects[$requiredChecksNames[$index]] =
175+
$this->allStaticCheckObjects[$requiredChecksNames[$index]];
176+
} else {
177+
$invalidCheckNames[] = $requiredChecksNames[$index];
178+
}
179+
}
180+
181+
if (!empty($invalidCheckNames)) {
182+
throw new InvalidArgumentException(
183+
'Invalid static check script(s): ' . implode(', ', $invalidCheckNames) . '.'
184+
);
185+
}
186+
}
187+
188+
/**
189+
* Parses and sets local ruleSet. If not found, simply returns and lets script continue.
190+
* @return void;
191+
*/
192+
private function parseRulesetJson()
193+
{
194+
$pathAddition = "/dev/tests/acceptance/";
195+
// MFTF is both NOT attached and no MAGENTO_BP defined in .env
196+
if (MAGENTO_BP === FW_BP) {
197+
$pathAddition = "/dev/";
198+
}
199+
$pathToRuleset = MAGENTO_BP . $pathAddition . "staticRuleset.json";
200+
if (!file_exists($pathToRuleset)) {
201+
$this->ioStyle->text("No ruleset under $pathToRuleset" . PHP_EOL);
202+
return;
203+
}
204+
$this->ioStyle->text("Using ruleset under $pathToRuleset" . PHP_EOL);
205+
$this->ruleSet = json_decode(file_get_contents($pathToRuleset), true);
206+
}
167207
}

0 commit comments

Comments
 (0)