From 725a4fd638c7f339cdf5f1daaff0899bdbca24d3 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 21 Nov 2014 10:07:56 +0000 Subject: [PATCH 1/4] Backup/restore Composer environment variables The tests could fail if an environment variable replaces an expected value. --- tests/Composer/Test/ConfigTest.php | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Composer/Test/ConfigTest.php b/tests/Composer/Test/ConfigTest.php index 705fe78e0..47ee08718 100644 --- a/tests/Composer/Test/ConfigTest.php +++ b/tests/Composer/Test/ConfigTest.php @@ -16,6 +16,39 @@ use Composer\Config; class ConfigTest extends \PHPUnit_Framework_TestCase { + private static $envVars = array( + 'VENDOR_DIR', + 'BIN_DIR', + 'PROCESS_TIMEOUT', + 'CACHE_DIR', + 'CACHE_FILES_DIR', + 'CACHE_REPO_DIR', + 'CACHE_VCS_DIR', + 'DISCARD_CHANGES', + ); + + private $envVarValues = array(); + + public function setUp() + { + foreach (self::$envVars as $var) { + $var = 'COMPOSER_' . $var; + + if ($value = getenv($var)) { + $this->envVarValues[$var] = $value; + + putenv($var . '='); + } + } + } + + public function tearDown() + { + foreach ($this->envVarValues as $var => $value) { + putenv(sprintf('%s=%s', $var, $value)); + } + } + /** * @dataProvider dataAddPackagistRepository */ From 5a56ebd54511b6acfaadd0c7008bfc3155c2c18d Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 21 Nov 2014 10:09:21 +0000 Subject: [PATCH 2/4] Revert "Backup/restore Composer environment variables" This reverts commit 725a4fd638c7f339cdf5f1daaff0899bdbca24d3. --- tests/Composer/Test/ConfigTest.php | 33 ------------------------------ 1 file changed, 33 deletions(-) diff --git a/tests/Composer/Test/ConfigTest.php b/tests/Composer/Test/ConfigTest.php index 47ee08718..705fe78e0 100644 --- a/tests/Composer/Test/ConfigTest.php +++ b/tests/Composer/Test/ConfigTest.php @@ -16,39 +16,6 @@ use Composer\Config; class ConfigTest extends \PHPUnit_Framework_TestCase { - private static $envVars = array( - 'VENDOR_DIR', - 'BIN_DIR', - 'PROCESS_TIMEOUT', - 'CACHE_DIR', - 'CACHE_FILES_DIR', - 'CACHE_REPO_DIR', - 'CACHE_VCS_DIR', - 'DISCARD_CHANGES', - ); - - private $envVarValues = array(); - - public function setUp() - { - foreach (self::$envVars as $var) { - $var = 'COMPOSER_' . $var; - - if ($value = getenv($var)) { - $this->envVarValues[$var] = $value; - - putenv($var . '='); - } - } - } - - public function tearDown() - { - foreach ($this->envVarValues as $var => $value) { - putenv(sprintf('%s=%s', $var, $value)); - } - } - /** * @dataProvider dataAddPackagistRepository */ From 86b5938cdb2b0bd43161a3f88f72108e053d572c Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 21 Nov 2014 10:14:40 +0000 Subject: [PATCH 3/4] Allow reading of COMPOSER_ environment variables to be disabled --- src/Composer/Config.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Composer/Config.php b/src/Composer/Config.php index e740bb8c7..bd5d86895 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -58,12 +58,17 @@ class Config private $repositories; private $configSource; private $authConfigSource; + private $useEnvironment; - public function __construct() + /** + * @param boolean $useEnvironment Use COMPOSER_ environment variables to replace config settings + */ + public function __construct($useEnvironment = true) { // load defaults $this->config = static::$defaultConfig; $this->repositories = static::$defaultRepositories; + $this->useEnvironment = (bool) $useEnvironment; } public function setConfigSource(ConfigSourceInterface $source) @@ -159,7 +164,7 @@ class Config // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); - $val = rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\'); + $val = rtrim($this->process($this->getComposerEnv($env) ?: $this->config[$key]), '/\\'); $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $val); return $val; @@ -201,7 +206,7 @@ class Config return rtrim($this->process($this->config[$key]), '/\\'); case 'discard-changes': - if ($env = getenv('COMPOSER_DISCARD_CHANGES')) { + if ($env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES')) { if (!in_array($env, array('stash', 'true', 'false', '1', '0'), true)) { throw new \RuntimeException( "Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash" @@ -288,4 +293,22 @@ class Config return $config->get($match[1]); }, $value); } + + /** + * Reads the value of a Composer environment variable + * + * This should be used to read COMPOSER_ environment variables + * that overload config values. + * + * @param string $var + * @return string|boolean + */ + private function getComposerEnv($var) + { + if ($this->useEnvironment) { + return getenv($var); + } + + return false; + } } From c819bd7e705f7937552e2fee374fdf254b992919 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 21 Nov 2014 10:15:17 +0000 Subject: [PATCH 4/4] Update config tests to not use environment variables --- tests/Composer/Test/ConfigTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Composer/Test/ConfigTest.php b/tests/Composer/Test/ConfigTest.php index 705fe78e0..521fb634b 100644 --- a/tests/Composer/Test/ConfigTest.php +++ b/tests/Composer/Test/ConfigTest.php @@ -21,7 +21,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ public function testAddPackagistRepository($expected, $localConfig, $systemConfig = null) { - $config = new Config(); + $config = new Config(false); if ($systemConfig) { $config->merge(array('repositories' => $systemConfig)); } @@ -102,7 +102,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase public function testMergeGithubOauth() { - $config = new Config(); + $config = new Config(false); $config->merge(array('config' => array('github-oauth' => array('foo' => 'bar')))); $config->merge(array('config' => array('github-oauth' => array('bar' => 'baz')))); @@ -111,7 +111,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase public function testVarReplacement() { - $config = new Config(); + $config = new Config(false); $config->merge(array('config' => array('a' => 'b', 'c' => '{$a}'))); $config->merge(array('config' => array('bin-dir' => '$HOME', 'cache-dir' => '~/foo/'))); @@ -123,7 +123,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase public function testOverrideGithubProtocols() { - $config = new Config(); + $config = new Config(false); $config->merge(array('config' => array('github-protocols' => array('https', 'git')))); $config->merge(array('config' => array('github-protocols' => array('https'))));