Skip to content

MQE-1959: Static-checks command can be configured #556

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 2 commits into from
Jan 24, 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
29 changes: 25 additions & 4 deletions docs/commands/mftf.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,18 +463,39 @@ The example parameters are taken from the `etc/config/.env.example` file.

### `static-checks`

Runs all MFTF static-checks on the test codebase that MFTF is currently attached to.
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.

#### Existing static checks
#### Usage

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

#### Usage
#### Examples

To check what existing static check scripts are available

```bash
vendor/bin/mftf static-checks --help
```

To run all existing static check scripts

```bash
vendor/bin/mftf static-checks
```

To run specific static check scripts

```bash
vendor/bin/mftf static-checks testDependencies
```

#### Existing static checks

* Test Dependency: Checks that test dependencies do not violate Magento module's composer dependencies.

### `upgrade:tests`

Applies all the MFTF major version upgrade scripts to test components in the given path (`test.xml`, `data.xml`, etc).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@

namespace Magento\FunctionalTestingFramework\Console;

use Magento\FunctionalTestingFramework\StaticCheck\StaticCheckInterface;
use Magento\FunctionalTestingFramework\StaticCheck\StaticChecksList;
use Magento\FunctionalTestingFramework\StaticCheck\StaticCheckListInterface;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Exception;

class StaticChecksCommand extends Command
{
/**
* Pool of static check scripts to run
* Pool of all existing static check objects
*
* @var StaticCheckListInterface
* @var StaticCheckInterface[]
*/
private $staticChecksList;
private $allStaticCheckObjects;

/**
* Static checks to run
*
* @var StaticCheckInterface[]
*/
private $staticCheckObjects;

/**
* Configures the current command.
Expand All @@ -32,13 +41,22 @@ class StaticChecksCommand extends Command
*/
protected function configure()
{
$list = new StaticChecksList();
$this->allStaticCheckObjects = $list->getStaticChecks();
$staticCheckNames = implode(', ', array_keys($this->allStaticCheckObjects));
$description = "This command will run all static checks on xml test materials. "
. "Available static check scripts are:\n{$staticCheckNames}";
$this->setName('static-checks')
->setDescription('This command will run all static checks on xml test materials.');
$this->staticChecksList = new StaticChecksList();
->setDescription($description)
->addArgument(
'names',
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'name(s) of specific static check script(s) to run'
);
}

/**
*
* Run required static check scripts
*
* @param InputInterface $input
* @param OutputInterface $output
Expand All @@ -47,11 +65,23 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$staticCheckObjects = $this->staticChecksList->getStaticChecks();
try {
$this->validateInputArguments($input, $output);
} catch (InvalidArgumentException $e) {
LoggingUtil::getInstance()->getLogger(StaticChecksCommand::class)->error($e->getMessage());
$output->writeln($e->getMessage() . " Please fix input arguments and rerun.");
return 1;
}

$errors = [];
foreach ($this->staticCheckObjects as $name => $staticCheck) {
LoggingUtil::getInstance()->getLogger(get_class($staticCheck))->info(
"\nRunning static check script for: " . $name
);
$output->writeln(
"\nRunning static check script for: " . $name
);

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

$staticOutput = $staticCheck->getOutput();
Expand All @@ -66,4 +96,38 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}
}

/**
* Validate input arguments
*
* @param InputInterface $input
* @return void
* @throws InvalidArgumentException
*/
private function validateInputArguments(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
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) . "."
);
}
}
}