From 33743faf92cf6f02e82732d8488d58f773f40ce6 Mon Sep 17 00:00:00 2001 From: Ji Lu Date: Thu, 30 Aug 2018 11:07:53 -0500 Subject: [PATCH] MQE-1009: Generated Suites Are Not Cleaned Up When Regenerating - bug fix --- .../Console/BaseGenerateCommand.php | 52 +++++++++++++++++++ .../Console/GenerateSuiteCommand.php | 12 ++++- .../Console/GenerateTestsCommand.php | 20 +++++-- .../Console/RunTestCommand.php | 18 +++++-- .../Console/RunTestGroupCommand.php | 17 ++++-- 5 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php diff --git a/src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php b/src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php new file mode 100644 index 000000000..d4044c74b --- /dev/null +++ b/src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php @@ -0,0 +1,52 @@ +addOption( + 'remove', + 'r', + InputOption::VALUE_NONE, + 'remove previous generated suites and tests' + ); + } + + /** + * Remove GENERATED_DIR if exists when running generate:tests. + * + * @param OutputInterface $output + * @param bool $verbose + * @return void + */ + protected function removeGeneratedDirectory(OutputInterface $output, bool $verbose) + { + $generatedDirectory = TESTS_MODULE_PATH . DIRECTORY_SEPARATOR . TestGenerator::GENERATED_DIR; + + if (file_exists($generatedDirectory)) { + DirSetupUtil::rmdirRecursive($generatedDirectory); + if ($verbose) { + $output->writeln("removed files and directory $generatedDirectory"); + } + } + } +} diff --git a/src/Magento/FunctionalTestingFramework/Console/GenerateSuiteCommand.php b/src/Magento/FunctionalTestingFramework/Console/GenerateSuiteCommand.php index ca9be1fac..815133e4d 100644 --- a/src/Magento/FunctionalTestingFramework/Console/GenerateSuiteCommand.php +++ b/src/Magento/FunctionalTestingFramework/Console/GenerateSuiteCommand.php @@ -8,12 +8,11 @@ namespace Magento\FunctionalTestingFramework\Console; use Magento\FunctionalTestingFramework\Suite\SuiteGenerator; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class GenerateSuiteCommand extends Command +class GenerateSuiteCommand extends BaseGenerateCommand { /** * Configures the current command. @@ -29,6 +28,8 @@ protected function configure() InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'argument which indicates suite names for generation (separated by space)' ); + + parent::configure(); } /** @@ -41,6 +42,13 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $remove = $input->getOption('remove'); + + // Remove previous GENERATED_DIR if --remove option is used + if ($remove) { + $this->removeGeneratedDirectory($output, $output->isVerbose()); + } + $suites = $input->getArgument('suites'); foreach ($suites as $suite) { diff --git a/src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php b/src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php index 845fb5151..fcd977060 100644 --- a/src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php +++ b/src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php @@ -14,13 +14,12 @@ use Magento\FunctionalTestingFramework\Util\Manifest\ParallelTestManifest; use Magento\FunctionalTestingFramework\Util\Manifest\TestManifestFactory; use Magento\FunctionalTestingFramework\Util\TestGenerator; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -class GenerateTestsCommand extends Command +class GenerateTestsCommand extends BaseGenerateCommand { /** * Configures the current command. @@ -52,7 +51,14 @@ protected function configure() 't', InputOption::VALUE_REQUIRED, 'A parameter accepting a JSON string used to determine the test configuration' - )->addOption('debug', 'd', InputOption::VALUE_NONE, 'run extra validation when generating tests'); + )->addOption( + 'debug', + 'd', + InputOption::VALUE_NONE, + 'run extra validation when generating tests' + ); + + parent::configure(); } /** @@ -73,6 +79,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $force = $input->getOption('force'); $time = $input->getOption('time') * 60 * 1000; // convert from minutes to milliseconds $debug = $input->getOption('debug'); + $remove = $input->getOption('remove'); + $verbose = $output->isVerbose(); if ($json !== null && !json_decode($json)) { @@ -85,6 +93,11 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new TestFrameworkException("time option cannot be less than or equal to 0"); } + // Remove previous GENERATED_DIR if --remove option is used + if ($remove) { + $this->removeGeneratedDirectory($output, $verbose || $debug); + } + $testConfiguration = $this->createTestConfiguration($json, $tests, $force, $debug, $verbose); // create our manifest file here @@ -153,7 +166,6 @@ private function createTestConfiguration($json, array $tests, bool $force, bool * * @param string $json * @param array $testConfiguration - * @throws TestFrameworkException * @return array */ private function parseTestsConfigJson($json, array $testConfiguration) diff --git a/src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php b/src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php index 9bf250ed0..097d7dfd8 100644 --- a/src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php +++ b/src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php @@ -7,16 +7,15 @@ namespace Magento\FunctionalTestingFramework\Console; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Debug\Debug; use Symfony\Component\Process\Process; +use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; -class RunTestCommand extends Command +class RunTestCommand extends BaseGenerateCommand { /** * Configures the current command. @@ -38,6 +37,8 @@ protected function configure() InputOption::VALUE_NONE, 'force generation of tests regardless of Magento Instance Configuration' ); + + parent::configure(); } /** @@ -55,6 +56,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $tests = $input->getArgument('name'); $skipGeneration = $input->getOption('skip-generate'); $force = $input->getOption('force'); + $remove = $input->getOption('remove'); + + if ($skipGeneration and $remove) { + // "skip-generate" and "remove" options cannot be used at the same time + throw new TestFrameworkException( + "\"skip-generate\" and \"remove\" options can not be used at the same time." + ); + } if (!$skipGeneration) { $command = $this->getApplication()->find('generate:tests'); @@ -63,7 +72,8 @@ protected function execute(InputInterface $input, OutputInterface $output) 'tests' => $tests, 'suites' => null ]), - '--force' => $force + '--force' => $force, + '--remove' => $remove ]; $command->run(new ArrayInput($args), $output); } diff --git a/src/Magento/FunctionalTestingFramework/Console/RunTestGroupCommand.php b/src/Magento/FunctionalTestingFramework/Console/RunTestGroupCommand.php index 110b86565..3098569fb 100644 --- a/src/Magento/FunctionalTestingFramework/Console/RunTestGroupCommand.php +++ b/src/Magento/FunctionalTestingFramework/Console/RunTestGroupCommand.php @@ -10,15 +10,15 @@ use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler; use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Process; +use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException; -class RunTestGroupCommand extends Command +class RunTestGroupCommand extends BaseGenerateCommand { /** * Configures the current command. @@ -44,6 +44,8 @@ protected function configure() InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'group names to be executed via codeception' ); + + parent::configure(); } /** @@ -61,6 +63,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $skipGeneration = $input->getOption('skip-generate'); $force = $input->getOption('force'); $groups = $input->getArgument('groups'); + $remove = $input->getOption('remove'); + + if ($skipGeneration and $remove) { + // "skip-generate" and "remove" options cannot be used at the same time + throw new TestFrameworkException( + "\"skip-generate\" and \"remove\" options can not be used at the same time." + ); + } // Create Mftf Configuration MftfApplicationConfig::create( @@ -75,7 +85,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $command = $this->getApplication()->find('generate:tests'); $args = [ '--tests' => $testConfiguration, - '--force' => $force + '--force' => $force, + '--remove' => $remove ]; $command->run(new ArrayInput($args), $output);