From e9b7549a21ea8ed6d12e036e3b10aace9cdfae62 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Sat, 3 Jul 2021 17:08:12 +0300 Subject: [PATCH] Run only those test cases that cover mutated line 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 --- .../AbstractTestFrameworkAdapter.php | 11 ++++++---- .../CommandLineArgumentsAndOptionsBuilder.php | 6 +++++- .../ArgumentsAndOptionsBuilder.php | 20 ++++++++++++++++++- .../ArgumentsAndOptionsBuilderTest.php | 16 ++++++++++++--- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/TestFramework/AbstractTestFrameworkAdapter.php b/src/TestFramework/AbstractTestFrameworkAdapter.php index 9f8fa0336..bea7cb635 100644 --- a/src/TestFramework/AbstractTestFrameworkAdapter.php +++ b/src/TestFramework/AbstractTestFrameworkAdapter.php @@ -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, []); } /** @@ -116,7 +116,8 @@ public function getMutantCommandLine( $mutationOriginalFilePath ), $extraOptions, - [] + [], + $tests, ); } @@ -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, diff --git a/src/TestFramework/CommandLineArgumentsAndOptionsBuilder.php b/src/TestFramework/CommandLineArgumentsAndOptionsBuilder.php index d18ad2c93..bb0372fcd 100644 --- a/src/TestFramework/CommandLineArgumentsAndOptionsBuilder.php +++ b/src/TestFramework/CommandLineArgumentsAndOptionsBuilder.php @@ -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; } diff --git a/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php b/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php index c95c734aa..9659354fc 100644 --- a/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php +++ b/src/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilder.php @@ -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; @@ -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', @@ -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; } } diff --git a/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php b/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php index 45055b14d..a7440c9a4 100644 --- a/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php +++ b/tests/phpunit/TestFramework/PhpUnit/CommandLine/ArgumentsAndOptionsBuilderTest.php @@ -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; @@ -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, '', []) ); } @@ -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', []) ); } @@ -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'), + ] + ) ); } }