-
-
Notifications
You must be signed in to change notification settings - Fork 161
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
Fail Infection execution when at least 1 ignore source code regex wasn't matched #1631
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,10 @@ | |
|
||
namespace Infection\Process\Runner; | ||
|
||
use function array_keys; | ||
use function array_merge; | ||
use function array_unique; | ||
use function array_values; | ||
use Infection\Differ\DiffSourceCodeMatcher; | ||
use Infection\Event\EventDispatcher\EventDispatcher; | ||
use Infection\Event\MutantProcessWasFinished; | ||
|
@@ -47,6 +51,7 @@ | |
use Infection\Mutation\Mutation; | ||
use Infection\Process\Factory\MutantProcessFactory; | ||
use function Pipeline\take; | ||
use function Safe\array_flip; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
/** | ||
|
@@ -99,15 +104,19 @@ public function run(iterable $mutations, string $testFrameworkExtraOptions): voi | |
$numberOfMutants = IterableCounter::bufferAndCountIfNeeded($mutations, $this->runConcurrently); | ||
$this->eventDispatcher->dispatch(new MutationTestingWasStarted($numberOfMutants)); | ||
|
||
$notMatchedSourceCodeRegexes = array_flip(array_unique(array_merge(...array_values($this->ignoreSourceCodeMutatorsMap)))); | ||
|
||
$processes = take($mutations) | ||
->cast(function (Mutation $mutation): Mutant { | ||
return $this->mutantFactory->create($mutation); | ||
}) | ||
->filter(function (Mutant $mutant): bool { | ||
->filter(function (Mutant $mutant) use (&$notMatchedSourceCodeRegexes): bool { | ||
$mutatorName = $mutant->getMutation()->getMutatorName(); | ||
|
||
foreach ($this->ignoreSourceCodeMutatorsMap[$mutatorName] ?? [] as $sourceCodeRegex) { | ||
if ($this->diffSourceCodeMatcher->matches($mutant->getDiff()->get(), $sourceCodeRegex)) { | ||
unset($notMatchedSourceCodeRegexes[$sourceCodeRegex]); | ||
|
||
$this->eventDispatcher->dispatch(new MutantProcessWasFinished( | ||
MutantExecutionResult::createFromIgnoredMutant($mutant) | ||
)); | ||
|
@@ -118,6 +127,13 @@ public function run(iterable $mutations, string $testFrameworkExtraOptions): voi | |
|
||
return true; | ||
}) | ||
->filter(static function (Mutant $mutant) use ($notMatchedSourceCodeRegexes): bool { | ||
if ($notMatchedSourceCodeRegexes !== []) { | ||
throw NotMatchedIgnoreSourceCodeRegexFound::forRegexes(array_keys($notMatchedSourceCodeRegexes)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not going to work because this gets called for every mutant, even for the very first one. |
||
} | ||
|
||
return true; | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sanmai I need your help here. I feel like What I want to do is after Mutants are filtered and we completed building So, desired logic: take($mutations)
->filter(fn (Mutant $mutant): bool {
// filter ignored mutants out and build $notMatchedSourceCodeRegexes
})
// (!) new
->tap(fn (array $mutants): array {
// check $notMatchedSourceCodeRegexes and throw an exception if needed
return $mutants;
})
->filter() // continue existing logic - filter not covered by tests mutants do you have any recommendations here? is it the right way to go? So, the following diff will reduce the number of iterations from N to 1: - ->filter(static function (Mutant $mutant) use ($notMatchedSourceCodeRegexes): bool {
+ ->tap(static function (array $mutants) use ($notMatchedSourceCodeRegexes): array {
if ($notMatchedSourceCodeRegexes !== []) {
hrow NotMatchedIgnoreSourceCodeRegexFound::forRegexes(array_keys($notMatchedSourceCodeRegexes));
}
- return true;
+ return $mutants;
}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem here is that there's no If you want to drop arbitrary parts of a sequence (say, first five, or two after ten), there's a handy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm looking for a place (method) where I can do some job after the first what are your recommendations? I can leave it as is, in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you can't know if you're looking at the very last mutant, you can't throw an exception from any pipeline method here. Neither you should require to iterate over mutants all over again (all memory savings we get will go out of window). Therefore I'd like to suggest to track regex usages (in a dedicated There's another option: you can inject a fake mutant at the end of the stream, and then catch it somewhere else. I can elaborate further, but this will be incredibly scuffed and very opaque. Simple counting should work better. |
||
->filter(function (Mutant $mutant): bool { | ||
// It's a proxy call to Mutation, can be done one stage up | ||
if ($mutant->isCoveredByTest()) { | ||
|
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\Process\Runner; | ||
|
||
use Exception; | ||
use function implode; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class NotMatchedIgnoreSourceCodeRegexFound extends Exception | ||
{ | ||
/** | ||
* @param array<int, string> $notMatchedSourceCodeRegexes | ||
*/ | ||
public static function forRegexes(array $notMatchedSourceCodeRegexes): self | ||
{ | ||
return new self('The following ignore source code regexes were not matched, consider removing them from `infection.json`: ' . implode(', ', $notMatchedSourceCodeRegexes)); | ||
} | ||
} |
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\Process\Runner; | ||
|
||
use Infection\Process\Runner\NotMatchedIgnoreSourceCodeRegexFound; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class NotMatchedIgnoreSourceCodeRegexFoundTest extends TestCase | ||
{ | ||
public function test_it_is_being_created_with_a_message(): void | ||
{ | ||
$exception = NotMatchedIgnoreSourceCodeRegexFound::forRegexes(['a', 'b']); | ||
|
||
$this->assertInstanceOf(NotMatchedIgnoreSourceCodeRegexFound::class, $exception); | ||
$this->assertSame( | ||
'The following ignore source code regexes were not matched, consider removing them from `infection.json`: a, b', | ||
$exception->getMessage() | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be: