diff --git a/.github/workflows/mt.yaml b/.github/workflows/mt.yaml index a3c1726ff..a5d988456 100644 --- a/.github/workflows/mt.yaml +++ b/.github/workflows/mt.yaml @@ -11,7 +11,7 @@ on: - master env: - MIN_MSI: 71.39 + MIN_MSI: 71.35 MIN_COVERED_MSI: 86.78 jobs: diff --git a/src/Configuration/ConfigurationFactory.php b/src/Configuration/ConfigurationFactory.php index 71cdf62c8..57e59058d 100644 --- a/src/Configuration/ConfigurationFactory.php +++ b/src/Configuration/ConfigurationFactory.php @@ -151,7 +151,7 @@ public function create( $schema->getSource()->getDirectories(), $schema->getSource()->getExcludes() ), - $this->retrieveFilter($filter, $gitDiffFilter, $isForGitDiffLines, $gitDiffBase), + $this->retrieveFilter($filter, $gitDiffFilter, $isForGitDiffLines, $gitDiffBase, $schema->getSource()->getDirectories()), $schema->getSource()->getExcludes(), $this->retrieveLogs($schema->getLogs(), $useGitHubLogger, $htmlLogFilePath), $logVerbosity, @@ -303,7 +303,10 @@ private function retrieveIgnoreSourceCodeMutatorsMap(array $resolvedMutatorsMap) return $map; } - private function retrieveFilter(string $filter, ?string $gitDiffFilter, bool $isForGitDiffLines, ?string $gitDiffBase): string + /** + * @param string[] $sourceDirectories + */ + private function retrieveFilter(string $filter, ?string $gitDiffFilter, bool $isForGitDiffLines, ?string $gitDiffBase, array $sourceDirectories): string { if ($gitDiffFilter === null && !$isForGitDiffLines) { return $filter; @@ -312,10 +315,10 @@ private function retrieveFilter(string $filter, ?string $gitDiffFilter, bool $is $baseBranch = $gitDiffBase ?? GitDiffFileProvider::DEFAULT_BASE; if ($isForGitDiffLines) { - return $this->gitDiffFileProvider->provide('AM', $baseBranch); + return $this->gitDiffFileProvider->provide('AM', $baseBranch, $sourceDirectories); } - return $this->gitDiffFileProvider->provide($gitDiffFilter, $baseBranch); + return $this->gitDiffFileProvider->provide($gitDiffFilter, $baseBranch, $sourceDirectories); } private function retrieveLogs(Logs $logs, ?bool $useGitHubLogger, ?string $htmlLogFilePath): Logs diff --git a/src/Logger/GitHub/GitDiffFileProvider.php b/src/Logger/GitHub/GitDiffFileProvider.php index 608e0d961..ab8a92aac 100644 --- a/src/Logger/GitHub/GitDiffFileProvider.php +++ b/src/Logger/GitHub/GitDiffFileProvider.php @@ -35,7 +35,9 @@ namespace Infection\Logger\GitHub; +use function array_map; use function escapeshellarg; +use function implode; use Infection\Process\ShellCommandLineExecutor; use function Safe\sprintf; use Symfony\Component\Process\Exception\ProcessFailedException; @@ -54,14 +56,18 @@ public function __construct(private ShellCommandLineExecutor $shellCommandLineEx { } - public function provide(string $gitDiffFilter, string $gitDiffBase): string + /** + * @param string[] $sourceDirectories + */ + public function provide(string $gitDiffFilter, string $gitDiffBase, array $sourceDirectories): string { $referenceCommit = $this->findReferenceCommit($gitDiffBase); $filter = $this->shellCommandLineExecutor->execute(sprintf( - 'git diff %s --diff-filter=%s --name-only | grep src/ | paste -s -d "," -', + 'git diff %s --diff-filter=%s --name-only -- %s | paste -s -d "," -', escapeshellarg($referenceCommit), - escapeshellarg($gitDiffFilter) + escapeshellarg($gitDiffFilter), + implode(' ', array_map('\\escapeshellarg', $sourceDirectories)) )); if ($filter === '') { diff --git a/tests/phpunit/Logger/GitHub/GitDiffFileProviderTest.php b/tests/phpunit/Logger/GitHub/GitDiffFileProviderTest.php index e993e3ee7..3e7890fe4 100644 --- a/tests/phpunit/Logger/GitHub/GitDiffFileProviderTest.php +++ b/tests/phpunit/Logger/GitHub/GitDiffFileProviderTest.php @@ -53,17 +53,17 @@ public function test_it_throws_no_code_to_mutate_exception_when_diff_is_empty(): $this->expectException(NoFilesInDiffToMutate::class); $diffProvider = new GitDiffFileProvider($shellCommandLineExecutor); - $diffProvider->provide('AM', 'master'); + $diffProvider->provide('AM', 'master', ['src/']); } public function test_it_executes_diff_and_returns_filter_as_a_string(): void { $expectedMergeBaseCommandLine = 'git merge-base \'master\' HEAD'; - $expectedDiffCommandLine = 'git diff \'0ABCMERGE_BASE_342\' --diff-filter=\'AM\' --name-only | grep src/ | paste -s -d "," -'; + $expectedDiffCommandLine = 'git diff \'0ABCMERGE_BASE_342\' --diff-filter=\'AM\' --name-only -- \'app/\' \'my lib/\' | paste -s -d "," -'; if (PHP_OS_FAMILY === 'Windows') { $expectedMergeBaseCommandLine = 'git merge-base "master" HEAD'; - $expectedDiffCommandLine = 'git diff "0ABCMERGE_BASE_342" --diff-filter="AM" --name-only | grep src/ | paste -s -d "," -'; + $expectedDiffCommandLine = 'git diff "0ABCMERGE_BASE_342" --diff-filter="AM" --name-only -- "app/" "my lib/" | paste -s -d "," -'; } $shellCommandLineExecutor = $this->createMock(ShellCommandLineExecutor::class); @@ -75,16 +75,16 @@ public function test_it_executes_diff_and_returns_filter_as_a_string(): void case $expectedMergeBaseCommandLine: return "0ABCMERGE_BASE_342\n"; case $expectedDiffCommandLine: - return 'src/A.php,src/B.php'; + return 'app/A.php,my lib/B.php'; default: $this->fail("Unexpected shell command: $command"); } }); $diffProvider = new GitDiffFileProvider($shellCommandLineExecutor); - $filter = $diffProvider->provide('AM', 'master'); + $filter = $diffProvider->provide('AM', 'master', ['app/', 'my lib/']); - $this->assertSame('src/A.php,src/B.php', $filter); + $this->assertSame('app/A.php,my lib/B.php', $filter); } public function test_it_provides_lines_filter_as_a_string(): void