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

Use temporary file (instead of STDOUT) to communicate result from child process to parent process #5290

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/Framework/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,13 @@ public function runInSeparateProcess(TestCase $test, bool $runEntireClass, bool
$includePath = var_export(get_include_path(), true);
// must do these fixes because TestCaseMethod.tpl has unserialize('{data}') in it, and we can't break BC
// the lines above used to use addcslashes() rather than var_export(), which breaks null byte escape sequences
$data = "'." . $data . ".'";
$dataName = "'.(" . $dataName . ").'";
$dependencyInput = "'." . $dependencyInput . ".'";
$includePath = "'." . $includePath . ".'";
$offset = hrtime();
$serializedConfiguration = $this->saveConfigurationForChildProcess();
$data = "'." . $data . ".'";
$dataName = "'.(" . $dataName . ").'";
$dependencyInput = "'." . $dependencyInput . ".'";
$includePath = "'." . $includePath . ".'";
$offset = hrtime();
$serializedConfiguration = $this->saveConfigurationForChildProcess();
$fileWithSerializedChildResult = tempnam(sys_get_temp_dir(), 'phpunit_');

$var = [
'bootstrap' => $bootstrap,
Expand All @@ -327,6 +328,7 @@ public function runInSeparateProcess(TestCase $test, bool $runEntireClass, bool
'offsetSeconds' => $offset[0],
'offsetNanoseconds' => $offset[1],
'serializedConfiguration' => $serializedConfiguration,
'fileWithSerializedChildResult' => $fileWithSerializedChildResult,
];

if (!$runEntireClass) {
Expand All @@ -336,7 +338,7 @@ public function runInSeparateProcess(TestCase $test, bool $runEntireClass, bool
$template->setVar($var);

$php = AbstractPhpProcess::factory();
$php->runTestJob($template->render(), $test);
$php->runTestJob($template->render(), $test, $fileWithSerializedChildResult);

@unlink($serializedConfiguration);
}
Expand Down
118 changes: 49 additions & 69 deletions src/Util/PHP/AbstractPhpProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
use function array_merge;
use function assert;
use function escapeshellarg;
use function file_get_contents;
use function ini_get_all;
use function restore_error_handler;
use function set_error_handler;
use function str_replace;
use function str_starts_with;
use function substr;
use function is_array;
use function trim;
use function unlink;
use function unserialize;
use ErrorException;
use PHPUnit\Event\Code\TestMethodBuilder;
use PHPUnit\Event\Code\ThrowableBuilder;
use PHPUnit\Event\Facade;
Expand Down Expand Up @@ -159,14 +156,14 @@
* @throws MoreThanOneDataSetFromDataProviderException
* @throws NoPreviousThrowableException
*/
public function runTestJob(string $job, Test $test): void
public function runTestJob(string $job, Test $test, string $fileWithSerializedChildResult): void
{
$_result = $this->runJob($job);
$result = $this->runJob($job);

$this->processChildResult(
$test,
$_result['stdout'],
$_result['stderr']
$result['stderr'],
$fileWithSerializedChildResult
);
}

Expand Down Expand Up @@ -238,18 +235,17 @@
}

/**
* @throws \PHPUnit\Runner\Exception
* @throws Exception
* @throws MoreThanOneDataSetFromDataProviderException
* @throws NoPreviousThrowableException
*/
private function processChildResult(Test $test, string $stdout, string $stderr): void
private function processChildResult(Test $test, string $stderr, string $fileWithSerializedChildResult): void
{
assert($test instanceof TestCase);

if (!empty($stderr)) {
$exception = new Exception(trim($stderr));

assert($test instanceof TestCase);

Facade::emitter()->testErrored(
TestMethodBuilder::fromTestCase($test),
ThrowableBuilder::from($exception)
Expand All @@ -258,75 +254,59 @@
return;
}

set_error_handler(
/**
* @throws ErrorException
*/
static function (int $errno, string $errstr, string $errfile, int $errline): never
{
throw new ErrorException($errstr, $errno, $errno, $errfile, $errline);
}
);
$serializedChildResult = @file_get_contents($fileWithSerializedChildResult);

try {
if (str_starts_with($stdout, "#!/usr/bin/env php\n")) {
$stdout = substr($stdout, 19);
}
if (!$serializedChildResult) {
$this->testErrored($test);

$childResult = unserialize(str_replace("#!/usr/bin/env php\n", '', $stdout));
restore_error_handler();
return;
}

if ($childResult === false) {
$exception = new AssertionFailedError('Test was run in child process and ended unexpectedly');
@unlink($fileWithSerializedChildResult);

assert($test instanceof TestCase);
$childResult = @unserialize($serializedChildResult);

Facade::emitter()->testErrored(
TestMethodBuilder::fromTestCase($test),
ThrowableBuilder::from($exception)
);
if (!is_array($childResult)) {
$this->testErrored($test);

Check warning on line 270 in src/Util/PHP/AbstractPhpProcess.php

View check run for this annotation

Codecov / codecov/patch

src/Util/PHP/AbstractPhpProcess.php#L270

Added line #L270 was not covered by tests

Facade::emitter()->testFinished(
TestMethodBuilder::fromTestCase($test),
0
);
}
} catch (ErrorException $e) {
restore_error_handler();
$childResult = false;
return;

Check warning on line 272 in src/Util/PHP/AbstractPhpProcess.php

View check run for this annotation

Codecov / codecov/patch

src/Util/PHP/AbstractPhpProcess.php#L272

Added line #L272 was not covered by tests
}

$exception = new Exception(trim($stdout), 0, $e);
Facade::instance()->forward($childResult['events']);
PassedTests::instance()->import($childResult['passedTests']);

assert($test instanceof TestCase);
$test->setResult($childResult['testResult']);
$test->addToAssertionCount($childResult['numAssertions']);

Facade::emitter()->testErrored(
TestMethodBuilder::fromTestCase($test),
ThrowableBuilder::from($exception)
if (CodeCoverage::instance()->isActive() &&
$childResult['codeCoverage'] instanceof \SebastianBergmann\CodeCoverage\CodeCoverage) {
CodeCoverage::instance()->codeCoverage()->merge(
$childResult['codeCoverage']

Check warning on line 284 in src/Util/PHP/AbstractPhpProcess.php

View check run for this annotation

Codecov / codecov/patch

src/Util/PHP/AbstractPhpProcess.php#L283-L284

Added lines #L283 - L284 were not covered by tests
);
}

if ($childResult !== false) {
if (!empty($childResult['output'])) {
$output = $childResult['output'];
}

Facade::instance()->forward($childResult['events']);
PassedTests::instance()->import($childResult['passedTests']);

assert($test instanceof TestCase);
if (!empty($childResult['output'])) {
print $childResult['output'];
}
}

$test->setResult($childResult['testResult']);
$test->addToAssertionCount($childResult['numAssertions']);
/**
* @throws Exception
* @throws MoreThanOneDataSetFromDataProviderException
* @throws NoPreviousThrowableException
*/
private function testErrored(TestCase $test): void
{
$exception = new AssertionFailedError('Test was run in child process and ended unexpectedly');

if (CodeCoverage::instance()->isActive() && $childResult['codeCoverage'] instanceof \SebastianBergmann\CodeCoverage\CodeCoverage) {
CodeCoverage::instance()->codeCoverage()->merge(
$childResult['codeCoverage']
);
}
}
Facade::emitter()->testErrored(
TestMethodBuilder::fromTestCase($test),
ThrowableBuilder::from($exception)
);

if (!empty($output)) {
print $output;
}
Facade::emitter()->testFinished(
TestMethodBuilder::fromTestCase($test),
0
);
}
}
33 changes: 14 additions & 19 deletions src/Util/PHP/Template/TestCaseClass.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ set_include_path('{include_path}');
$composerAutoload = {composerAutoload};
$phar = {phar};

ob_start();

if ($composerAutoload) {
require_once $composerAutoload;

Expand Down Expand Up @@ -52,18 +50,12 @@ function __phpunit_run_isolated_test()
$test->setDependencyInput(unserialize('{dependencyInput}'));
$test->setInIsolation(true);

ob_end_clean();

$test->run();

$output = '';

if (!$test->hasExpectationOnOutput()) {
$output = $test->output();
}

ini_set('xdebug.scream', '0');

$output = $test->hasUnexpectedOutput() ? $test->output() : '';

// Not every STDOUT target stream is rewindable
@rewind(STDOUT);

Expand All @@ -77,15 +69,18 @@ function __phpunit_run_isolated_test()
}
}

print serialize(
[
'testResult' => $test->result(),
'codeCoverage' => {collectCodeCoverageInformation} ? CodeCoverage::instance()->codeCoverage() : null,
'numAssertions' => $test->numberOfAssertionsPerformed(),
'output' => $output,
'events' => $dispatcher->flush(),
'passedTests' => PassedTests::instance()
]
file_put_contents(
'{fileWithSerializedChildResult}',
serialize(
[
'testResult' => $test->result(),
'output' => $output,
'codeCoverage' => {collectCodeCoverageInformation} ? CodeCoverage::instance()->codeCoverage() : null,
'numAssertions' => $test->numberOfAssertionsPerformed(),
'events' => $dispatcher->flush(),
'passedTests' => PassedTests::instance()
]
)
);
}

Expand Down
33 changes: 14 additions & 19 deletions src/Util/PHP/Template/TestCaseMethod.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ set_include_path('{include_path}');
$composerAutoload = {composerAutoload};
$phar = {phar};

ob_start();

if ($composerAutoload) {
require_once $composerAutoload;

Expand Down Expand Up @@ -53,18 +51,12 @@ function __phpunit_run_isolated_test()
$test->setDependencyInput(unserialize('{dependencyInput}'));
$test->setInIsolation(true);

ob_end_clean();

$test->run();

$output = '';

if (!$test->hasExpectationOnOutput()) {
$output = $test->output();
}

ini_set('xdebug.scream', '0');

$output = $test->hasUnexpectedOutput() ? $test->output() : '';

// Not every STDOUT target stream is rewindable
@rewind(STDOUT);

Expand All @@ -78,15 +70,18 @@ function __phpunit_run_isolated_test()
}
}

print serialize(
[
'testResult' => $test->result(),
'codeCoverage' => {collectCodeCoverageInformation} ? CodeCoverage::instance()->codeCoverage() : null,
'numAssertions' => $test->numberOfAssertionsPerformed(),
'output' => $output,
'events' => $dispatcher->flush(),
'passedTests' => PassedTests::instance()
]
file_put_contents(
'{fileWithSerializedChildResult}',
serialize(
[
'testResult' => $test->result(),
'output' => $output,
'codeCoverage' => {collectCodeCoverageInformation} ? CodeCoverage::instance()->codeCoverage() : null,
'numAssertions' => $test->numberOfAssertionsPerformed(),
'events' => $dispatcher->flush(),
'passedTests' => PassedTests::instance()
]
)
);
}

Expand Down