Skip to content

Commit

Permalink
re-adds back covered_only
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Sep 5, 2024
1 parent bc11eaa commit 8d00113
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Contracts/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function min(float $minScore, ?bool $failOnZeroMutations = null): self;

public function ignoreMinScoreOnZeroMutations(bool $ignore = true): self;

public function coveredOnly(bool $coveredOnly = true): self;

public function parallel(bool $parallel = true): self;

public function processes(?int $processes = null): self;
Expand Down
28 changes: 28 additions & 0 deletions src/Options/CoveredOnlyOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Pest\Mutate\Options;

use Symfony\Component\Console\Input\InputOption;

class CoveredOnlyOption
{
final public const ARGUMENT = 'covered-only';

public static function remove(): bool
{
return true;
}

public static function match(string $argument): bool
{
return $argument === sprintf('--%s', self::ARGUMENT) ||
str_starts_with($argument, sprintf('--%s=', self::ARGUMENT));
}

public static function inputOption(): InputOption
{
return new InputOption(sprintf('--%s', self::ARGUMENT), null, InputOption::VALUE_OPTIONAL, '');
}
}
2 changes: 1 addition & 1 deletion src/Repositories/ConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function mergedConfiguration(): Configuration
}

return $this->mergedConfiguration = new Configuration(
coveredOnly: true,
coveredOnly: $config['covered_only'] ?? false,
paths: $config['paths'] ?? $this->pathsFromPhpunitConfiguration(),
pathsToIgnore: $config['paths_to_ignore'] ?? [],
mutators: array_diff($config['mutators'] ?? DefaultSet::mutators(), $config['excluded_mutators'] ?? []),
Expand Down
15 changes: 14 additions & 1 deletion src/Support/Configuration/AbstractConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ abstract class AbstractConfiguration implements ConfigurationContract
*/
private ?array $paths = null;

/**
* @var bool
*/
private ?bool $coveredOnly = null;

/**
* @var string[]|null
*/
Expand Down Expand Up @@ -114,6 +119,13 @@ public function ignoreMinScoreOnZeroMutations(bool $ignore = true): self
return $this;
}

public function coveredOnly(bool $coveredOnly = true): self
{
$this->coveredOnly = $coveredOnly;

return $this;
}

public function parallel(bool $parallel = true): self
{
$this->parallel = $parallel;
Expand Down Expand Up @@ -185,11 +197,12 @@ public function everything(): self
}

/**
* @return array{paths?: string[], paths_to_ignore?: string[], mutators?: class-string<Mutator>[], excluded_mutators?: class-string<Mutator>[], classes?: string[], parallel?: bool, processes?: int, profile?: bool, min_score?: float, ignore_min_score_on_zero_mutations?: bool, covered_only?: bool, stop_on_untested?: bool, stop_on_uncovered?: bool, mutation_id?: string, retry?: bool, everything?: bool}
* @return array{covered_only?: bool, paths?: string[], paths_to_ignore?: string[], mutators?: class-string<Mutator>[], excluded_mutators?: class-string<Mutator>[], classes?: string[], parallel?: bool, processes?: int, profile?: bool, min_score?: float, ignore_min_score_on_zero_mutations?: bool, covered_only?: bool, stop_on_untested?: bool, stop_on_uncovered?: bool, mutation_id?: string, retry?: bool, everything?: bool}
*/
public function toArray(): array
{
return array_filter([
'covered_only' => $this->coveredOnly,
'paths' => $this->paths,
'paths_to_ignore' => $this->pathsToIgnore,
'mutators' => $this->mutators !== null ? array_values(array_diff($this->mutators, $this->excludedMutators ?? [])) : null,
Expand Down
6 changes: 6 additions & 0 deletions src/Support/Configuration/CliConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Pest\Mutate\Options\BailOption;
use Pest\Mutate\Options\ClassOption;
use Pest\Mutate\Options\ClearCacheOption;
use Pest\Mutate\Options\CoveredOnlyOption;
use Pest\Mutate\Options\EverythingOption;
use Pest\Mutate\Options\ExceptOption;
use Pest\Mutate\Options\IgnoreMinScoreOnZeroMutationsOption;
Expand Down Expand Up @@ -51,6 +52,7 @@ class CliConfiguration extends AbstractConfiguration
NoCacheOption::class,
ClearCacheOption::class,
EverythingOption::class,
CoveredOnlyOption::class,
];

/**
Expand Down Expand Up @@ -94,6 +96,10 @@ public function fromArguments(array $arguments): array
$this->except(explode(',', (string) $input->getOption(ExceptOption::ARGUMENT))); // @phpstan-ignore-line
}

if ($input->hasOption(CoveredOnlyOption::ARGUMENT)) {
$this->coveredOnly($input->getOption(CoveredOnlyOption::ARGUMENT) !== 'false');
}

if ($input->hasOption(MinScoreOption::ARGUMENT)) {
$this->min((float) $input->getOption(MinScoreOption::ARGUMENT)); // @phpstan-ignore-line
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Features/Profiles/HandlesCliProfileConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
->paths_to_ignore->toEqual(['src/path-1', 'src/path-2']);
});

it('enables covered only option if --covered-only argument is passed', function (): void {
$this->configuration->fromArguments(['--mutate='.ConfigurationRepository::FAKE]);
expect($this->configuration->toArray())
->covered_only->toBeNull();

$this->configuration->fromArguments(['--covered-only']);
expect($this->configuration->toArray())
->covered_only->toBeTrue();

$this->configuration->fromArguments(['--covered-only=true']);
expect($this->configuration->toArray())
->covered_only->toBeTrue();

$this->configuration->fromArguments(['--covered-only=false']);
expect($this->configuration->toArray())
->covered_only->toBeFalse();
});

it('sets the mutators if --mutators argument is passed', function (): void {
$this->configuration->fromArguments(['--mutator=SetArithmetic']);
expect($this->configuration->toArray())
Expand Down

0 comments on commit 8d00113

Please sign in to comment.