8000 Fix env inheritance and added tests · symfony/symfony@38df47a · GitHub
[go: up one dir, main page]

Skip to content

Commit 38df47a

Browse files
committed
Fix env inheritance and added tests
1 parent f555c62 commit 38df47a

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Process
8989
*
9090
* @param string $commandline The command line to run
9191
* @param string $cwd The working directory
92-
* @param array $env The environment variables
92+
* @param array $env The environment variables or null to inherit
9393
* @param string $stdin The STDIN content
9494
* @param integer $timeout The timeout in seconds
9595
* @param array $options An array of options for proc_open

src/Symfony/Component/Process/ProcessBuilder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ public function getProcess()
110110
$script .= ' '.implode(' ', array_map('escapeshellarg', $arguments));
111111
}
112112

113-
$env = $this->inheritEnv && $_ENV ? $this->env + $_ENV : $this->env;
113+
$env = null;
114+
if ($this->inheritEnv) {
115+
$env = $_ENV ? $this->env + $_ENV : $this->env;
116+
} else {
117+
$env = $this->env;
118+
}
114119

115120
return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
116121
}

tests/Symfony/Tests/Component/Process/ProcessBuilderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ public function shouldInheritEnvironmentVars()
3232
$_ENV = $snapshot;
3333
}
3434

35+
/**
36+
* @test
37+
*/
38+
public function shouldInheritAndOverrideEnvironmentVars()
39+
{
40+
$snapshot = $_ENV;
41+
$_ENV = array('foo' => 'bar', 'bar' => 'baz');
42+
$expected = array('foo' => ' E6E6 foo', 'bar' => 'baz');
43+
44+
$pb = new ProcessBuilder();
45+
$pb->add('foo')->inheritEnvironmentVariables()
46+
->setEnv('foo', 'foo');
47+
$proc = $pb->getProcess();
48+
49+
$this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV');
50+
51+
$_ENV = $snapshot;
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function shouldNotInheritEnvironmentVarsByDefault()
58+
{
59+
$pb = new ProcessBuilder();
60+
$proc = $pb->add('foo')->getProcess();
61+
62+
$this->assertEquals(array(), $proc->getEnv());
63+
}
64+
3565
/**
3666
* @test
3767
*/

0 commit comments

Comments
 (0)
0