test: keep the developer's Composer variables out of the packaging testsUnverified
A COMPOSER or COMPOSER_VENDOR_DIR set in the shell running PHPUnit pointed the offline install at another manifest or vendor dir, failing the tests.
51f7215 parent: 158d086 modified
tests/PackagerTest.php +10 -1 | @@ -174,7 +174,7 @@ final class PackagerTest extends TestCase | ||
| 174 | 174 | 'COMPOSER_DISABLE_NETWORK' => '1', |
| 175 | 175 | 'COMPOSER_NO_AUDIT' => '1', |
| 176 | 176 | ]; |
| 177 | - $process = proc_open([\PHP_BINARY, '-d', 'sys_temp_dir='.$this->dir.'/tmp', '-r', $code], [0 => ['file', '/dev/null', 'r'], 1 => ['file', '/dev/null', 'w'], 2 => ['pipe', 'w']], $pipes, null, $env + getenv()); | |
| 177 | + $process = proc_open([\PHP_BINARY, '-d', 'sys_temp_dir='.$this->dir.'/tmp', '-r', $code], [0 => ['file', '/dev/null', 'r'], 1 => ['file', '/dev/null', 'w'], 2 => ['pipe', 'w']], $pipes, null, $env + $this->inheritedEnv()); | |
| 178 | 178 | \assert(false !== $process); |
| 179 | 179 | $log = (string) stream_get_contents($pipes[2]); |
| 180 | 180 | fclose($pipes[2]); |
| @@ -182,6 +182,15 @@ final class PackagerTest extends TestCase | ||
| 182 | 182 | return [proc_close($process), $log]; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | + /** | |
| 186 | + * @return array<string, string> | |
| 187 | + */ | |
| 188 | + private function inheritedEnv(): array | |
| 189 | + { | |
| 190 | + // Hermetic: a COMPOSER, COMPOSER_VENDOR_DIR... of the developer's shell would point the install elsewhere. | |
| 191 | + return array_filter(getenv(), static fn (string $name): bool => !str_starts_with($name, 'COMPOSER'), \ARRAY_FILTER_USE_KEY); | |
| 192 | + } | |
| 193 | + | |
| 185 | 194 | /** |
| 186 | 195 | * @return array<mixed> |
| 187 | 196 | */ |
| @@ -174,7 +174,7 @@ final class PackagerTest extends TestCase | |||
| 174 | 'COMPOSER_DISABLE_NETWORK' => '1', | 174 | 'COMPOSER_DISABLE_NETWORK' => '1', |
| 175 | 'COMPOSER_NO_AUDIT' => '1', | 175 | 'COMPOSER_NO_AUDIT' => '1', |
| 176 | ]; | 176 | ]; |
| 177 | - $process = proc_open([\PHP_BINARY, '-d', 'sys_temp_dir='.$this->dir.'/tmp', '-r', $code], [0 => ['file', '/dev/null', 'r'], 1 => ['file', '/dev/null', 'w'], 2 => ['pipe', 'w']], $pipes, null, $env + getenv()); | 177 | + $process = proc_open([\PHP_BINARY, '-d', 'sys_temp_dir='.$this->dir.'/tmp', '-r', $code], [0 => ['file', '/dev/null', 'r'], 1 => ['file', '/dev/null', 'w'], 2 => ['pipe', 'w']], $pipes, null, $env + $this->inheritedEnv()); |
| 178 | \assert(false !== $process); | 178 | \assert(false !== $process); |
| 179 | $log = (string) stream_get_contents($pipes[2]); | 179 | $log = (string) stream_get_contents($pipes[2]); |
| 180 | fclose($pipes[2]); | 180 | fclose($pipes[2]); |
| @@ -182,6 +182,15 @@ final class PackagerTest extends TestCase | |||
| 182 | return [proc_close($process), $log]; | 182 | return [proc_close($process), $log]; |
| 183 | } | 183 | } |
| 184 | 184 | ||
| 185 | + /** | ||
| 186 | + * @return array<string, string> | ||
| 187 | + */ | ||
| 188 | + private function inheritedEnv(): array | ||
| 189 | + { | ||
| 190 | + // Hermetic: a COMPOSER, COMPOSER_VENDOR_DIR... of the developer's shell would point the install elsewhere. | ||
| 191 | + return array_filter(getenv(), static fn (string $name): bool => !str_starts_with($name, 'COMPOSER'), \ARRAY_FILTER_USE_KEY); | ||
| 192 | + } | ||
| 193 | + | ||
| 185 | /** | 194 | /** |
| 186 | * @return array<mixed> | 195 | * @return array<mixed> |
| 187 | */ | 196 | */ |