From 9bd0651bb39ca7db68fb2a175fdda0c8bb5cac7b Mon Sep 17 00:00:00 2001 From: Ji Lu Date: Wed, 22 Jan 2020 10:55:19 -0600 Subject: [PATCH] MQE-1959: Static-checks command can be configured --- docs/commands/mftf.md | 29 ++++++- .../Console/StaticChecksCommand.php | 82 +++++++++++++++++-- 2 files changed, 98 insertions(+), 13 deletions(-) diff --git a/docs/commands/mftf.md b/docs/commands/mftf.md index 642bbb1bd..cf1260b6a 100644 --- a/docs/commands/mftf.md +++ b/docs/commands/mftf.md @@ -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 []... +``` -#### 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). diff --git a/src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php b/src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php index 9757f494e..30c9cd600 100644 --- a/src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php +++ b/src/Magento/FunctionalTestingFramework/Console/StaticChecksCommand.php @@ -8,10 +8,12 @@ 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; @@ -19,11 +21,18 @@ 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. @@ -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 @@ -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(); @@ -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) . "." + ); + } + } }