Merge pull request #61 from dimitrioskarvounaris/windows-gitbash

Fixes autoloading, plugins and tests on Windows
This commit is contained in:
Nuno Maduro
2020-06-05 20:01:55 +02:00
committed by GitHub
13 changed files with 55 additions and 32 deletions

View File

@ -30,7 +30,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, zip
extensions: dom, mbstring, zip
tools: prestissimo
coverage: pcov

View File

@ -29,7 +29,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, zip
extensions: dom, mbstring, zip
coverage: none
- name: Install Composer dependencies

View File

@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php declare(strict_types=1);
use NunoMaduro\Collision\Provider;
use Pest\Actions\ValidatesEnvironment;
use Pest\Console\Command;
use Pest\TestSuite;
@ -8,18 +9,18 @@ use Symfony\Component\Console\Output\ConsoleOutput;
(static function () {
// Used when Pest is required using composer.
$vendorPath = realpath(__DIR__ . '/../../../../vendor/autoload.php');
$vendorPath = dirname(__DIR__, 4) . '/vendor/autoload.php';
// Used when Pest maintainers are running Pest tests.
$localPath = realpath(__DIR__ . '/../vendor/autoload.php');
$localPath = dirname(__DIR__) . '/vendor/autoload.php';
if ($vendorPath) {
if (file_exists($vendorPath)) {
include_once $vendorPath;
} else {
include_once $localPath;
}
(new \NunoMaduro\Collision\Provider)->register();
(new Provider())->register();
$rootPath = getcwd();

View File

@ -67,9 +67,9 @@
"lint": "rector process src && php-cs-fixer fix -v",
"test:lint": "php-cs-fixer fix -v --dry-run && rector process src --dry-run",
"test:types": "phpstan analyse --ansi",
"test:unit": "bin/pest --colors=always --exclude-group=integration",
"test:integration": "bin/pest --colors=always --group=integration",
"test:integration:snapshots": "REBUILD_SNAPSHOTS=true bin/pest --colors=always",
"test:unit": "php bin/pest --colors=always --exclude-group=integration",
"test:integration": "php bin/pest --colors=always --group=integration",
"test:integration:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always",
"test": [
"@test:lint",
"@test:types",

View File

@ -158,16 +158,25 @@ final class TestCaseFactory
*/
public function makeClassFromFilename(string $filename): string
{
if ('\\' === DIRECTORY_SEPARATOR) {
// In case Windows, strtolower drive name, like in UsesCall.
$filename = (string) preg_replace_callback('~^(?P<drive>[a-z]+:\\\)~i', function ($match): string {
return strtolower($match['drive']);
}, $filename);
}
$filename = (string) realpath($filename);
$rootPath = TestSuite::getInstance()->rootPath;
$relativePath = str_replace($rootPath . DIRECTORY_SEPARATOR, '', $filename);
$relativePath = dirname(ucfirst($relativePath)) . DIRECTORY_SEPARATOR . basename($relativePath, '.php');
$relativePath = str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath);
// Strip out any %-encoded octets.
$relativePath = (string) preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $relativePath);
// Limit to A-Z, a-z, 0-9, '_', '-'.
$relativePath = (string) preg_replace('/[^A-Za-z0-9.\/]/', '', $relativePath);
$classFQN = 'P\\' . basename(ucfirst(str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath)), '.php');
$relativePath = (string) preg_replace('/[^A-Za-z0-9.\\\]/', '', $relativePath);
$classFQN = 'P\\' . $relativePath;
if (class_exists($classFQN)) {
return $classFQN;
}

View File

@ -59,7 +59,17 @@ final class UsesCall
public function in(string ...$targets): void
{
$targets = array_map(function ($path): string {
return $path[0] === DIRECTORY_SEPARATOR
$startChar = DIRECTORY_SEPARATOR;
if ('\\' === DIRECTORY_SEPARATOR || preg_match('~\A[A-Z]:(?![^/\\\\])~i', $path) > 0) {
$path = (string) preg_replace_callback('~^(?P<drive>[a-z]+:\\\)~i', function ($match): string {
return strtolower($match['drive']);
}, $path);
$startChar = strtolower((string) preg_replace('~^([a-z]+:\\\).*$~i', '$1', __DIR__));
}
return 0 === strpos($path, $startChar)
? $path
: implode(DIRECTORY_SEPARATOR, [
dirname($this->filename),
@ -68,12 +78,12 @@ final class UsesCall
}, $targets);
$this->targets = array_map(function ($target): string {
$realTarget = realpath($target);
if ($realTarget === false) {
$isValid = is_dir($target) || file_exists($target);
if (!$isValid) {
throw new InvalidUsesPath($target);
}
return $realTarget;
return (string) realpath($target);
}, $targets);
}

View File

@ -24,7 +24,7 @@ final class Backtrace
$current = null;
foreach (debug_backtrace() as $trace) {
if (Str::endsWith($trace[self::FILE], 'vendor/phpunit/phpunit/src/Util/FileLoader.php')) {
if (Str::endsWith($trace[self::FILE], (string) realpath('vendor/phpunit/phpunit/src/Util/FileLoader.php'))) {
break;
}

View File

@ -83,7 +83,7 @@ final class TestSuite
$this->afterEach = new AfterEachRepository();
$this->afterAll = new AfterAllRepository();
$this->rootPath = $rootPath;
$this->rootPath = (string) realpath($rootPath);
}
/**

View File

@ -81,10 +81,10 @@
✓ higher order message test
PASS Tests\Fixtures\DirectoryWithTests\ExampleTest
✓ it example
✓ it example 1
PASS Tests\Fixtures\ExampleTest
✓ it example
✓ it example 2
PASS Tests\PHPUnit\CustomTestCase\UsesPerDirectory
✓ closure was bound to CustomTestCase
@ -135,4 +135,4 @@
s visual snapshot of test suite on success
Tests: 6 skipped, 71 passed
Time: 2.96s
Time: 2.89s

View File

@ -1,3 +1,3 @@
<?php
it('example')->assertTrue(true);
it('example 1')->assertTrue(true);

View File

@ -1,3 +1,3 @@
<?php
it('example')->assertTrue(true);
it('example 2')->assertTrue(true);

View File

@ -3,7 +3,7 @@
use Symfony\Component\Process\Process;
$run = function (string $target) {
$process = new Process(['./bin/pest', $target], dirname(__DIR__, 2));
$process = new Process(['php', 'bin/pest', $target], dirname(__DIR__, 2));
$process->run();
@ -13,7 +13,7 @@ $run = function (string $target) {
test('allows to run a single test', function () use ($run) {
assertStringContainsString(<<<EOF
PASS Tests\Fixtures\DirectoryWithTests\ExampleTest
it example
it example 1
Tests: 1 passed
EOF, $run('tests/Fixtures/DirectoryWithTests/ExampleTest.php'));
@ -22,10 +22,10 @@ EOF, $run('tests/Fixtures/DirectoryWithTests/ExampleTest.php'));
test('allows to run a directory', function () use ($run) {
assertStringContainsString(<<<EOF
PASS Tests\Fixtures\DirectoryWithTests\ExampleTest
it example
it example 1
PASS Tests\Fixtures\ExampleTest
it example
it example 2
Tests: 2 passed
EOF, $run('tests/Fixtures'));
@ -33,6 +33,7 @@ EOF, $run('tests/Fixtures'));
it('has ascii chars (decorated printer)', function () {
$process = new Process([
'php',
'./bin/pest',
'tests/Fixtures/DirectoryWithTests/ExampleTest.php',
], dirname(__DIR__, 2));
@ -41,7 +42,7 @@ it('has ascii chars (decorated printer)', function () {
$output = $process->getOutput();
assertStringContainsString(<<<EOF
\e[30;42;1m PASS \e[39;49;22m\e[39m Tests\Fixtures\DirectoryWithTests\ExampleTest\e[39m
\e[32;1m✓\e[39;22m\e[39m \e[2mit example\e[22m\e[39m
\e[32;1m✓\e[39;22m\e[39m \e[2mit example 1\e[22m\e[39m
\e[37;1mTests: \e[39;22m\e[32;1m1 passed\e[39;22m
EOF, $output);
@ -49,6 +50,7 @@ EOF, $output);
it('disable decorating printer when colors is set to never', function () {
$process = new Process([
'php',
'./bin/pest',
'--colors=never',
'tests/Fixtures/DirectoryWithTests/ExampleTest.php',
@ -58,7 +60,7 @@ it('disable decorating printer when colors is set to never', function () {
assertStringContainsString(<<<EOF
PASS Tests\Fixtures\DirectoryWithTests\ExampleTest
\e[2mit example\e[22m
\e[2mit example 1\e[22m
Tests: 1 passed
EOF, $output);

View File

@ -9,7 +9,7 @@ test('visual snapshot of test suite on success', function () {
]);
$output = function () use ($testsPath) {
$process = (new Symfony\Component\Process\Process(['./bin/pest'], dirname($testsPath), ['EXCLUDE' => 'integration', 'REBUILD_SNAPSHOTS' => false]));
$process = (new Symfony\Component\Process\Process(['php', 'bin/pest'], dirname($testsPath), ['EXCLUDE' => 'integration', 'REBUILD_SNAPSHOTS' => false]));
$process->run();
@ -24,4 +24,5 @@ test('visual snapshot of test suite on success', function () {
array_pop($output);
assertStringContainsString(implode("\n", $output), file_get_contents($snapshot));
}
})->skip(!getenv('REBUILD_SNAPSHOTS') && getenv('EXCLUDE'));
})->skip(!getenv('REBUILD_SNAPSHOTS') && getenv('EXCLUDE'))
->skip(PHP_OS_FAMILY === 'Windows', 'File sorting algorithm causes different test order on Windows');