From 5d4edb61a12dbc3910ab82726ab09d5e047d9880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fede=20=C3=81lvarez?= Date: Tue, 20 Sep 2016 11:34:36 +0200 Subject: [PATCH] Add new command: test launcher. Very specific, useful only with Bitban's framework --- README.md | 12 ++++ src/Command/Test/RunCommand.php | 110 ++++++++++++++++++++++++++++++++ src/Console/Application.php | 7 +- 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 src/Command/Test/RunCommand.php diff --git a/README.md b/README.md index 7c1fa2b..1b9b992 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,18 @@ Shows debug information about: This command, may be unavailable in any future versions of these tools, as it has been added for debug purposes. +## Run Bitban Lightweight tests + +Runs Bitban's framework lightweight tests. It assumes that Bitban's Development VM is up and running, and it has test launcher script `run_light_tests.sh` installed. + +`bin/php-cqtools test:run --vmHost[=VMHOST] [setName(self::COMMAND_NAME) + ->setDescription(self::COMMAND_DESCRIPTION) + ->setHelp(self::COMMAND_HELP) + ->addOption(self::OPTION_VM_HOST, null, InputOption::VALUE_OPTIONAL, 'Host name of development VM', 'ndevel'); + } + + /** + * @param string $vmHost + * @return bool + */ + private function checkVmConnection($vmHost) + { + if (!$socket = @fsockopen($vmHost, 22)) { + return false; + } + fclose($socket); + return true; + } + + /** + * @param Process $process + * @param OutputInterface $output + */ + private function runTests($process, $output) + { + if ($output->isDebug()) { + $process->run(function ($type, $buffer) use ($output) { + if (Process::ERR === $type) { + $output->write('' . $buffer . ''); + } else { + $output->write($buffer); + } + }); + } else { + $process->run(); + } + } + + /** + * @param Process $process + * @param OutputInterface $output + * @return bool + */ + private function outputTestsResult($process, $output) + { + $processExitCode = $process->getExitCode(); + if ($processExitCode > 0) { + if (strpos($process->getErrorOutput(), 'bash') !== false) { + $output->writeln('run_light_tests.sh script not found, could not perform any test'); + } else { + $output->writeln($process->getOutput()); + $output->writeln('Tests FAILED'); + } + } else { + $output->writeln('Tests OK'); + } + + return $processExitCode === 0; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $vmHost = $input->getOption(self::OPTION_VM_HOST); + if (!$this->checkVmConnection($vmHost)) { + $output->writeln("VM host '$vmHost' is down, could not perform any test"); + return false; + } + + $project = basename($this->projectBasepath); + + $output->writeln("Running lightweight tests for project '$project', please be patient..."); + + $commandLine = <<setTimeout(3600); + + $this->runTests($process, $output); + $testsResult = $this->outputTestsResult($process, $output); + return $testsResult; + } +} diff --git a/src/Console/Application.php b/src/Console/Application.php index 4945e8d..60de946 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -16,12 +16,13 @@ use Bitban\PhpCodeQualityTools\Command\GitHooks\PostCheckoutCommand; use Bitban\PhpCodeQualityTools\Command\GitHooks\PostMergeCommand; use Bitban\PhpCodeQualityTools\Command\GitHooks\UninstallCommand; +use Bitban\PhpCodeQualityTools\Command\Test\RunCommand; use Symfony\Component\Console\Application as BaseApplication; class Application extends BaseApplication { const APP_NAME = 'Bitban Technologies PHP Code Quality Tools'; - const APP_VERSION = '0.9.10'; + const APP_VERSION = '0.9.11'; public function __construct() { @@ -38,7 +39,9 @@ public function __construct() new ValidateCommand(), new CustomFixCommand(), - new ShowValuesCommand() + new ShowValuesCommand(), + + new RunCommand() ]); } }