mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
fix(parallel): load the autoloader of the parent process in workers (#2058)
* fix(parallel): load the autoloader of the parent process in workers * test(parallel): cover the worker autoloader handoff end to end --------- Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
@@ -192,6 +192,7 @@ use Symfony\Component\Console\Output\ConsoleOutput;
|
||||
|
||||
include_once $autoloadPath;
|
||||
|
||||
$GLOBALS['_composer_autoload_path'] = $autoloadPath;
|
||||
$GLOBALS['_composer_bin_dir'] ??= dirname($autoloadPath).'/bin';
|
||||
$rootPath = ComposerProject::rootPath($autoloadPath);
|
||||
|
||||
|
||||
@@ -43,9 +43,11 @@ $bootPest = (static function (string $rootPath): void {
|
||||
'testdox-columns:',
|
||||
'testdox-summary',
|
||||
'phpunit-argv:',
|
||||
'composer-autoload:',
|
||||
]);
|
||||
|
||||
$composerAutoloadFiles = [
|
||||
...(isset($getopt['composer-autoload']) && is_string($getopt['composer-autoload']) ? [$getopt['composer-autoload']] : []),
|
||||
dirname(__DIR__, 3).DIRECTORY_SEPARATOR.'autoload.php',
|
||||
dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php',
|
||||
dirname(__DIR__).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php',
|
||||
|
||||
@@ -156,6 +156,10 @@ final class WrapperRunner implements RunnerInterface
|
||||
$parameters[] = $wrapper;
|
||||
$parameters[] = '--test-directory='.TestSuite::getInstance()->testPath;
|
||||
|
||||
if (is_string($GLOBALS['_composer_autoload_path'] ?? null)) {
|
||||
$parameters[] = '--composer-autoload='.$GLOBALS['_composer_autoload_path'];
|
||||
}
|
||||
|
||||
$this->parameters = $parameters;
|
||||
$this->codeCoverageFilterRegistry = new CodeCoverageFilterRegistry;
|
||||
}
|
||||
|
||||
@@ -2646,6 +2646,9 @@
|
||||
✓ parallel counts a shared issue only once across recycled workers with ('notice', 'shares notice', 'notice')
|
||||
✓ parallel combines stop-on counts from different workers and cleans up their files
|
||||
|
||||
PASS Tests\Visual\ParallelWorkerAutoload
|
||||
✓ loads the autoloader of the parent process in a parallel worker
|
||||
|
||||
PASS Tests\Visual\Shard
|
||||
✓ an empty shard runs no tests with dataset "sequential"
|
||||
✓ an empty shard runs no tests with dataset "parallel"
|
||||
@@ -2690,4 +2693,4 @@
|
||||
✓ pass with dataset with ('my-datas-set-value')
|
||||
✓ within describe → pass with dataset with ('my-datas-set-value')
|
||||
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1977 passed (4474 assertions)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1978 passed (4476 assertions)
|
||||
@@ -423,29 +423,12 @@ final class Project
|
||||
|
||||
public function scaffoldVendor(string $directory): void
|
||||
{
|
||||
$pestRoot = dirname(__DIR__, 3);
|
||||
$pest = $directory.'/vendor/pestphp/pest';
|
||||
$this->scaffoldPest($directory.'/vendor/pestphp/pest', $directory.'/vendor', '/pestphp/pest/src');
|
||||
}
|
||||
|
||||
foreach (['src', 'overrides', 'resources', 'stubs'] as $tree) {
|
||||
self::mirror($pestRoot.'/'.$tree, $pest.'/'.$tree);
|
||||
}
|
||||
|
||||
foreach (['pest', 'worker.php'] as $binary) {
|
||||
self::mirror($pestRoot.'/bin/'.$binary, $pest.'/bin/'.$binary);
|
||||
}
|
||||
|
||||
self::mirror($pestRoot.'/composer.json', $pest.'/composer.json');
|
||||
|
||||
file_put_contents($directory.'/vendor/autoload.php', sprintf(
|
||||
"<?php\n\n\$loader = require %s;\n\$loader->addPsr4('Pest\\\\', __DIR__.'/pestphp/pest/src', true);\n\nreturn \$loader;\n",
|
||||
var_export($pestRoot.'/vendor/autoload.php', true),
|
||||
));
|
||||
|
||||
if (! is_dir($directory.'/vendor/bin') && ! @mkdir($directory.'/vendor/bin', 0755, true)) {
|
||||
throw new RuntimeException(sprintf('Unable to create [%s].', $directory.'/vendor/bin'));
|
||||
}
|
||||
|
||||
self::mirror($pestRoot.'/vendor/pest-plugins.json', $directory.'/vendor/pest-plugins.json');
|
||||
public function scaffoldCheckout(string $directory): void
|
||||
{
|
||||
$this->scaffoldPest($directory, $directory.'/vendor', '/../src');
|
||||
}
|
||||
|
||||
public function destroy(): void
|
||||
@@ -512,6 +495,33 @@ final class Project
|
||||
return $project;
|
||||
}
|
||||
|
||||
private function scaffoldPest(string $pest, string $vendor, string $sourceFromVendor): void
|
||||
{
|
||||
$pestRoot = dirname(__DIR__, 3);
|
||||
|
||||
foreach (['src', 'overrides', 'resources', 'stubs'] as $tree) {
|
||||
self::mirror($pestRoot.'/'.$tree, $pest.'/'.$tree);
|
||||
}
|
||||
|
||||
foreach (['pest', 'worker.php'] as $binary) {
|
||||
self::mirror($pestRoot.'/bin/'.$binary, $pest.'/bin/'.$binary);
|
||||
}
|
||||
|
||||
self::mirror($pestRoot.'/composer.json', $pest.'/composer.json');
|
||||
|
||||
if (! is_dir($vendor.'/bin') && ! @mkdir($vendor.'/bin', 0755, true)) {
|
||||
throw new RuntimeException(sprintf('Unable to create [%s].', $vendor.'/bin'));
|
||||
}
|
||||
|
||||
file_put_contents($vendor.'/autoload.php', sprintf(
|
||||
"<?php\n\n\$loader = require %s;\n\$loader->addPsr4('Pest\\\\', __DIR__.%s, true);\n\nreturn \$loader;\n",
|
||||
var_export($pestRoot.'/vendor/autoload.php', true),
|
||||
var_export($sourceFromVendor, true),
|
||||
));
|
||||
|
||||
self::mirror($pestRoot.'/vendor/pest-plugins.json', $vendor.'/pest-plugins.json');
|
||||
}
|
||||
|
||||
private static function mirror(string $from, string $to): void
|
||||
{
|
||||
if (is_dir($from)) {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Tests\Fixtures\Tia\Project;
|
||||
|
||||
test('loads the autoloader of the parent process in a parallel worker', function (): void {
|
||||
$project = Project::withoutGit();
|
||||
|
||||
try {
|
||||
$checkout = $project->path('checkout');
|
||||
$project->scaffoldCheckout($checkout);
|
||||
|
||||
$project->write('vendor/autoload.php', <<<'DECOY'
|
||||
<?php
|
||||
|
||||
fwrite(STDERR, 'decoy autoloader');
|
||||
|
||||
exit(1);
|
||||
DECOY);
|
||||
|
||||
$project->write('checkout/phpunit.xml', <<<'CONFIGURATION'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache"/>
|
||||
CONFIGURATION);
|
||||
|
||||
$test = 'checkout/tests/WorkerAutoloadTest.php';
|
||||
$project->write($test, <<<'FIXTURE'
|
||||
<?php
|
||||
|
||||
test('runs in a worker with the autoloader of the checkout', function (): void {
|
||||
expect($_SERVER['PEST_PARALLEL'] ?? null)->toBe('1')
|
||||
->and(realpath((new ReflectionClass(Pest\TestSuite::class))->getFileName()))->toStartWith(dirname(__DIR__).DIRECTORY_SEPARATOR.'src');
|
||||
});
|
||||
FIXTURE);
|
||||
|
||||
$process = new Process(
|
||||
[PHP_BINARY, $checkout.'/bin/pest', '--configuration='.$project->path('checkout/phpunit.xml'), '--colors=never', $project->path($test), '--parallel', '--processes=2'],
|
||||
$checkout,
|
||||
['PARATEST' => '0', 'PAO_DISABLE' => '1'],
|
||||
);
|
||||
|
||||
expect($process->run())->toBe(0, $process->getOutput().$process->getErrorOutput())
|
||||
->and($process->getOutput())->toContain('1 passed (2 assertions)');
|
||||
} finally {
|
||||
Project::destroyAll();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user