Skip to content

Commit

Permalink
Stop Infection execution with 0 exit code when git diff filter retu…
Browse files Browse the repository at this point in the history
…rns empty result (#1600)

* Use Symfony's `Process::fromShellCommandLin()` to execute a command

* Stop Infection execution with `0` exit code when git diff filter returns empty result (no files to be mutated)

* Stop Infection execution with `0` exit code when git diff filter returns empty result (no files to be mutated)

Fixes #1599

This will improve the speed of CI builds and immediately stop Infection execution.

Example: if `README.md` is updated on the root of the folder, we don't want/need to run Infection against the whole project.

* Depending on OS, check command line differently

https://www.php.net/manual/en/function.escapeshellarg.php

> On Windows, escapeshellarg() instead replaces percent signs, exclamation marks (delayed variable substitution) and double quotes with spaces and adds double quotes around the string.
  • Loading branch information
maks-rafalko committed Oct 28, 2021
1 parent 84140e6 commit a7ece6f
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 29 deletions.
39 changes: 22 additions & 17 deletions src/Command/RunCommand.php
Expand Up @@ -53,6 +53,7 @@
use Infection\FileSystem\Locator\FileOrDirectoryNotFound;
use Infection\FileSystem\Locator\Locator;
use Infection\Logger\ConsoleLogger;
use Infection\Logger\GitHub\NoFilesInDiffToMutate;
use Infection\Metrics\MinMsiCheckFailed;
use Infection\Process\Runner\InitialTestsFailed;
use Infection\TestFramework\TestFrameworkTypes;
Expand Down Expand Up @@ -329,26 +330,30 @@ protected function executeCommand(IO $io): bool
$container = $this->createContainer($io, $logger);
$consoleOutput = new ConsoleOutput($logger);

$this->startUp($container, $consoleOutput, $logger, $io);

$engine = new Engine(
$container->getConfiguration(),
$container->getTestFrameworkAdapter(),
$container->getCoverageChecker(),
$container->getEventDispatcher(),
$container->getInitialTestsRunner(),
$container->getMemoryLimiter(),
$container->getMutationGenerator(),
$container->getMutationTestingRunner(),
$container->getMinMsiChecker(),
$consoleOutput,
$container->getMetricsCalculator(),
$container->getTestFrameworkExtraOptionsFilter()
);

try {
$this->startUp($container, $consoleOutput, $logger, $io);

$engine = new Engine(
$container->getConfiguration(),
$container->getTestFrameworkAdapter(),
$container->getCoverageChecker(),
$container->getEventDispatcher(),
$container->getInitialTestsRunner(),
$container->getMemoryLimiter(),
$container->getMutationGenerator(),
$container->getMutationTestingRunner(),
$container->getMinMsiChecker(),
$consoleOutput,
$container->getMetricsCalculator(),
$container->getTestFrameworkExtraOptionsFilter()
);

$engine->execute();

return true;
} catch (NoFilesInDiffToMutate $e) {
$io->success($e->getMessage());

return true;
} catch (InitialTestsFailed | MinMsiCheckFailed $exception) {
// TODO: we can move that in a dedicated logger later and handle those cases in the
Expand Down
13 changes: 11 additions & 2 deletions src/Container.php
Expand Up @@ -104,6 +104,7 @@
use Infection\Process\Runner\MutationTestingRunner;
use Infection\Process\Runner\ParallelProcessRunner;
use Infection\Process\Runner\ProcessRunner;
use Infection\Process\ShellCommandLineExecutor;
use Infection\Resource\Memory\MemoryFormatter;
use Infection\Resource\Memory\MemoryLimiter;
use Infection\Resource\Memory\MemoryLimiterEnvironment;
Expand Down Expand Up @@ -654,8 +655,11 @@ public static function create(): self
DiffSourceCodeMatcher::class => static function (): DiffSourceCodeMatcher {
return new DiffSourceCodeMatcher();
},
GitDiffFileProvider::class => static function (): GitDiffFileProvider {
return new GitDiffFileProvider();
ShellCommandLineExecutor::class => static function (): ShellCommandLineExecutor {
return new ShellCommandLineExecutor();
},
GitDiffFileProvider::class => static function (self $container): GitDiffFileProvider {
return new GitDiffFileProvider($container->getShellCommandLineExecutor());
},
]);

Expand Down Expand Up @@ -1256,6 +1260,11 @@ public function getDiffSourceCodeMatcher(): DiffSourceCodeMatcher
return $this->get(DiffSourceCodeMatcher::class);
}

public function getShellCommandLineExecutor(): ShellCommandLineExecutor
{
return $this->get(ShellCommandLineExecutor::class);
}

public function getGitDiffFileProvider(): GitDiffFileProvider
{
return $this->get(GitDiffFileProvider::class);
Expand Down
27 changes: 19 additions & 8 deletions src/Logger/GitHub/GitDiffFileProvider.php
Expand Up @@ -36,8 +36,8 @@
namespace Infection\Logger\GitHub;

use function escapeshellarg;
use Infection\Process\ShellCommandLineExecutor;
use function Safe\sprintf;
use function shell_exec;

/**
* @final
Expand All @@ -48,14 +48,25 @@ class GitDiffFileProvider
{
public const DEFAULT_BASE = 'origin/master';

private ShellCommandLineExecutor $shellCommandLineExecutor;

public function __construct(ShellCommandLineExecutor $shellCommandLineExecutor)
{
$this->shellCommandLineExecutor = $shellCommandLineExecutor;
}

public function provide(string $gitDiffFilter, string $gitDiffBase): string
{
return (string) shell_exec(
sprintf(
'git diff %s --diff-filter=%s --name-only | grep src/ | paste -s -d "," -',
escapeshellarg($gitDiffBase),
escapeshellarg($gitDiffFilter)
)
);
$filter = $this->shellCommandLineExecutor->execute(sprintf(
'git diff %s --diff-filter=%s --name-only | grep src/ | paste -s -d "," -',
escapeshellarg($gitDiffBase),
escapeshellarg($gitDiffFilter)
));

if ($filter === '') {
throw NoFilesInDiffToMutate::create();
}

return $filter;
}
}
49 changes: 49 additions & 0 deletions src/Logger/GitHub/NoFilesInDiffToMutate.php
@@ -0,0 +1,49 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Logger\GitHub;

use Exception;

/**
* @internal
*/
final class NoFilesInDiffToMutate extends Exception
{
public static function create(): self
{
return new self('No files in diff found, skipping mutation analysis.');
}
}
52 changes: 52 additions & 0 deletions src/Process/ShellCommandLineExecutor.php
@@ -0,0 +1,52 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Process;

use Symfony\Component\Process\Process;
use function trim;

/**
* @internal
*
* @final
*/
class ShellCommandLineExecutor
{
public function execute(string $commandLine): string
{
return trim(Process::fromShellCommandline($commandLine)->mustRun()->getOutput());
}
}
4 changes: 2 additions & 2 deletions tests/phpunit/AutoReview/ProjectCode/ProjectCodeProvider.php
Expand Up @@ -58,7 +58,6 @@
use Infection\FileSystem\Finder\ComposerExecutableFinder;
use Infection\FileSystem\Finder\NonExecutableFinder;
use Infection\FileSystem\Finder\TestFrameworkFinder;
use Infection\Logger\GitHub\GitDiffFileProvider;
use Infection\Logger\Http\StrykerCurlClient;
use Infection\Logger\Http\StrykerDashboardClient;
use Infection\Metrics\MetricsCalculator;
Expand All @@ -67,6 +66,7 @@
use Infection\Mutator\NodeMutationGenerator;
use Infection\Process\OriginalPhpProcess;
use Infection\Process\Runner\IndexedProcessBearer;
use Infection\Process\ShellCommandLineExecutor;
use Infection\TestFramework\AdapterInstaller;
use Infection\TestFramework\Coverage\JUnit\TestFileTimeData;
use Infection\TestFramework\Coverage\NodeLineRangeData;
Expand Down Expand Up @@ -114,7 +114,7 @@ final class ProjectCodeProvider
XdebugHandler::class,
NullSubscriber::class,
FormatterName::class,
GitDiffFileProvider::class,
ShellCommandLineExecutor::class,
];

/**
Expand Down
78 changes: 78 additions & 0 deletions tests/phpunit/Logger/GitHub/GitDiffFileProviderTest.php
@@ -0,0 +1,78 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Tests\Logger\GitHub;

use Infection\Logger\GitHub\GitDiffFileProvider;
use Infection\Logger\GitHub\NoFilesInDiffToMutate;
use Infection\Process\ShellCommandLineExecutor;
use const PHP_OS_FAMILY;
use PHPUnit\Framework\TestCase;

final class GitDiffFileProviderTest extends TestCase
{
public function test_it_throws_no_code_to_mutate_exception_when_diff_is_empty(): void
{
$shellCommandLineExecutor = $this->createMock(ShellCommandLineExecutor::class);
$shellCommandLineExecutor->expects($this->once())
->method('execute')
->willReturn('');

$this->expectException(NoFilesInDiffToMutate::class);

$diffProvider = new GitDiffFileProvider($shellCommandLineExecutor);
$diffProvider->provide('AM', 'master');
}

public function test_it_executes_diff_and_returns_filter_as_a_string(): void
{
$expectedCommandLine = 'git diff \'master\' --diff-filter=\'AM\' --name-only | grep src/ | paste -s -d "," -';

if (PHP_OS_FAMILY === 'Windows') {
$expectedCommandLine = 'git diff "master" --diff-filter="AM" --name-only | grep src/ | paste -s -d "," -';
}

$shellCommandLineExecutor = $this->createMock(ShellCommandLineExecutor::class);
$shellCommandLineExecutor->expects($this->once())
->method('execute')
->with($expectedCommandLine)
->willReturn('src/A.php,src/B.php');

$diffProvider = new GitDiffFileProvider($shellCommandLineExecutor);
$filter = $diffProvider->provide('AM', 'master');

$this->assertSame('src/A.php,src/B.php', $filter);
}
}
53 changes: 53 additions & 0 deletions tests/phpunit/Logger/GitHub/NoFilesInDiffToMutateTest.php
@@ -0,0 +1,53 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Tests\Logger\GitHub;

use Infection\Logger\GitHub\NoFilesInDiffToMutate;
use PHPUnit\Framework\TestCase;

final class NoFilesInDiffToMutateTest extends TestCase
{
public function test_composer_not_found_exception(): void
{
$exception = NoFilesInDiffToMutate::create();

$this->assertInstanceOf(NoFilesInDiffToMutate::class, $exception);
$this->assertSame(
'No files in diff found, skipping mutation analysis.',
$exception->getMessage()
);
}
}

0 comments on commit a7ece6f

Please sign in to comment.