From 937374431a0d0076753b39a0593d3276aee82565 Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Thu, 28 May 2020 08:42:00 +0100 Subject: [PATCH 1/3] Fix Windows builds running Pest --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d22ce69d..40136288 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,7 +36,7 @@ jobs: run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist - name: Unit Tests - run: bin/pest --colors=always --exclude-group=integration + run: php bin/pest --colors=always --exclude-group=integration - name: Integration Tests - run: bin/pest --colors=always --group=integration + run: php bin/pest --colors=always --group=integration From b093e8ee29e8edaa978acb3d5329285d8df4a61d Mon Sep 17 00:00:00 2001 From: "johannes.pichler" Date: Fri, 29 May 2020 11:22:45 +0200 Subject: [PATCH 2/3] Integrate pest-plugins with 2 interfaces integrate pest-plugin package and remove core coverage stuff --- composer.json | 9 +- src/Actions/AddsCoverage.php | 79 ---------- src/Console/Command.php | 38 +++-- src/Console/Coverage.php | 167 --------------------- src/Contracts/Plugins/AddsOutput.php | 19 +++ src/Contracts/Plugins/HandlesArguments.php | 25 +++ tests/.snapshots/success.txt | 11 +- tests/Unit/Actions/AddsCoverage.php | 32 ---- tests/Unit/Console/Coverage.php | 24 --- 9 files changed, 71 insertions(+), 333 deletions(-) delete mode 100644 src/Actions/AddsCoverage.php delete mode 100644 src/Console/Coverage.php create mode 100644 src/Contracts/Plugins/AddsOutput.php create mode 100644 src/Contracts/Plugins/HandlesArguments.php delete mode 100644 tests/Unit/Actions/AddsCoverage.php delete mode 100644 tests/Unit/Console/Coverage.php diff --git a/composer.json b/composer.json index 3f9a60ae..a8c085a4 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "require": { "php": "^7.3", "nunomaduro/collision": "^5.0", + "pestphp/pest-plugin": "@dev", "phpunit/phpunit": "^9.1.4", "sebastian/environment": "^5.1" }, @@ -81,5 +82,11 @@ "Pest\\Laravel\\PestServiceProvider" ] } - } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/fetzi/pest-plugin.git" + } + ] } diff --git a/src/Actions/AddsCoverage.php b/src/Actions/AddsCoverage.php deleted file mode 100644 index e1abdf9c..00000000 --- a/src/Actions/AddsCoverage.php +++ /dev/null @@ -1,79 +0,0 @@ - - */ - private const OPTIONS = [self::COVERAGE_OPTION, self::MIN_OPTION]; - - /** - * If any, adds the coverage params to the given original arguments. - * - * @param array $originals - * - * @return array - */ - public static function from(TestSuite $testSuite, array $originals): array - { - $arguments = array_merge([''], array_values(array_filter($originals, function ($original): bool { - foreach (self::OPTIONS as $option) { - if ($original === sprintf('--%s', $option) || Str::startsWith($original, sprintf('--%s=', $option))) { - return true; - } - } - - return false; - }))); - - $originals = array_flip($originals); - foreach ($arguments as $argument) { - unset($originals[$argument]); - } - $originals = array_flip($originals); - - $inputs = []; - $inputs[] = new InputOption(self::COVERAGE_OPTION, null, InputOption::VALUE_NONE); - $inputs[] = new InputOption(self::MIN_OPTION, null, InputOption::VALUE_REQUIRED); - - $input = new ArgvInput($arguments, new InputDefinition($inputs)); - if ((bool) $input->getOption(self::COVERAGE_OPTION)) { - $testSuite->coverage = true; - $originals[] = '--coverage-php'; - $originals[] = Coverage::getPath(); - } - - if ($input->getOption(self::MIN_OPTION) !== null) { - /* @phpstan-ignore-next-line */ - $testSuite->coverageMin = (float) $input->getOption(self::MIN_OPTION); - } - - return $originals; - } -} diff --git a/src/Console/Command.php b/src/Console/Command.php index 071e691a..d496a810 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -4,11 +4,13 @@ declare(strict_types=1); namespace Pest\Console; -use Pest\Actions\AddsCoverage; use Pest\Actions\AddsDefaults; use Pest\Actions\AddsTests; use Pest\Actions\LoadStructure; use Pest\Actions\ValidatesConfiguration; +use Pest\Contracts\Plugins\AddsOutput; +use Pest\Contracts\Plugins\HandlesArguments; +use Pest\Plugin\Loader; use Pest\TestSuite; use PHPUnit\Framework\TestSuite as BaseTestSuite; use PHPUnit\TextUI\Command as BaseCommand; @@ -54,9 +56,14 @@ final class Command extends BaseCommand protected function handleArguments(array $argv): void { /* - * First, let's handle pest is own `--coverage` param. + * First, let's call all plugins that want to handle arguments */ - $argv = AddsCoverage::from($this->testSuite, $argv); + $plugins = Loader::getPlugins(HandlesArguments::class); + + /** @var HandlesArguments $plugin */ + foreach ($plugins as $plugin) { + $argv = $plugin->handleArguments($this->testSuite, $argv); + } /* * Next, as usual, let's send the console arguments to PHPUnit. @@ -119,25 +126,14 @@ final class Command extends BaseCommand { $result = parent::run($argv, false); - if ($result === 0 && $this->testSuite->coverage) { - if (!Coverage::isAvailable()) { - $this->output->writeln( - "\n ERROR No code coverage driver is available.", - ); - exit(1); - } + /* + * Let's call all plugins that want to add output after test execution + */ + $plugins = Loader::getPlugins(AddsOutput::class); - $coverage = Coverage::report($this->output); - - $result = (int) ($coverage < $this->testSuite->coverageMin); - - if ($result === 1) { - $this->output->writeln(sprintf( - "\n FAIL Code coverage below expected: %s %%. Minimum: %s %%.", - number_format($coverage, 1), - number_format($this->testSuite->coverageMin, 1) - )); - } + /** @var AddsOutput $plugin */ + foreach ($plugins as $plugin) { + $plugin->addOutput($this->testSuite, $this->output, $result); } exit($result); diff --git a/src/Console/Coverage.php b/src/Console/Coverage.php deleted file mode 100644 index 2da149a0..00000000 --- a/src/Console/Coverage.php +++ /dev/null @@ -1,167 +0,0 @@ -canCollectCodeCoverage(); - } - - /** - * Reports the code coverage report to the - * console and returns the result in float. - */ - public static function report(OutputInterface $output): float - { - if (!file_exists($reportPath = self::getPath())) { - throw ShouldNotHappen::fromMessage(sprintf('Coverage not found in path: %s.', $reportPath)); - } - - /** @var \SebastianBergmann\CodeCoverage\CodeCoverage $codeCoverage */ - $codeCoverage = require $reportPath; - unlink($reportPath); - - $totalWidth = (new Terminal())->getWidth(); - - $dottedLineLength = $totalWidth <= 70 ? $totalWidth : 70; - - $totalCoverage = $codeCoverage->getReport()->getLineExecutedPercent(); - - $output->writeln( - sprintf( - ' Cov: %s', - $totalCoverage - ) - ); - - $output->writeln(''); - - /** @var Directory $report */ - $report = $codeCoverage->getReport(); - - foreach ($report->getIterator() as $file) { - if (!$file instanceof File) { - continue; - } - $dirname = dirname($file->getId()); - $basename = basename($file->getId(), '.php'); - - $name = $dirname === '.' ? $basename : implode(DIRECTORY_SEPARATOR, [ - $dirname, - $basename, - ]); - $rawName = $dirname === '.' ? $basename : implode(DIRECTORY_SEPARATOR, [ - $dirname, - $basename, - ]); - - $linesExecutedTakenSize = 0; - - if ($file->getLineExecutedPercent() != '0.00%') { - $linesExecutedTakenSize = strlen($uncoveredLines = trim(implode(', ', self::getMissingCoverage($file)))) + 1; - $name .= sprintf(' %s', $uncoveredLines); - } - - $percentage = $file->getNumExecutableLines() === 0 - ? '100.0' - : number_format((float) $file->getLineExecutedPercent(), 1, '.', ''); - - $takenSize = strlen($rawName . $percentage) + 4 + $linesExecutedTakenSize; // adding 3 space and percent sign - - $percentage = sprintf( - '%s', - $percentage === '100.0' ? 'green' : ($percentage === '0.0' ? 'red' : 'yellow'), - $percentage - ); - - $output->writeln(sprintf(' %s %s %s %%', - $name, - str_repeat('.', max($dottedLineLength - $takenSize, 1)), - $percentage - )); - } - - return (float) $totalCoverage; - } - - /** - * Generates an array of missing coverage on the following format:. - * - * ``` - * ['11', '20..25', '50', '60...80']; - * ``` - * - * @param File $file - * - * @return array - */ - public static function getMissingCoverage($file): array - { - $shouldBeNewLine = true; - - $eachLine = function (array $array, array $tests, int $line) use (&$shouldBeNewLine): array { - if (count($tests) > 0) { - $shouldBeNewLine = true; - - return $array; - } - - if ($shouldBeNewLine) { - $array[] = (string) $line; - $shouldBeNewLine = false; - - return $array; - } - - $lastKey = count($array) - 1; - - if (array_key_exists($lastKey, $array) && strpos($array[$lastKey], '..') !== false) { - [$from] = explode('..', $array[$lastKey]); - $array[$lastKey] = sprintf('%s..%s', $from, $line); - - return $array; - } - - $array[$lastKey] = sprintf('%s..%s', $array[$lastKey], $line); - - return $array; - }; - - $array = []; - foreach (array_filter($file->getCoverageData(), 'is_array') as $line => $tests) { - $array = $eachLine($array, $tests, $line); - } - - return $array; - } -} diff --git a/src/Contracts/Plugins/AddsOutput.php b/src/Contracts/Plugins/AddsOutput.php new file mode 100644 index 00000000..82c7d6bd --- /dev/null +++ b/src/Contracts/Plugins/AddsOutput.php @@ -0,0 +1,19 @@ + $arguments + * + * @return array the updated list of arguments + */ + public function handleArguments(TestSuite $testSuite, array $arguments): array; +} diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 23b93305..25f4531a 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -98,10 +98,6 @@ PASS Tests\Plugins\Traits ✓ it allows global uses - PASS Tests\Unit\Actions\AddsCoverage - ✓ it adds coverage if --coverage exist - ✓ it adds coverage if --min exist - PASS Tests\Unit\Actions\AddsDefaults ✓ it sets defaults ✓ it does not override options @@ -115,9 +111,6 @@ ✓ it throws exception when `process isolation` is true ✓ it do not throws exception when `process isolation` is false - PASS Tests\Unit\Console\Coverage - ✓ it generates coverage based on file input - PASS Tests\Unit\Support\Backtrace ✓ it gets file name from called file @@ -135,5 +128,5 @@ WARN Tests\Visual\Success s visual snapshot of test suite on success - Tests: 6 skipped, 70 passed - Time: 2.68s + Tests: 6 skipped, 67 passed + Time: 2.67s diff --git a/tests/Unit/Actions/AddsCoverage.php b/tests/Unit/Actions/AddsCoverage.php deleted file mode 100644 index 4d384615..00000000 --- a/tests/Unit/Actions/AddsCoverage.php +++ /dev/null @@ -1,32 +0,0 @@ -coverage); - - $arguments = AddsCoverage::from($testSuite, []); - assertEquals([], $arguments); - assertFalse($testSuite->coverage); - - $arguments = AddsCoverage::from($testSuite, ['--coverage']); - assertEquals(['--coverage-php', \Pest\Console\Coverage::getPath()], $arguments); - assertTrue($testSuite->coverage); -}); - -it('adds coverage if --min exist', function () { - $testSuite = new TestSuite(getcwd()); - assertEquals($testSuite->coverageMin, 0.0); - - assertFalse($testSuite->coverage); - AddsCoverage::from($testSuite, []); - assertEquals($testSuite->coverageMin, 0.0); - - AddsCoverage::from($testSuite, ['--min=2']); - assertEquals($testSuite->coverageMin, 2.0); - - AddsCoverage::from($testSuite, ['--min=2.4']); - assertEquals($testSuite->coverageMin, 2.4); -}); diff --git a/tests/Unit/Console/Coverage.php b/tests/Unit/Console/Coverage.php deleted file mode 100644 index 1a395039..00000000 --- a/tests/Unit/Console/Coverage.php +++ /dev/null @@ -1,24 +0,0 @@ - ['foo'], - 2 => ['bar'], - 4 => [], - 5 => [], - 6 => [], - 7 => null, - 100 => null, - 101 => ['foo'], - 102 => [], - ]; - } - })); -}); From 78cdd4da36412b8293631f357c821ed451c4b196 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 29 May 2020 21:15:51 +0200 Subject: [PATCH 3/3] Update composer.json --- composer.json | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index a8c085a4..ff0c912e 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require": { "php": "^7.3", "nunomaduro/collision": "^5.0", - "pestphp/pest-plugin": "@dev", + "pestphp/pest-plugin": "dev-master", "phpunit/phpunit": "^9.1.4", "sebastian/environment": "^5.1" }, @@ -82,11 +82,5 @@ "Pest\\Laravel\\PestServiceProvider" ] } - }, - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/fetzi/pest-plugin.git" - } - ] + } }