1
0
Fork 0

Merge pull request #3448 from cs278/config-env-test

Fix config tests when environment variables are set
pull/3178/merge
Nils Adermann 2014-11-21 15:57:16 +01:00
commit b21f2be651
2 changed files with 30 additions and 7 deletions

View File

@ -58,12 +58,17 @@ class Config
private $repositories; private $repositories;
private $configSource; private $configSource;
private $authConfigSource; 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 // load defaults
$this->config = static::$defaultConfig; $this->config = static::$defaultConfig;
$this->repositories = static::$defaultRepositories; $this->repositories = static::$defaultRepositories;
$this->useEnvironment = (bool) $useEnvironment;
} }
public function setConfigSource(ConfigSourceInterface $source) 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 // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
$env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); $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); $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $val);
return $val; return $val;
@ -201,7 +206,7 @@ class Config
return rtrim($this->process($this->config[$key]), '/\\'); return rtrim($this->process($this->config[$key]), '/\\');
case 'discard-changes': 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)) { if (!in_array($env, array('stash', 'true', 'false', '1', '0'), true)) {
throw new \RuntimeException( throw new \RuntimeException(
"Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash" "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]); return $config->get($match[1]);
}, $value); }, $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;
}
} }

View File

@ -21,7 +21,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
*/ */
public function testAddPackagistRepository($expected, $localConfig, $systemConfig = null) public function testAddPackagistRepository($expected, $localConfig, $systemConfig = null)
{ {
$config = new Config(); $config = new Config(false);
if ($systemConfig) { if ($systemConfig) {
$config->merge(array('repositories' => $systemConfig)); $config->merge(array('repositories' => $systemConfig));
} }
@ -102,7 +102,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
public function testMergeGithubOauth() 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('foo' => 'bar'))));
$config->merge(array('config' => array('github-oauth' => array('bar' => 'baz')))); $config->merge(array('config' => array('github-oauth' => array('bar' => 'baz'))));
@ -111,7 +111,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
public function testVarReplacement() public function testVarReplacement()
{ {
$config = new Config(); $config = new Config(false);
$config->merge(array('config' => array('a' => 'b', 'c' => '{$a}'))); $config->merge(array('config' => array('a' => 'b', 'c' => '{$a}')));
$config->merge(array('config' => array('bin-dir' => '$HOME', 'cache-dir' => '~/foo/'))); $config->merge(array('config' => array('bin-dir' => '$HOME', 'cache-dir' => '~/foo/')));
@ -123,7 +123,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
public function testOverrideGithubProtocols() 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', 'git'))));
$config->merge(array('config' => array('github-protocols' => array('https')))); $config->merge(array('config' => array('github-protocols' => array('https'))));