diff --git a/src/commands/Command.php b/src/commands/Command.php index 5b0fd9f..94a3d66 100644 --- a/src/commands/Command.php +++ b/src/commands/Command.php @@ -14,6 +14,7 @@ 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 Throwable; /** * Command to lauch tests from command line accross all seacms dependencies @@ -96,13 +97,36 @@ use Symfony\Component\Console\Output\OutputInterface; default: // default display help - $inputInternal = new ArrayInput([ + return $this->runApplicationSafe([ 'command' => 'help', 'command_name' => 'seacms-test' - ]); - (new Application())->run($inputInternal,$output); - return Command::SUCCESS; + ],$output); + } + } + + /** + * run safe new Application + * @param array $def + * @param OutputInterface $output + * @return int errorCode + */ + protected function runApplicationSafe(array $def, OutputInterface $output): int + { + $app = new Application(); + $app->setCatchExceptions(false); // force not catching exceptions + $app->setAutoExit(false); // force not exiting + try { + $code = $app->run(new ArrayInput($def),$output); + if ($code != 0 && $code != 2){ + $output->writeln("Commands {$def['command']} return code $code"); + return Command::FAILURE; + } + } catch (Throwable $th){ + $output->writeln("Something went wrong ({$th->getMessage()} - code :{$th->getCode()} )"); + $output->writeln("In file {$th->getFile()}(line {$th->getLine()})"); + return Command::FAILURE; } + return Command::SUCCESS; } /** @@ -132,7 +156,7 @@ use Symfony\Component\Console\Output\OutputInterface; return ($v == '--pversion') ? '--version' : $v; // hack to prevent bad caught },$args); } - (new Application())->run(new ArrayInput($def),$output); + return $this->runApplicationSafe($def,$output); } else { $output->writeln('phpunit not installed !'); }