From ef92a062059b91c5951874183168e96d9bcdb224 Mon Sep 17 00:00:00 2001 From: Rob Bast Date: Mon, 16 Feb 2015 15:58:58 +0100 Subject: [PATCH 1/2] composer config repositories.packagist false closes #3728, closes #3713, closes #3290, not sure about #3025 --- src/Composer/Command/ConfigCommand.php | 90 ++++++------ .../Fixtures/composer-repositories.json | 6 + .../config-with-exampletld-repository.json | 10 ++ .../config/config-with-packagist-false.json | 7 + .../Test/Config/JsonConfigSourceTest.php | 129 ++++++++++++------ 5 files changed, 157 insertions(+), 85 deletions(-) create mode 100644 tests/Composer/Test/Config/Fixtures/composer-repositories.json create mode 100644 tests/Composer/Test/Config/Fixtures/config/config-with-exampletld-repository.json create mode 100644 tests/Composer/Test/Config/Fixtures/config/config-with-packagist-false.json diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index e21c99c8a..1ebb5bcc0 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -237,48 +237,6 @@ EOT $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); }; $booleanNormalizer = function ($val) { return $val !== 'false' && (bool) $val; }; @@ -402,6 +360,54 @@ 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) && $booleanValidator($values[0])) { + if (false === $booleanNormalizer($values[0])) { + 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'); } diff --git a/tests/Composer/Test/Config/Fixtures/composer-repositories.json b/tests/Composer/Test/Config/Fixtures/composer-repositories.json new file mode 100644 index 000000000..fc303ec38 --- /dev/null +++ b/tests/Composer/Test/Config/Fixtures/composer-repositories.json @@ -0,0 +1,6 @@ +{ + "name": "my-vend/my-app", + "license": "MIT", + "repositories": { + } +} diff --git a/tests/Composer/Test/Config/Fixtures/config/config-with-exampletld-repository.json b/tests/Composer/Test/Config/Fixtures/config/config-with-exampletld-repository.json new file mode 100644 index 000000000..085c875c5 --- /dev/null +++ b/tests/Composer/Test/Config/Fixtures/config/config-with-exampletld-repository.json @@ -0,0 +1,10 @@ +{ + "name": "my-vend/my-app", + "license": "MIT", + "repositories": { + "example_tld": { + "type": "git", + "url": "example.tld" + } + } +} diff --git a/tests/Composer/Test/Config/Fixtures/config/config-with-packagist-false.json b/tests/Composer/Test/Config/Fixtures/config/config-with-packagist-false.json new file mode 100644 index 000000000..c4d884527 --- /dev/null +++ b/tests/Composer/Test/Config/Fixtures/config/config-with-packagist-false.json @@ -0,0 +1,7 @@ +{ + "name": "my-vend/my-app", + "license": "MIT", + "repositories": { + "packagist": false + } +} diff --git a/tests/Composer/Test/Config/JsonConfigSourceTest.php b/tests/Composer/Test/Config/JsonConfigSourceTest.php index b7a3b592c..529532263 100644 --- a/tests/Composer/Test/Config/JsonConfigSourceTest.php +++ b/tests/Composer/Test/Config/JsonConfigSourceTest.php @@ -18,6 +18,9 @@ use Composer\Util\Filesystem; class JsonConfigSourceTest extends \PHPUnit_Framework_TestCase { + /** @var Filesystem */ + private $fs; + /** @var string */ private $workingDir; 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) { 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) { return array( @@ -156,25 +220,4 @@ class JsonConfigSourceTest extends \PHPUnit_Framework_TestCase $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); - } } From 47d75f77d5cdbe63b2fe2bfbe9db4712ba98a812 Mon Sep 17 00:00:00 2001 From: Rob Bast Date: Mon, 16 Feb 2015 16:54:52 +0100 Subject: [PATCH 2/2] Let's be gentle and allow FALSE too --- src/Composer/Command/ConfigCommand.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 1ebb5bcc0..9773ea885 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -373,8 +373,9 @@ EOT )); } - if (1 === count($values) && $booleanValidator($values[0])) { - if (false === $booleanNormalizer($values[0])) { + if (1 === count($values)) { + $bool = strtolower($values[0]); + if (true === $booleanValidator($bool) && false === $booleanNormalizer($bool)) { return $this->configSource->addRepository($matches[1], false); } }