From ddc08cf0f9947462ed8bbbe206e72de639e8fcf7 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sun, 24 May 2020 21:41:35 +0200 Subject: [PATCH 01/11] docs: updates changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8c14f01..b9f38b19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +## [v0.1.5 (2020-05-24)](https://github.com/pestphp/pest/compare/v0.1.4...v0.1.5) +### Fixed +- Missing default decorated output on coverage ([88d2391(https://github.com/pestphp/pest/commit/88d2391d2e6fe9c9416462734b9b523cb418f469)) + ## [v0.1.4 (2020-05-24)](https://github.com/pestphp/pest/compare/v0.1.3...v0.1.4) ### Added - Support to Lumen on artisan commands ([#18](https://github.com/pestphp/pest/pull/18)) From 937374431a0d0076753b39a0593d3276aee82565 Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Thu, 28 May 2020 08:42:00 +0100 Subject: [PATCH 02/11] 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 03/11] 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 04/11] 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" - } - ] + } } From 2e7ed741b69008bcf742aa35c2aafbbf47f8d597 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 29 May 2020 22:33:05 +0200 Subject: [PATCH 05/11] fix: keeps result from plugin to plugin --- src/Console/Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Command.php b/src/Console/Command.php index d496a810..b4dc3c75 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -133,7 +133,7 @@ final class Command extends BaseCommand /** @var AddsOutput $plugin */ foreach ($plugins as $plugin) { - $plugin->addOutput($this->testSuite, $this->output, $result); + $result = $plugin->addOutput($this->testSuite, $this->output, $result); } exit($result); From 1f2976c0e001cf81fc18bc702832e665b20cc943 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 29 May 2020 22:35:20 +0200 Subject: [PATCH 06/11] tests: types --- src/Contracts/Plugins/AddsOutput.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contracts/Plugins/AddsOutput.php b/src/Contracts/Plugins/AddsOutput.php index 82c7d6bd..543eace9 100644 --- a/src/Contracts/Plugins/AddsOutput.php +++ b/src/Contracts/Plugins/AddsOutput.php @@ -15,5 +15,5 @@ interface AddsOutput /** * Allows to add custom output after the test suite was executed. */ - public function addOutput(TestSuite $testSuite, OutputInterface $output, int $testReturnCode): void; + public function addOutput(TestSuite $testSuite, OutputInterface $output, int $testReturnCode): int; } From 01daf0316cc3dfa262bdc676460f66ca97588f88 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 29 May 2020 22:44:41 +0200 Subject: [PATCH 07/11] refacto: removes unused code about coverage --- src/TestSuite.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/TestSuite.php b/src/TestSuite.php index 311fa644..2d2bbfd8 100644 --- a/src/TestSuite.php +++ b/src/TestSuite.php @@ -30,20 +30,6 @@ final class TestSuite */ public $tests; - /** - * Whether should show the coverage or not. - * - * @var bool - */ - public $coverage = false; - - /** - * The minimum coverage. - * - * @var float - */ - public $coverageMin = 0.0; - /** * Holds the before each repository. * From 76796ffc6771b866e9377c7bbe5508d5108e7e49 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sat, 30 May 2020 01:43:02 +0200 Subject: [PATCH 08/11] refacto: includes coverage plugin --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index ff0c912e..2f2ffd62 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "php": "^7.3", "nunomaduro/collision": "^5.0", "pestphp/pest-plugin": "dev-master", + "pestphp/pest-plugin-coverage": "dev-master", "phpunit/phpunit": "^9.1.4", "sebastian/environment": "^5.1" }, From 73f56e58a5266470ba9ff1f7cd698830fb14e177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jib=C3=A9=20Barth?= Date: Sun, 31 May 2020 12:32:43 +0200 Subject: [PATCH 09/11] Pass color and verbosity args to printer (fix #49) --- src/Actions/AddsDefaults.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Actions/AddsDefaults.php b/src/Actions/AddsDefaults.php index 951a1e46..799eda6e 100644 --- a/src/Actions/AddsDefaults.php +++ b/src/Actions/AddsDefaults.php @@ -21,7 +21,7 @@ final class AddsDefaults public static function to(array $arguments): array { if (!array_key_exists('printer', $arguments)) { - $arguments['printer'] = new Printer(); + $arguments['printer'] = new Printer(null, $arguments['verbose'] ?? false, $arguments['colors'] ?? 'always'); } return $arguments; From 28d06663d314f2104cdcfe3b25c4272c75747fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jib=C3=A9=20Barth?= Date: Sun, 31 May 2020 12:33:27 +0200 Subject: [PATCH 10/11] Add tests for --colors=never option --- tests/.snapshots/success.txt | 6 +++-- tests/Visual/SingleTestOrDirectory.php | 34 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 25f4531a..4509aee3 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -124,9 +124,11 @@ PASS Tests\Visual\SingleTestOrDirectory ✓ allows to run a single test ✓ allows to run a directory + ✓ it has ascii chars (decorated printer) + ✓ it disable decorating printer when colors is set to never WARN Tests\Visual\Success s visual snapshot of test suite on success - Tests: 6 skipped, 67 passed - Time: 2.67s + Tests: 6 skipped, 69 passed + Time: 2.34s diff --git a/tests/Visual/SingleTestOrDirectory.php b/tests/Visual/SingleTestOrDirectory.php index 6dd9204b..d7a42e0a 100644 --- a/tests/Visual/SingleTestOrDirectory.php +++ b/tests/Visual/SingleTestOrDirectory.php @@ -30,3 +30,37 @@ test('allows to run a directory', function () use ($run) { Tests: 2 passed EOF, $run('tests/Fixtures')); }); + +it('has ascii chars (decorated printer)', function () { + $process = new Process([ + './bin/pest', + 'tests/Fixtures/DirectoryWithTests/ExampleTest.php', + ], dirname(__DIR__, 2)); + + $process->run(); + $output = $process->getOutput(); + assertStringContainsString(<<run(); + $output = $process->getOutput(); + + assertStringContainsString(<< Date: Sun, 31 May 2020 13:42:27 +0200 Subject: [PATCH 11/11] chore: cs --- tests/Visual/SingleTestOrDirectory.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Visual/SingleTestOrDirectory.php b/tests/Visual/SingleTestOrDirectory.php index d7a42e0a..d86fc4c9 100644 --- a/tests/Visual/SingleTestOrDirectory.php +++ b/tests/Visual/SingleTestOrDirectory.php @@ -62,5 +62,4 @@ it('disable decorating printer when colors is set to never', function () { Tests: 1 passed EOF, $output); - });