Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop Infection execution with 0 exit code when git diff filter returns empty result #1600

Merged
merged 4 commits into from Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
71 changes: 71 additions & 0 deletions tests/phpunit/Logger/GitHub/GitDiffFileProviderTest.php
@@ -0,0 +1,71 @@
<?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 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
{
$shellCommandLineExecutor = $this->createMock(ShellCommandLineExecutor::class);
$shellCommandLineExecutor->expects($this->once())
->method('execute')
->with('git diff \'master\' --diff-filter=\'AM\' --name-only | grep src/ | paste -s -d "," -')
->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()
);
}
}