Skip to content

MQE-2047: Jenkins Pipeline - Static Check Options #697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions docs/commands/mftf.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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')) {
Expand All @@ -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);
}
}