Skip to content

Commit

Permalink
GitDiffFile: consume configured directories (#1697)
Browse files Browse the repository at this point in the history
* GitDiffFile: consume configured directories

* CS Fix

* CS Fix 2

* Prefer native git listing over grep

* Lower overall MIN_MSI as requested
  • Loading branch information
Slamdunk committed Jun 9, 2022
1 parent 62f52df commit 11df4ea
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mt.yaml
Expand Up @@ -11,7 +11,7 @@ on:
- master

env:
MIN_MSI: 71.39
MIN_MSI: 71.35
MIN_COVERED_MSI: 86.78

jobs:
Expand Down
11 changes: 7 additions & 4 deletions src/Configuration/ConfigurationFactory.php
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/Logger/GitHub/GitDiffFileProvider.php
Expand Up @@ -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;
Expand All @@ -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 === '') {
Expand Down
12 changes: 6 additions & 6 deletions tests/phpunit/Logger/GitHub/GitDiffFileProviderTest.php
Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 11df4ea

Please sign in to comment.