Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added debug message section. #610

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Command/InfectionCommand.php
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/Logger/DebugFileLogger.php
Expand Up @@ -68,6 +68,18 @@ protected function getLogLines(): array
'Not Covered'
);

if (!empty($this->metricsCalculator->getDebugInfo())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File DebugFileLogger is used for end-to-end tests only. It's not supposed to be used for developers.
We have recently removed it from the documentation, but it hasn't been released yet.

I've added a comment to this class that it's for e2e tests only.

$logs[] = '';
$logs[] = 'Debug Messages:';
$logs[] = '===============';
$logs[] = '';

foreach ($this->metricsCalculator->getDebugInfo() as $line) {
$logs[] = '';
$logs[] = $line;
}
}

return $logs;
}

Expand Down
18 changes: 18 additions & 0 deletions src/Mutant/MetricsCalculator.php
Expand Up @@ -98,6 +98,11 @@ class MetricsCalculator
*/
private $totalMutantsCount = 0;

/**
* @var string[]
*/
private $debugMessage = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure MetricsCalculator should have debugMessage array, this looks a bit confusing.

Since the writing to log files is done through subscribers (see MutationTestingResultsLoggerSubscriber), I would suggest the following:

  1. Add a initial tests run process to the InitialTestSuiteStarted() event's constructor here:

$this->eventDispatcher->dispatch(new InitialTestSuiteStarted());

  1. Subscribe to this InitialTestSuiteStarted event in MutationTestingResultsLoggerSubscriber here

public function getSubscribedEvents(): array
{
return [
MutationTestingFinished::class => [$this, 'onMutationTestingFinished'],
];
}

  1. Extract the process (or command line string) from the InitialTestSuiteStarted event and use it in MutationTestingResultsLoggerSubscriber::onMutationTestingFinished() (which is executed later than InitialTestSuiteStarted)


/**
* Build a metric calculator with a sub-set of mutators
*
Expand Down Expand Up @@ -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;
}
}
23 changes: 22 additions & 1 deletion src/Process/Runner/InitialTestsRunner.php
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -73,6 +80,20 @@ 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();
Expand Down
9 changes: 8 additions & 1 deletion tests/Console/E2ETest.php
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Mutant/MetricsCalculatorTest.php
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/Process/Runner/InitialTestsRunnerTest.php
Expand Up @@ -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 Infection\Process\Runner\InitialTestsRunner;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -78,7 +79,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);
}
Expand Down