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 (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.
  • Loading branch information
maks-rafalko committed Oct 27, 2021
1 parent 7b995c2 commit de6048b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
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
49 changes: 49 additions & 0 deletions src/Logger/GitHub/NoFilesInDiffToMutate.php
Original file line number Diff line number Diff line change
@@ -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.');
}
}
53 changes: 53 additions & 0 deletions tests/phpunit/Logger/GitHub/NoFilesInDiffToMutateTest.php
Original file line number Diff line number Diff line change
@@ -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 de6048b

Please sign in to comment.