Skip to content

Commit

Permalink
Run only those test cases that cover mutated line
Browse files Browse the repository at this point in the history
Previously, we ran **files** that contains tests that cover mutated line. In `phpunit.xml` this is all we can do.

To run **test cases** that cover mutated line from that files, we can only use `--filter` options. This is what this change does
  • Loading branch information
maks-rafalko committed Jul 25, 2021
1 parent 0dde91a commit e9b7549
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/TestFramework/AbstractTestFrameworkAdapter.php
Expand Up @@ -91,7 +91,7 @@ public function getInitialTestRunCommandLine(
array $phpExtraArgs,
bool $skipCoverage
): array {
return $this->getCommandLine($this->buildInitialConfigFile(), $extraOptions, $phpExtraArgs);
return $this->getCommandLine($this->buildInitialConfigFile(), $extraOptions, $phpExtraArgs, []);
}

/**
Expand All @@ -116,7 +116,8 @@ public function getMutantCommandLine(
$mutationOriginalFilePath
),
$extraOptions,
[]
[],
$tests,
);
}

Expand Down Expand Up @@ -154,15 +155,17 @@ protected function buildMutationConfigFile(

/**
* @param string[] $phpExtraArgs
* @param TestLocation[] $tests
*
* @return string[]
*/
private function getCommandLine(
string $configPath,
string $extraOptions,
array $phpExtraArgs
array $phpExtraArgs,
array $tests
): array {
$frameworkArgs = $this->argumentsAndOptionsBuilder->build($configPath, $extraOptions);
$frameworkArgs = $this->argumentsAndOptionsBuilder->build($configPath, $extraOptions, $tests);

return $this->commandLineBuilder->build(
$this->testFrameworkExecutable,
Expand Down
6 changes: 5 additions & 1 deletion src/TestFramework/CommandLineArgumentsAndOptionsBuilder.php
Expand Up @@ -35,13 +35,17 @@

namespace Infection\TestFramework;

use Infection\AbstractTestFramework\Coverage\TestLocation;

/**
* @internal
*/
interface CommandLineArgumentsAndOptionsBuilder
{
/**
* @param TestLocation[] $tests
*
* @return string[]
*/
public function build(string $configPath, string $extraOptions): array;
public function build(string $configPath, string $extraOptions, array $tests): array;
}
Expand Up @@ -37,7 +37,12 @@

use function array_map;
use function array_merge;
use function array_unique;
use function count;
use function escapeshellcmd;
use function explode;
use function implode;
use Infection\AbstractTestFramework\Coverage\TestLocation;
use Infection\TestFramework\CommandLineArgumentsAndOptionsBuilder;
use function ltrim;

Expand All @@ -46,7 +51,8 @@
*/
final class ArgumentsAndOptionsBuilder implements CommandLineArgumentsAndOptionsBuilder
{
public function build(string $configPath, string $extraOptions): array
// todo build & buildForMutant
public function build(string $configPath, string $extraOptions, array $tests): array
{
$options = [
'--configuration',
Expand All @@ -62,6 +68,18 @@ public function build(string $configPath, string $extraOptions): array
);
}

// preg_replace('/\swith data set (.*)/', '', $test->getMethod())

if (count($tests) > 0) {
$escapedTests = array_map(
static fn (TestLocation $testLocation): string => escapeshellcmd($testLocation->getMethod()),
$tests
);

$options[] = '--filter';
$options[] = implode('|', array_unique($escapedTests));
}

return $options;
}
}
Expand Up @@ -35,6 +35,7 @@

namespace Infection\Tests\TestFramework\PhpUnit\CommandLine;

use Infection\AbstractTestFramework\Coverage\TestLocation;
use Infection\TestFramework\PhpUnit\CommandLine\ArgumentsAndOptionsBuilder;
use PHPUnit\Framework\TestCase;

Expand All @@ -59,7 +60,7 @@ public function test_it_can_build_the_command_without_extra_options(): void
'--configuration',
$configPath,
],
$this->builder->build($configPath, '')
$this->builder->build($configPath, '', [])
);
}

Expand All @@ -74,7 +75,7 @@ public function test_it_can_build_the_command_with_extra_options(): void
'--verbose',
'--debug',
],
$this->builder->build($configPath, '--verbose --debug')
$this->builder->build($configPath, '--verbose --debug', [])
);
}

Expand All @@ -87,8 +88,17 @@ public function test_it_can_build_the_command_with_extra_options_that_contains_s
'--configuration',
$configPath,
'--path=/a path/with spaces',
'--filter',
'App\\\\Test::test_case1|App\\\\Test::test_case2',
],
$this->builder->build($configPath, '--path=/a path/with spaces')
$this->builder->build(
$configPath,
'--path=/a path/with spaces',
[
TestLocation::forTestMethod('App\Test::test_case1'),
TestLocation::forTestMethod('App\Test::test_case2'),
]
)
);
}
}

0 comments on commit e9b7549

Please sign in to comment.