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

make test directory configurable #283

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions bin/pest
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ use Symfony\Component\Console\Output\OutputInterface;
(new Provider())->register();

// get $rootPath based on $autoloadPath
$rootPath = dirname($autoloadPath, 2);
$rootPath = dirname($autoloadPath, 2);
$argv = new ArgvInput();

$testSuite = TestSuite::getInstance($rootPath);
$testSuite = TestSuite::getInstance($rootPath, $argv->getParameterOption('--test-directory', 'tests'));

$isDecorated = (new ArgvInput())->getParameterOption('--colors', 'always') !== 'never';
$isDecorated = $argv->getParameterOption('--colors', 'always') !== 'never';
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, $isDecorated);

$container = Container::getInstance();
Expand All @@ -41,5 +42,14 @@ use Symfony\Component\Console\Output\OutputInterface;

ValidatesEnvironment::in($testSuite);

// lets remove any arguments that PHPUnit does not understand
if ($argv->hasParameterOption('--test-directory')) {
foreach ($_SERVER['argv'] as $key => $value) {
if (strpos($value, '--test-directory') !== false) {
unset($_SERVER['argv'][$key]);
}
}
}

exit($container->get(Command::class)->run($_SERVER['argv']));
})();
3 changes: 2 additions & 1 deletion src/Actions/LoadStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Pest\Actions;

use Pest\Support\Str;
use function Pest\testDirectory;
use PHPUnit\TextUI\Configuration\Configuration;
use PHPUnit\Util\FileLoader;
use RecursiveDirectoryIterator;
Expand Down Expand Up @@ -33,7 +34,7 @@ final class LoadStructure
*/
public static function in(string $rootPath): void
{
$testsPath = $rootPath . DIRECTORY_SEPARATOR . 'tests';
$testsPath = $rootPath . DIRECTORY_SEPARATOR . testDirectory();

$load = function ($filename): bool {
return file_exists($filename) && (bool) FileLoader::checkAndLoad($filename);
Expand Down
3 changes: 2 additions & 1 deletion src/Laravel/Commands/PestDatasetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Pest\Exceptions\InvalidConsoleArgument;
use function Pest\testDirectory;

/**
* @internal
Expand Down Expand Up @@ -36,7 +37,7 @@ public function handle(): void
/** @var string $name */
$name = $this->argument('name');

$relativePath = sprintf('tests/Datasets/%s.php', ucfirst($name));
$relativePath = sprintf(testDirectory('Datasets/%s.php'), ucfirst($name));

/* @phpstan-ignore-next-line */
$target = base_path($relativePath);
Expand Down
3 changes: 2 additions & 1 deletion src/Laravel/Commands/PestInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\File;
use Pest\Console\Thanks;
use Pest\Exceptions\InvalidConsoleArgument;
use function Pest\testDirectory;

/**
* @internal
Expand All @@ -34,7 +35,7 @@ final class PestInstallCommand extends Command
public function handle(): void
{
/* @phpstan-ignore-next-line */
$pest = base_path('tests/Pest.php');
$pest = base_path(testDirectory('Pest.php'));
$stubs = 'stubs/Laravel';

if (File::exists($pest)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Laravel/Commands/PestTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\File;
use Pest\Exceptions\InvalidConsoleArgument;
use Pest\Support\Str;
use function Pest\testDirectory;

/**
* @internal
Expand Down Expand Up @@ -38,7 +39,7 @@ public function handle(): void

$type = ((bool) $this->option('unit')) ? 'Unit' : (((bool) $this->option('dusk')) ? 'Browser' : 'Feature');

$relativePath = sprintf('tests/%s/%s.php',
$relativePath = sprintf(testDirectory('%s/%s.php'),
$type,
ucfirst($name)
);
Expand Down
5 changes: 5 additions & 0 deletions src/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ function version(): string
{
return '1.0.4';
}

function testDirectory(string $file = ''): string
{
return TestSuite::getInstance()->testPath . $file;
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Plugin
public static function uses(string ...$traits): void
{
self::$callables[] = function () use ($traits): void {
uses(...$traits)->in(TestSuite::getInstance()->rootPath . DIRECTORY_SEPARATOR . 'tests');
uses(...$traits)->in(TestSuite::getInstance()->rootPath . DIRECTORY_SEPARATOR . testDirectory());
};
}
}
16 changes: 12 additions & 4 deletions src/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ final class TestSuite
*/
public $rootPath;

/**
* Holds the test path.
*
* @var string
*/
public $testPath;

/**
* Holds an instance of the test suite.
*
Expand All @@ -76,7 +83,7 @@ final class TestSuite
/**
* Creates a new instance of the test suite.
*/
public function __construct(string $rootPath)
public function __construct(string $rootPath, string $testPath)
{
$this->beforeAll = new BeforeAllRepository();
$this->beforeEach = new BeforeEachRepository();
Expand All @@ -85,15 +92,16 @@ public function __construct(string $rootPath)
$this->afterAll = new AfterAllRepository();

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

/**
* Returns the current instance of the test suite.
*/
public static function getInstance(string $rootPath = null): TestSuite
public static function getInstance(string $rootPath = null, string $testPath = null): TestSuite
{
if (is_string($rootPath)) {
self::$instance = new TestSuite($rootPath);
if (is_string($rootPath) && is_string($testPath)) {
self::$instance = new TestSuite($rootPath, $testPath);

foreach (Plugin::$callables as $callable) {
$callable();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Actions/AddsTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$testSuite->addTest($phpUnitTestCase);
expect($testSuite->tests())->toHaveCount(1);

AddsTests::to($testSuite, new \Pest\TestSuite(getcwd()));
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd(), 'tests'));
expect($testSuite->tests())->toHaveCount(1);
});

Expand All @@ -27,6 +27,6 @@
$warningTestCase = new WarningTestCase('No tests found in class "Pest\TestCase".');
$testSuite->addTest($warningTestCase);

AddsTests::to($testSuite, new \Pest\TestSuite(getcwd()));
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd(), 'tests'));
expect($testSuite->tests())->toHaveCount(0);
});
1 change: 1 addition & 0 deletions tests/Unit/Support/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

it('can resolve builtin value types', function () {
$this->container->add('rootPath', getcwd());
$this->container->add('testPath', 'tests');

$instance = $this->container->get(TestSuite::class);
expect($instance)->toBeInstanceOf(TestSuite::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Pest\TestSuite;

it('does not allow to add the same test description twice', function () {
$testSuite = new TestSuite(getcwd());
$testSuite = new TestSuite(getcwd(), 'tests');
$test = function () {};
$testSuite->tests->set(new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test));
$this->expectException(TestAlreadyExist::class);
Expand Down