Skip to content

Commit

Permalink
Merge pull request #405 from def-studio/add-new-ci-option-to-pest-bin
Browse files Browse the repository at this point in the history
Add new --ci option to pest bin
  • Loading branch information
nunomaduro authored Sep 25, 2021
2 parents 66d47e4 + 7d70b6e commit a6e34d2
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 13 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"plugins": [
"Pest\\Plugins\\Coverage",
"Pest\\Plugins\\Init",
"Pest\\Plugins\\Version"
"Pest\\Plugins\\Version",
"Pest\\Plugins\\Context"
]
},
"laravel": {
Expand Down
47 changes: 47 additions & 0 deletions src/Plugins/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Pest\Plugins;

use Pest\Contracts\Plugins\HandlesArguments;

/**
* @internal
*/
final class Context implements HandlesArguments
{
public const ENV_CI = 'ci';
public const ENV_LOCAL = 'local';

/**
* @var \Pest\Plugins\Context
*/
private static $instance;

/**
* @var string
*/
public $env = 'local';

public static function getInstance(): Context
{
if (self::$instance === null) {
self::$instance = new self();
}

return self::$instance;
}

public function handleArguments(array $arguments): array
{
foreach ($arguments as $index => $argument) {
if ($argument === '--ci') {
unset($arguments[$index]);
self::getInstance()->env = 'ci';
}
}

return array_values($arguments);
}
}
5 changes: 5 additions & 0 deletions src/Repositories/TestRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Pest\Exceptions\TestCaseAlreadyInUse;
use Pest\Exceptions\TestCaseClassOrTraitNotFound;
use Pest\Factories\TestCaseFactory;
use Pest\Plugins\Context;
use Pest\Support\Reflection;
use Pest\Support\Str;
use Pest\TestSuite;
Expand Down Expand Up @@ -119,6 +120,10 @@ public function build(TestSuite $testSuite, callable $each): void
*/
private function testsUsingOnly(): array
{
if (Context::getInstance()->env === Context::ENV_CI) {
return [];
}

return array_filter($this->state, function ($testFactory): bool {
return $testFactory->only;
});
Expand Down
4 changes: 2 additions & 2 deletions src/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function __construct(string $rootPath, string $testPath)
$this->afterEach = new AfterEachRepository();
$this->afterAll = new AfterAllRepository();

$this->rootPath = (string) realpath($rootPath);
$this->testPath = $testPath;
$this->rootPath = (string) realpath($rootPath);
$this->testPath = $testPath;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@
✓ it show the actual dataset of multiple non-named datasets in their description
✓ it show the correct description for mixed named and not-named datasets

PASS Tests\Unit\Plugins\Context
✓ environment is set to CI when --ci option is used
✓ environment is set to Local when --ci option is not used

PASS Tests\Unit\Plugins\Version
✓ it outputs the version when --version is used
✓ it do not outputs version when --version is not used
Expand All @@ -682,6 +686,7 @@
✓ it alerts users about tests with arguments but no input
✓ it can return an array of all test suite filenames
✓ it can filter the test suite filenames to those with the only method
✓ it does not filter the test suite filenames to those with the only method when working in CI pipeline

PASS Tests\Visual\Help
✓ visual snapshot of help command output
Expand Down Expand Up @@ -715,5 +720,5 @@
✓ it is a test
✓ it uses correct parent class

Tests: 4 incompleted, 9 skipped, 475 passed
Tests: 4 incompleted, 9 skipped, 478 passed

23 changes: 23 additions & 0 deletions tests/Unit/Plugins/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Pest\Plugins\Context;

test('environment is set to CI when --ci option is used', function () {
$old_env = Context::getInstance()->env;

$plugin = new Context();

$plugin->handleArguments(['foo', '--ci', 'bar']);

expect(Context::getInstance()->env)->toBe(Context::ENV_CI);

Context::getInstance()->env = $old_env;
});

test('environment is set to Local when --ci option is not used', function () {
$plugin = new Context();

$plugin->handleArguments(['foo', 'bar', 'baz']);

expect(Context::getInstance()->env)->toBe(Context::ENV_LOCAL);
});
41 changes: 32 additions & 9 deletions tests/Unit/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

use Pest\Exceptions\DatasetMissing;
use Pest\Exceptions\TestAlreadyExist;
use Pest\Factories\TestCaseFactory;
use Pest\Plugins\Context;
use Pest\TestSuite;

it('does not allow to add the same test description twice', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$test = function () {};
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
})->throws(
TestAlreadyExist::class,
sprintf('A test with the description `%s` already exist in the filename `%s`.', 'foo', __FILE__),
Expand All @@ -17,17 +19,17 @@
it('alerts users about tests with arguments but no input', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$test = function (int $arg) {};
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
})->throws(
DatasetMissing::class,
sprintf("A test with the description '%s' has %d argument(s) ([%s]) and no dataset(s) provided in %s", 'foo', 1, 'int $arg', __FILE__),
);

it('can return an array of all test suite filenames', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
$test = function () {};
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'bar', $test));
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'bar', $test));

expect($testSuite->tests->getFilenames())->toEqual([
__FILE__,
Expand All @@ -36,16 +38,37 @@
});

it('can filter the test suite filenames to those with the only method', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
$test = function () {};

$testWithOnly = new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test);
$testWithOnly = new TestCaseFactory(__FILE__, 'foo', $test);
$testWithOnly->only = true;
$testSuite->tests->set($testWithOnly);

$testSuite->tests->set(new \Pest\Factories\TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test));
$testSuite->tests->set(new TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test));

expect($testSuite->tests->getFilenames())->toEqual([
__FILE__,
]);
});

it('does not filter the test suite filenames to those with the only method when working in CI pipeline', function () {
$old_env = Context::getInstance()->env;
Context::getInstance()->env = Context::ENV_CI;
$testSuite = TestSuite::getInstance(getcwd(), 'tests');

$test = function () {};

$testWithOnly = new TestCaseFactory(__FILE__, 'foo', $test);
$testWithOnly->only = true;
$testSuite->tests->set($testWithOnly);

$testSuite->tests->set(new TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test));

expect($testSuite->tests->getFilenames())->toEqual([
__FILE__,
'Baz/Bar/Boo.php',
]);

Context::getInstance()->env = $old_env;
});

0 comments on commit a6e34d2

Please sign in to comment.