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

Add pre-filters configuration option #1327

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,12 @@
"testFrameworkOptions": {
"type": "string",
"description": "Specify an additional options to pass to the test framework (e.g. enabling Verbose Mode). --test-framework-options will override this option."
},
"plugins": {
"type": "array",
"items": {
"type": "string"
}
}
},
"definitions": {
Expand Down
4 changes: 4 additions & 0 deletions scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
'whitelist' => [
\Composer\Autoload\ClassLoader::class,
'Safe\*',
\Infection\Plugins\Plugin::class,
\Infection\Plugins\MutantFilterPlugin::class,
\Infection\Plugins\Mutant::class,
\Infection\Plugins\Configuration::class,
],
'whitelist-global-constants' => false,
'whitelist-global-classes' => false,
Expand Down
15 changes: 14 additions & 1 deletion src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Infection\Configuration\Entry\Logs;
use Infection\Configuration\Entry\PhpUnit;
use Infection\Mutator\Mutator;
use Infection\Plugins\Plugin;
use Infection\TestFramework\TestFrameworkTypes;
use Symfony\Component\Finder\SplFileInfo;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -82,13 +83,15 @@ class Configuration
private $threadCount;
private $dryRun;
private $ignoreSourceCodeMutatorsMap;
private $plugins;

/**
* @param string[] $sourceDirectories
* @param string[] $sourceFilesExcludes
* @param iterable<SplFileInfo> $sourceFiles
* @param array<string, Mutator> $mutators
* @param array<string, array<int, string>> $ignoreSourceCodeMutatorsMap
* @param array<class-string<Plugin>> $plugins
*/
public function __construct(
float $timeout,
Expand Down Expand Up @@ -118,7 +121,8 @@ public function __construct(
int $msiPrecision,
int $threadCount,
bool $dryRun,
array $ignoreSourceCodeMutatorsMap
array $ignoreSourceCodeMutatorsMap,
array $plugins
) {
Assert::nullOrGreaterThanEq($timeout, 0);
Assert::allString($sourceDirectories);
Expand Down Expand Up @@ -156,6 +160,7 @@ public function __construct(
$this->threadCount = $threadCount;
$this->dryRun = $dryRun;
$this->ignoreSourceCodeMutatorsMap = $ignoreSourceCodeMutatorsMap;
$this->plugins = $plugins;
}

public function getProcessTimeout(): float
Expand Down Expand Up @@ -255,6 +260,14 @@ public function shouldSkipInitialTests(): bool
return $this->skipInitialTests;
}

/**
* @return array<class-string<Plugin>>
*/
public function getPlugins(): array
{
return $this->plugins;
}

public function isDebugEnabled(): bool
{
return $this->debug;
Expand Down
3 changes: 2 additions & 1 deletion src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ public function create(
$msiPrecision,
$threadCount,
$dryRun,
$ignoreSourceCodeMutatorsMap
$ignoreSourceCodeMutatorsMap,
$schema->getPlugins()
);
}

Expand Down
15 changes: 14 additions & 1 deletion src/Configuration/Schema/SchemaConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Infection\Configuration\Entry\Logs;
use Infection\Configuration\Entry\PhpUnit;
use Infection\Configuration\Entry\Source;
use Infection\Plugins\Plugin;
use Infection\TestFramework\TestFrameworkTypes;
use Webmozart\Assert\Assert;

Expand All @@ -60,9 +61,11 @@ final class SchemaConfiguration
private $bootstrap;
private $initialTestsPhpOptions;
private $testFrameworkExtraOptions;
private $plugins;

/**
* @param array<string, mixed> $mutators
* @param array<class-string<Plugin>> $plugins
*/
public function __construct(
string $file,
Expand All @@ -78,7 +81,8 @@ public function __construct(
?string $testFramework,
?string $bootstrap,
?string $initialTestsPhpOptions,
?string $testFrameworkExtraOptions
?string $testFrameworkExtraOptions,
array $plugins
) {
Assert::nullOrGreaterThanEq($timeout, 0);
Assert::nullOrOneOf($testFramework, TestFrameworkTypes::TYPES);
Expand All @@ -97,6 +101,7 @@ public function __construct(
$this->bootstrap = $bootstrap;
$this->initialTestsPhpOptions = $initialTestsPhpOptions;
$this->testFrameworkExtraOptions = $testFrameworkExtraOptions;
$this->plugins = $plugins;
}

public function getFile(): string
Expand Down Expand Up @@ -171,4 +176,12 @@ public function getTestFrameworkExtraOptions(): ?string
{
return $this->testFrameworkExtraOptions;
}

/**
* @return array<class-string<Plugin>>
*/
public function getPlugins(): array
{
return $this->plugins;
}
}
3 changes: 2 additions & 1 deletion src/Configuration/Schema/SchemaConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public function create(string $path, stdClass $rawConfig): SchemaConfiguration
$rawConfig->testFramework ?? null,
self::normalizeString($rawConfig->bootstrap ?? null),
self::normalizeString($rawConfig->initialTestsPhpOptions ?? null),
self::normalizeString($rawConfig->testFrameworkOptions ?? null)
self::normalizeString($rawConfig->testFrameworkOptions ?? null),
(array) ($rawConfig->plugins ?? [])
);
}

Expand Down
24 changes: 24 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
use Infection\Mutant\MutantCodeFactory;
use Infection\Mutant\MutantExecutionResultFactory;
use Infection\Mutant\MutantFactory;
use Infection\Mutant\MutantFiltersHandler;
use Infection\Mutation\FileMutationGenerator;
use Infection\Mutation\MutationAttributeKeys;
use Infection\Mutation\MutationGenerator;
Expand All @@ -89,6 +90,7 @@
use Infection\Mutator\MutatorResolver;
use Infection\PhpParser\FileParser;
use Infection\PhpParser\NodeTraverserFactory;
use Infection\Plugins\Wrapper\ContainerWrapper;
use Infection\Process\Factory\InitialTestsRunProcessFactory;
use Infection\Process\Factory\MutantProcessFactory;
use Infection\Process\Runner\DryProcessRunner;
Expand Down Expand Up @@ -563,6 +565,7 @@ public static function create(): self
return new MutationTestingRunner(
$container->getMutantProcessFactory(),
$container->getMutantFactory(),
$container->getMutantFiltersHandler(),
$container->getProcessRunner(),
$container->getEventDispatcher(),
$configuration->isDryRun()
Expand All @@ -574,6 +577,17 @@ public static function create(): self
$configuration->getIgnoreSourceCodeMutatorsMap()
);
},
MutantFiltersHandler::class => static function (self $container): MutantFiltersHandler {
return new MutantFiltersHandler(
$container->getPluginProvider()->getMutantFilters()
);
},
PluginProvider::class => static function (self $container): PluginProvider {
return new PluginProvider(
$container->getConfiguration()->getPlugins(),
new ContainerWrapper($container)
);
},
LineRangeCalculator::class => static function (): LineRangeCalculator {
return new LineRangeCalculator();
},
Expand Down Expand Up @@ -852,6 +866,16 @@ public function getMutantFactory(): MutantFactory
return $this->get(MutantFactory::class);
}

public function getMutantFiltersHandler(): MutantFiltersHandler
{
return $this->get(MutantFiltersHandler::class);
}

public function getPluginProvider(): PluginProvider
{
return $this->get(PluginProvider::class);
}

public function getDiffer(): Differ
{
return $this->get(Differ::class);
Expand Down
12 changes: 12 additions & 0 deletions src/Mutant/Mutant.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

use Infection\AbstractTestFramework\Coverage\TestLocation;
use Infection\Mutation\Mutation;
use Infection\Plugins\Mutant as PublicMutant;
use Infection\Plugins\Wrapper\MutantWrapper;

/**
* @internal
Expand All @@ -50,6 +52,11 @@ class Mutant
private $diff;
private $prettyPrintedOriginalCode;

/**
* @var PublicMutant|null
*/
private $wrapper;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private $wrapper;
private $publicMutant;


public function __construct(
string $mutantFilePath,
Mutation $mutation,
Expand Down Expand Up @@ -101,4 +108,9 @@ public function getTests(): array
{
return $this->mutation->getAllTests();
}

public function getMutantWrapper(): PublicMutant
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function getMutantWrapper(): PublicMutant
public function getPublicMutant(): PublicMutant

{
return $this->wrapper ?? $this->wrapper = new MutantWrapper($this);
}
}
81 changes: 81 additions & 0 deletions src/Mutant/MutantFiltersHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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\Mutant;

use Infection\Plugins\Mutant as PublicMutant;
use Infection\Plugins\MutantFilterPlugin;
use Pipeline\Interfaces\StandardPipeline as MutantPipeline;

/**
* @internal
* @final
*/
class MutantFiltersHandler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class MutantFiltersHandler
class MutantFilterer

?

{
/**
* @var array<callable>
* @psalm-var array<callable(PublicMutant): bool>
*/
private $filters;

/**
* @param array<MutantFilterPlugin> $filters
*/
public function __construct(array $filters)
{
$this->filters = [];

foreach ($filters as $filter) {
$filterCallable = $filter->getMutantFilter();

if ($filterCallable !== null) {
$this->filters[] = $filterCallable;
}
}
}

/**
* @param MutantPipeline<Mutant> $mutants
*/
public function applyFilters(MutantPipeline $mutants): void
theofidry marked this conversation as resolved.
Show resolved Hide resolved
{
foreach ($this->filters as $filterCallback) {
$mutants->filter(static function (Mutant $mutant) use ($filterCallback): bool {
return $filterCallback($mutant->getMutantWrapper());
});
}
}
}