Skip to content

Commit

Permalink
ProcessBuilderTest: Don't depend on export shell builtin
Browse files Browse the repository at this point in the history
Shells other than Bash may not have an `export` builtin command
(in particular it doesn't exist on Windows). Shells are icky anyway.
Let's make our own in one line of PHP.

I'm doing this as the first patch towards making PHPUnit pass on
Windows, because trying to run a command that doesn't exist triggers
a really unpleasant bug in amphp/process 1.x on Windows, where it
hangs forever: amphp/process#71

(I had to move `$process->getStdout()` before `$process->join()`,
otherwise that would also hang forever. No idea why.)
  • Loading branch information
MatmaRex committed Feb 22, 2024
1 parent e1fe94f commit a39d251
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/Amp/Tests/Process/ProcessBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ProcessBuilderTest extends TestCase

public function testBuildProcess(): void
{
$process = ProcessBuilder::create(['export'])->build();
$process = $this->makeEnvDumpProcess()->build();
$pid = wait($process->start());
$exitCode = wait($process->join());
self::assertEquals(0, $exitCode);
Expand All @@ -22,28 +22,33 @@ public function testBuildProcess(): void
public function testDoesNotMergeEnvByDefaultWhenEnvVarsPassed(): void
{
putenv('FOO='.self::PARENT_PROCESS_ENV_VAR);
$process = ProcessBuilder::create(['export'])->env(['ENV'=> 'myenvvar'])->build();
$process = $this->makeEnvDumpProcess()->env(['ENV'=> 'myenvvar'])->build();
$pid = wait($process->start());
$exitCode = wait($process->join());
self::assertEquals(0, $exitCode);
/** @var string $out @phpstan-ignore-next-line */
$out = wait(buffer($process->getStdout()));
self::assertStringContainsString('myenvvar', $out);
self::assertStringNotContainsString(self::PARENT_PROCESS_ENV_VAR, $out);
$exitCode = wait($process->join());
self::assertEquals(0, $exitCode);
putenv('FOO');
}

public function testInheritsWhenInstructedToWhenEnvVarsPassed(): void
{
putenv('FOO='.self::PARENT_PROCESS_ENV_VAR);
$process = ProcessBuilder::create(['export'])->env(['ENV'=> 'myenvvar'])->mergeParentEnv()->build();
$process = $this->makeEnvDumpProcess()->env(['ENV'=> 'myenvvar'])->mergeParentEnv()->build();
$pid = wait($process->start());
$exitCode = wait($process->join());
self::assertEquals(0, $exitCode);
/** @var string $out @phpstan-ignore-next-line */
$out = wait(buffer($process->getStdout()));
self::assertStringContainsString('myenvvar', $out);
self::assertStringContainsString(self::PARENT_PROCESS_ENV_VAR, $out);
$exitCode = wait($process->join());
self::assertEquals(0, $exitCode);
putenv('FOO');
}

private function makeEnvDumpProcess(): ProcessBuilder
{
return ProcessBuilder::create([PHP_BINARY, '-r', 'var_dump(getenv());']);
}
}

0 comments on commit a39d251

Please sign in to comment.