From 8c5f2dbb97c84a404e228159d3faef6b8293e6ee Mon Sep 17 00:00:00 2001 From: Mohamed Hubail Date: Thu, 22 Aug 2024 11:38:16 +0300 Subject: [PATCH] Add `GlobalCommandTest.php` (#12073) * Add `GlobalCommandTest.php` - `testGlobal` to check `COMPOSER_HOME` is followed correctly + check `COMPOSER` is unset. - `testNotCreateHome` to test handling invalid `COMPOSER_HOME`. * Add error string for non obvious test case * Clean up env vars and minor code style changes --------- Co-authored-by: Jordi Boggiano --- .../Test/Command/GlobalCommandTest.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/Composer/Test/Command/GlobalCommandTest.php diff --git a/tests/Composer/Test/Command/GlobalCommandTest.php b/tests/Composer/Test/Command/GlobalCommandTest.php new file mode 100644 index 000000000..1c3b32a36 --- /dev/null +++ b/tests/Composer/Test/Command/GlobalCommandTest.php @@ -0,0 +1,80 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Command; + +use Composer\Test\TestCase; +use Composer\Util\Platform; + +class GlobalCommandTest extends TestCase +{ + public function tearDown(): void + { + parent::tearDown(); + Platform::clearEnv('COMPOSER_HOME'); + Platform::clearEnv('COMPOSER'); + } + + public function testGlobal(): void + { + $script = '@php -r \'echo getenv("COMPOSER") . PHP_EOL;\''; + $fakeComposer = 'TMP_COMPOSER.JSON'; + $composerHome = $this->initTempComposer( + [ + "scripts" => [ + "test-script" => $script, + ], + ] + ); + + Platform::putEnv('COMPOSER_HOME', $composerHome); + Platform::putEnv('COMPOSER', $fakeComposer); + + $dir = self::getUniqueTmpDirectory(); + chdir($dir); + + $appTester = $this->getApplicationTester(); + $appTester->run([ + 'command' => 'global', + 'command-name' => 'test-script', + '--no-interaction' => true, + ]); + + $display = $appTester->getDisplay(true); + + self::assertStringContainsString( + 'Changed current directory to ' . $composerHome, + $display + ); + self::assertStringContainsString($script, $display); + self::assertStringNotContainsString($fakeComposer, $display, '$COMPOSER is not unset by global command'); + } + + public function testCannotCreateHome(): void + { + $dir = self::getUniqueTmpDirectory(); + $filename = $dir . '/file'; + file_put_contents($filename, ''); + + Platform::putEnv('COMPOSER_HOME', $filename); + + self::expectException(\RuntimeException::class); + $this->expectExceptionMessage($filename . ' exists and is not a directory.'); + + $appTester = $this->getApplicationTester(); + $appTester->run([ + 'command' => 'global', + 'command-name' => 'test-script', + '--no-interaction' => true, + ]); + } +}