1
0
Fork 0

Merge pull request #3749 from alcohol/allow-setting-packagist-to-false-through-config-command

composer config repositories.packagist false
pull/3758/head
Jordi Boggiano 2015-02-17 19:41:03 +00:00
commit efcd1e4247
5 changed files with 158 additions and 85 deletions

View File

@ -237,48 +237,6 @@ EOT
$values = $input->getArgument('setting-value'); // what the user is trying to add/change $values = $input->getArgument('setting-value'); // what the user is trying to add/change
// handle repositories
if (preg_match('/^repos?(?:itories)?\.(.+)/', $settingKey, $matches)) {
if ($input->getOption('unset')) {
return $this->configSource->removeRepository($matches[1]);
}
if (2 !== count($values)) {
throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs http://bar.com');
}
return $this->configSource->addRepository($matches[1], array(
'type' => $values[0],
'url' => $values[1],
));
}
// handle github-oauth
if (preg_match('/^(github-oauth|http-basic)\.(.+)/', $settingKey, $matches)) {
if ($input->getOption('unset')) {
$this->authConfigSource->removeConfigSetting($matches[1].'.'.$matches[2]);
$this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
return;
}
if ($matches[1] === 'github-oauth') {
if (1 !== count($values)) {
throw new \RuntimeException('Too many arguments, expected only one token');
}
$this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
$this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], $values[0]);
} elseif ($matches[1] === 'http-basic') {
if (2 !== count($values)) {
throw new \RuntimeException('Expected two arguments (username, password), got '.count($values));
}
$this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
$this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], array('username' => $values[0], 'password' => $values[1]));
}
return;
}
$booleanValidator = function ($val) { return in_array($val, array('true', 'false', '1', '0'), true); }; $booleanValidator = function ($val) { return in_array($val, array('true', 'false', '1', '0'), true); };
$booleanNormalizer = function ($val) { return $val !== 'false' && (bool) $val; }; $booleanNormalizer = function ($val) { return $val !== 'false' && (bool) $val; };
@ -402,6 +360,55 @@ EOT
} }
} }
// handle repositories
if (preg_match('/^repos?(?:itories)?\.(.+)/', $settingKey, $matches)) {
if ($input->getOption('unset')) {
return $this->configSource->removeRepository($matches[1]);
}
if (2 === count($values)) {
return $this->configSource->addRepository($matches[1], array(
'type' => $values[0],
'url' => $values[1],
));
}
if (1 === count($values)) {
$bool = strtolower($values[0]);
if (true === $booleanValidator($bool) && false === $booleanNormalizer($bool)) {
return $this->configSource->addRepository($matches[1], false);
}
}
throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs http://bar.com');
}
// handle github-oauth
if (preg_match('/^(github-oauth|http-basic)\.(.+)/', $settingKey, $matches)) {
if ($input->getOption('unset')) {
$this->authConfigSource->removeConfigSetting($matches[1].'.'.$matches[2]);
$this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
return;
}
if ($matches[1] === 'github-oauth') {
if (1 !== count($values)) {
throw new \RuntimeException('Too many arguments, expected only one token');
}
$this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
$this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], $values[0]);
} elseif ($matches[1] === 'http-basic') {
if (2 !== count($values)) {
throw new \RuntimeException('Expected two arguments (username, password), got '.count($values));
}
$this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
$this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], array('username' => $values[0], 'password' => $values[1]));
}
return;
}
throw new \InvalidArgumentException('Setting '.$settingKey.' does not exist or is not supported by this command'); throw new \InvalidArgumentException('Setting '.$settingKey.' does not exist or is not supported by this command');
} }

View File

@ -0,0 +1,6 @@
{
"name": "my-vend/my-app",
"license": "MIT",
"repositories": {
}
}

View File

@ -0,0 +1,10 @@
{
"name": "my-vend/my-app",
"license": "MIT",
"repositories": {
"example_tld": {
"type": "git",
"url": "example.tld"
}
}
}

View File

@ -0,0 +1,7 @@
{
"name": "my-vend/my-app",
"license": "MIT",
"repositories": {
"packagist": false
}
}

View File

@ -18,6 +18,9 @@ use Composer\Util\Filesystem;
class JsonConfigSourceTest extends \PHPUnit_Framework_TestCase class JsonConfigSourceTest extends \PHPUnit_Framework_TestCase
{ {
/** @var Filesystem */
private $fs;
/** @var string */
private $workingDir; private $workingDir;
protected function fixturePath($name) protected function fixturePath($name)
@ -39,6 +42,89 @@ class JsonConfigSourceTest extends \PHPUnit_Framework_TestCase
} }
} }
public function testAddRepository()
{
$config = $this->workingDir.'/composer.json';
copy($this->fixturePath('composer-repositories.json'), $config);
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
$jsonConfigSource->addRepository('example_tld', array('type' => 'git', 'url' => 'example.tld'));
$this->assertFileEquals($this->fixturePath('config/config-with-exampletld-repository.json'), $config);
}
public function testRemoveRepository()
{
$config = $this->workingDir.'/composer.json';
copy($this->fixturePath('config/config-with-exampletld-repository.json'), $config);
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
$jsonConfigSource->removeRepository('example_tld');
$this->assertFileEquals($this->fixturePath('composer-repositories.json'), $config);
}
public function testAddPackagistRepositoryWithFalseValue()
{
$config = $this->workingDir.'/composer.json';
copy($this->fixturePath('composer-repositories.json'), $config);
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
$jsonConfigSource->addRepository('packagist', false);
$this->assertFileEquals($this->fixturePath('config/config-with-packagist-false.json'), $config);
}
public function testRemovePackagist()
{
$config = $this->workingDir.'/composer.json';
copy($this->fixturePath('config/config-with-packagist-false.json'), $config);
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
$jsonConfigSource->removeRepository('packagist');
$this->assertFileEquals($this->fixturePath('composer-repositories.json'), $config);
}
/**
* Test addLink()
*
* @param string $sourceFile Source file
* @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
* @param string $name Name
* @param string $value Value
* @param string $compareAgainst File to compare against after making changes
*
* @dataProvider provideAddLinkData
*/
public function testAddLink($sourceFile, $type, $name, $value, $compareAgainst)
{
$composerJson = $this->workingDir.'/composer.json';
copy($sourceFile, $composerJson);
$jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
$jsonConfigSource->addLink($type, $name, $value);
$this->assertFileEquals($compareAgainst, $composerJson);
}
/**
* Test removeLink()
*
* @param string $sourceFile Source file
* @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
* @param string $name Name
* @param string $compareAgainst File to compare against after making changes
*
* @dataProvider provideRemoveLinkData
*/
public function testRemoveLink($sourceFile, $type, $name, $compareAgainst)
{
$composerJson = $this->workingDir.'/composer.json';
copy($sourceFile, $composerJson);
$jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
$jsonConfigSource->removeLink($type, $name);
$this->assertFileEquals($compareAgainst, $composerJson);
}
protected function addLinkDataArguments($type, $name, $value, $fixtureBasename, $before) protected function addLinkDataArguments($type, $name, $value, $fixtureBasename, $before)
{ {
return array( return array(
@ -88,28 +174,6 @@ class JsonConfigSourceTest extends \PHPUnit_Framework_TestCase
); );
} }
/**
* Test addLink()
*
* @param string $sourceFile Source file
* @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
* @param string $name Name
* @param string $value Value
* @param string $compareAgainst File to compare against after making changes
*
* @dataProvider provideAddLinkData
*/
public function testAddLink($sourceFile, $type, $name, $value, $compareAgainst)
{
$composerJson = $this->workingDir.'/composer.json';
copy($sourceFile, $composerJson);
$jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
$jsonConfigSource->addLink($type, $name, $value);
$this->assertFileEquals($compareAgainst, $composerJson);
}
protected function removeLinkDataArguments($type, $name, $fixtureBasename, $after = null) protected function removeLinkDataArguments($type, $name, $fixtureBasename, $after = null)
{ {
return array( return array(
@ -156,25 +220,4 @@ class JsonConfigSourceTest extends \PHPUnit_Framework_TestCase
$this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-twoOfEverything', $twoOfEverything), $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-twoOfEverything', $twoOfEverything),
); );
} }
/**
* Test removeLink()
*
* @param string $sourceFile Source file
* @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
* @param string $name Name
* @param string $compareAgainst File to compare against after making changes
*
* @dataProvider provideRemoveLinkData
*/
public function testRemoveLink($sourceFile, $type, $name, $compareAgainst)
{
$composerJson = $this->workingDir.'/composer.json';
copy($sourceFile, $composerJson);
$jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
$jsonConfigSource->removeLink($type, $name);
$this->assertFileEquals($compareAgainst, $composerJson);
}
} }