From c30b92c9f3a99d5cae8d86a958926c84cb2bc7c0 Mon Sep 17 00:00:00 2001 From: siad007 Date: Sun, 13 Jan 2019 02:34:04 +0100 Subject: [PATCH 1/4] Added debug message section. --- src/Command/InfectionCommand.php | 2 +- src/Logger/DebugFileLogger.php | 8 +++++++ src/Mutant/MetricsCalculator.php | 18 +++++++++++++++ src/Process/Runner/InitialTestsRunner.php | 22 ++++++++++++++++++- tests/Mutant/MetricsCalculatorTest.php | 2 ++ .../Process/Runner/InitialTestsRunnerTest.php | 7 +++++- 6 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Command/InfectionCommand.php b/src/Command/InfectionCommand.php index 95f9a7e31..d6312a22a 100644 --- a/src/Command/InfectionCommand.php +++ b/src/Command/InfectionCommand.php @@ -219,7 +219,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $processBuilder = new ProcessBuilder($adapter, $config->getProcessTimeout()); $testFrameworkOptions = $this->getTestFrameworkExtraOptions($testFrameworkKey); - $initialTestsRunner = new InitialTestsRunner($processBuilder, $this->eventDispatcher); + $initialTestsRunner = new InitialTestsRunner($processBuilder, $this->eventDispatcher, $metricsCalculator); $initialTestSuitProcess = $initialTestsRunner->run( $testFrameworkOptions->getForInitialProcess(), $this->skipCoverage, diff --git a/src/Logger/DebugFileLogger.php b/src/Logger/DebugFileLogger.php index 83320cdec..7b1f5c71a 100644 --- a/src/Logger/DebugFileLogger.php +++ b/src/Logger/DebugFileLogger.php @@ -67,6 +67,14 @@ protected function getLogLines(): array $this->metricsCalculator->getNotCoveredMutantProcesses(), 'Not Covered' ); + $logs[] = ''; + $logs[] = 'Debug Messages:'; + $logs[] = '==============='; + $logs[] = ''; + foreach ($this->metricsCalculator->getDebugInfo() as $line) { + $logs[] = ''; + $logs[] = $line; + } return $logs; } diff --git a/src/Mutant/MetricsCalculator.php b/src/Mutant/MetricsCalculator.php index d4200875e..b5e322a73 100644 --- a/src/Mutant/MetricsCalculator.php +++ b/src/Mutant/MetricsCalculator.php @@ -98,6 +98,11 @@ class MetricsCalculator */ private $totalMutantsCount = 0; + /** + * @var string[] + */ + private $debugMessage = []; + /** * Build a metric calculator with a sub-set of mutators * @@ -274,4 +279,17 @@ public function getErrorProcesses(): array { return $this->errorProcesses; } + + public function addDebugInfo(string $msg): void + { + $this->debugMessage[] = $msg; + } + + /** + * @return string[] + */ + public function getDebugInfo(): array + { + return $this->debugMessage; + } } diff --git a/src/Process/Runner/InitialTestsRunner.php b/src/Process/Runner/InitialTestsRunner.php index 2108cc00f..60a290eeb 100644 --- a/src/Process/Runner/InitialTestsRunner.php +++ b/src/Process/Runner/InitialTestsRunner.php @@ -39,6 +39,7 @@ use Infection\Events\InitialTestCaseCompleted; use Infection\Events\InitialTestSuiteFinished; use Infection\Events\InitialTestSuiteStarted; +use Infection\Mutant\MetricsCalculator; use Infection\Process\Builder\ProcessBuilder; use Symfony\Component\Process\Process; @@ -57,10 +58,16 @@ final class InitialTestsRunner */ private $eventDispatcher; - public function __construct(ProcessBuilder $processBuilder, EventDispatcherInterface $eventDispatcher) + /** + * @var MetricsCalculator + */ + private $metrics; + + public function __construct(ProcessBuilder $processBuilder, EventDispatcherInterface $eventDispatcher, MetricsCalculator $metrics) { $this->processBuilder = $processBuilder; $this->eventDispatcher = $eventDispatcher; + $this->metrics = $metrics; } public function run(string $testFrameworkExtraOptions, bool $skipCoverage, array $phpExtraOptions = []): Process @@ -73,6 +80,19 @@ public function run(string $testFrameworkExtraOptions, bool $skipCoverage, array $this->eventDispatcher->dispatch(new InitialTestSuiteStarted()); + $cmd = $process->getCommandLine(); + if (!empty($cmd)) { + $this->metrics->addDebugInfo( + sprintf( + "[%s()] Command:%s%s%s", + __METHOD__, + PHP_EOL, + $cmd, + PHP_EOL + ) + ); + } + $process->run(function ($type) use ($process): void { if ($process::ERR === $type) { $process->stop(); diff --git a/tests/Mutant/MetricsCalculatorTest.php b/tests/Mutant/MetricsCalculatorTest.php index f0b240554..9d87f689e 100644 --- a/tests/Mutant/MetricsCalculatorTest.php +++ b/tests/Mutant/MetricsCalculatorTest.php @@ -65,6 +65,8 @@ public function test_it_shows_zero_values_by_default(): void $this->assertSame(0.0, $calculator->getMutationScoreIndicator()); $this->assertSame(0.0, $calculator->getCoverageRate()); $this->assertSame(0.0, $calculator->getCoveredCodeMutationScoreIndicator()); + + $this->assertSame([], $calculator->getDebugInfo()); } public function test_it_collects_all_values(): void diff --git a/tests/Process/Runner/InitialTestsRunnerTest.php b/tests/Process/Runner/InitialTestsRunnerTest.php index df13bd166..f3e210bdc 100644 --- a/tests/Process/Runner/InitialTestsRunnerTest.php +++ b/tests/Process/Runner/InitialTestsRunnerTest.php @@ -35,10 +35,12 @@ namespace Infection\Tests\Process\Runner; +use Infection\Console\ConsoleOutputInterface; use Infection\EventDispatcher\EventDispatcherInterface; use Infection\Events\InitialTestCaseCompleted; use Infection\Events\InitialTestSuiteFinished; use Infection\Events\InitialTestSuiteStarted; +use Infection\Mutant\MetricsCalculator; use Infection\Process\Builder\ProcessBuilder; use Infection\Process\Runner\InitialTestsRunner; use PHPUnit\Framework\MockObject\MockObject; @@ -78,7 +80,10 @@ public function test_it_dispatches_events(): void $this->isInstanceOf(InitialTestSuiteFinished::class) ); - $testRunner = new InitialTestsRunner($processBuilder, $eventDispatcher); + $metrics = $this->getMockBuilder(MetricsCalculator::class)->getMock(); + $metrics->method('getDebugInfo')->willReturn(['test']); + + $testRunner = new InitialTestsRunner($processBuilder, $eventDispatcher, $metrics); $testRunner->run('', false); } From 3566d4d351e11820557626a45c3d47689731b217 Mon Sep 17 00:00:00 2001 From: siad007 Date: Sun, 13 Jan 2019 02:46:41 +0100 Subject: [PATCH 2/4] Fixed ci --- src/Logger/DebugFileLogger.php | 1 + src/Process/Runner/InitialTestsRunner.php | 3 ++- tests/Process/Runner/InitialTestsRunnerTest.php | 1 - 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Logger/DebugFileLogger.php b/src/Logger/DebugFileLogger.php index 7b1f5c71a..13edbc338 100644 --- a/src/Logger/DebugFileLogger.php +++ b/src/Logger/DebugFileLogger.php @@ -71,6 +71,7 @@ protected function getLogLines(): array $logs[] = 'Debug Messages:'; $logs[] = '==============='; $logs[] = ''; + foreach ($this->metricsCalculator->getDebugInfo() as $line) { $logs[] = ''; $logs[] = $line; diff --git a/src/Process/Runner/InitialTestsRunner.php b/src/Process/Runner/InitialTestsRunner.php index 60a290eeb..db105c4ca 100644 --- a/src/Process/Runner/InitialTestsRunner.php +++ b/src/Process/Runner/InitialTestsRunner.php @@ -81,10 +81,11 @@ public function run(string $testFrameworkExtraOptions, bool $skipCoverage, array $this->eventDispatcher->dispatch(new InitialTestSuiteStarted()); $cmd = $process->getCommandLine(); + if (!empty($cmd)) { $this->metrics->addDebugInfo( sprintf( - "[%s()] Command:%s%s%s", + '[%s()] Command:%s%s%s', __METHOD__, PHP_EOL, $cmd, diff --git a/tests/Process/Runner/InitialTestsRunnerTest.php b/tests/Process/Runner/InitialTestsRunnerTest.php index f3e210bdc..1c6ad6947 100644 --- a/tests/Process/Runner/InitialTestsRunnerTest.php +++ b/tests/Process/Runner/InitialTestsRunnerTest.php @@ -35,7 +35,6 @@ namespace Infection\Tests\Process\Runner; -use Infection\Console\ConsoleOutputInterface; use Infection\EventDispatcher\EventDispatcherInterface; use Infection\Events\InitialTestCaseCompleted; use Infection\Events\InitialTestSuiteFinished; From 4597e3441ff9a7c434394f61ece9ebb615de4ecc Mon Sep 17 00:00:00 2001 From: siad007 Date: Sun, 13 Jan 2019 03:06:56 +0100 Subject: [PATCH 3/4] Simplified assert --- tests/Console/E2ETest.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/Console/E2ETest.php b/tests/Console/E2ETest.php index 039c36d89..762e1f245 100644 --- a/tests/Console/E2ETest.php +++ b/tests/Console/E2ETest.php @@ -172,7 +172,14 @@ private function runOnE2EFixture($path): string $this->markTestSkipped('Saved golden output'); } - $this->assertFileEquals('expected-output.txt', 'infection.log', sprintf('%s/expected-output.txt is not same as infection.log (if that is OK, run GOLDEN=1 vendor/bin/phpunit)', getcwd())); + $this->assertStringStartsWith( + file_get_contents('expected-output.txt'), + file_get_contents('infection.log'), + sprintf( + '%s/expected-output.txt is not same as infection.log (if that is OK, run GOLDEN=1 vendor/bin/phpunit)', + getcwd() + ) + ); return $output; } From f43690cc26dd245164d980a49400a072ede279cf Mon Sep 17 00:00:00 2001 From: siad007 Date: Sun, 13 Jan 2019 03:29:12 +0100 Subject: [PATCH 4/4] Added condition --- src/Logger/DebugFileLogger.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Logger/DebugFileLogger.php b/src/Logger/DebugFileLogger.php index 13edbc338..c39e2561c 100644 --- a/src/Logger/DebugFileLogger.php +++ b/src/Logger/DebugFileLogger.php @@ -67,14 +67,17 @@ protected function getLogLines(): array $this->metricsCalculator->getNotCoveredMutantProcesses(), 'Not Covered' ); - $logs[] = ''; - $logs[] = 'Debug Messages:'; - $logs[] = '==============='; - $logs[] = ''; - foreach ($this->metricsCalculator->getDebugInfo() as $line) { + if (!empty($this->metricsCalculator->getDebugInfo())) { $logs[] = ''; - $logs[] = $line; + $logs[] = 'Debug Messages:'; + $logs[] = '==============='; + $logs[] = ''; + + foreach ($this->metricsCalculator->getDebugInfo() as $line) { + $logs[] = ''; + $logs[] = $line; + } } return $logs;