Skip to content

Commit

Permalink
Add workaround for PHPUnit process isolation issues for PHPUnit <6.5 …
Browse files Browse the repository at this point in the history
…as well, fixes #10387
  • Loading branch information
Seldaek committed Dec 30, 2021
1 parent 0a85c3f commit 2a731ef
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Composer/Installer/BinaryInstaller.php
Expand Up @@ -264,13 +264,20 @@ protected function generateUnixyProxyCode($bin, $link)
$binPathExported = $this->filesystem->findShortestPathCode($link, $bin, false, true);
$streamProxyCode = $streamHint = '';
$globalsCode = '$GLOBALS[\'_composer_bin_dir\'] = __DIR__;'."\n";
$phpunitHack1 = $phpunitHack2 = '';
// Don't expose autoload path when vendor dir was not set in custom installers
if ($this->vendorDir) {
$globalsCode .= '$GLOBALS[\'_composer_autoload_path\'] = ' . $this->filesystem->findShortestPathCode($link, $this->vendorDir . '/autoload.php', false, true).";\n";
}
// Add workaround for PHPUnit process isolation on PHPUnit 6+
// Add workaround for PHPUnit process isolation
if ($this->filesystem->normalizePath($bin) === $this->filesystem->normalizePath($this->vendorDir.'/phpunit/phpunit/phpunit')) {
// workaround issue on PHPUnit 6.5+ running on PHP 8+
$globalsCode .= '$GLOBALS[\'__PHPUNIT_ISOLATION_EXCLUDE_LIST\'] = $GLOBALS[\'__PHPUNIT_ISOLATION_BLACKLIST\'] = array(realpath('.$binPathExported.'));'."\n";
// workaround issue on all PHPUnit versions running on PHP <8
$phpunitHack1 = "'phpvfs1://'.";

This comment has been minimized.

Copy link
@johnstevenson

johnstevenson Dec 30, 2021

Member

Is this phpvfs1:// prefix just to differentiate errors invoking phpunit?

$phpunitHack2 = '
$data = str_replace(\'__DIR__\', var_export(dirname($this->realpath), true), $data);
$data = str_replace(\'__FILE__\', var_export($this->realpath, true), $data);';

This comment has been minimized.

Copy link
@johnstevenson

johnstevenson Dec 30, 2021

Member

This is causing me parse errors on Windows and Wsl:Ubuntu, for example:

PHP Parse error:  syntax error, unexpected ''p' (T_ENCAPSED_AND_WHITESPACE), expecting ']' in phpvfs1://C:\Users\John\projects\bug\vendor\phpunit\phpunit\phpunit on line 89
PHP Parse error:  syntax error, unexpected end of file in phpvfs1:///mnt/c/Users/John/projects/bug/vendor/phpunit/phpunit/phpunit on line 89

But if I capture the stream_wrapper output (from stream_read) and save it to a file, that file runs perfectly.

This comment has been minimized.

Copy link
@johnstevenson

johnstevenson Dec 30, 2021

Member

Okay, deleted my previous comment as I can see what you're doing now. It would seem that the errors are only on PHP7.4 upwards, so maybe you could only do this for PHPUnit on < PHP 7.4?

This comment has been minimized.

Copy link
@Seldaek

Seldaek Dec 30, 2021

Author Member

The point of this is setting $opened_path back to something with phpvfs1 prefix to trigger https://github.com/sebastianbergmann/phpunit/blob/master/src/Util/GlobalState.php#L92-L94

I am not entirely sure what is causing the parse errors, but indeed we only need this for PHPUnit <6.5, so it's unlikely that people would use those in combination with PHP 7.4 I suppose. Also please join the party in #10387 if you have more info to share so we keep it all in one place.

}
if (trim($match[0]) !== '<?php') {
$streamHint = ' using a stream wrapper to prevent the shebang from being output on PHP<8'."\n *";
Expand All @@ -284,13 +291,15 @@ final class BinProxyWrapper
{
private \$handle;
private \$position;
private \$realpath;
public function stream_open(\$path, \$mode, \$options, &\$opened_path)
{
// get rid of composer-bin-proxy:// prefix for __FILE__ & __DIR__ resolution
\$opened_path = substr(\$path, 21);
\$opened_path = realpath(\$opened_path) ?: \$opened_path;
\$this->handle = fopen(\$opened_path, \$mode);
\$this->realpath = realpath(\$opened_path) ?: \$opened_path;
\$opened_path = $phpunitHack1\$this->realpath;
\$this->handle = fopen(\$this->realpath, \$mode);
\$this->position = 0;
// remove all traces of this stream wrapper once it has been used
Expand All @@ -305,7 +314,7 @@ public function stream_read(\$count)
if (\$this->position === 0) {
\$data = preg_replace('{^#!.*\\r?\\n}', '', \$data);
}
}$phpunitHack2
\$this->position += strlen(\$data);
Expand Down

0 comments on commit 2a731ef

Please sign in to comment.