From dc94e65c447bdce716e0d1cfea97f53604ec5164 Mon Sep 17 00:00:00 2001 From: Jura Khrapunov Date: Fri, 20 Oct 2023 19:27:10 -0400 Subject: [PATCH 1/2] CLI-1171: updated env variable testing condition --- src/Helpers/LocalMachineHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/LocalMachineHelper.php b/src/Helpers/LocalMachineHelper.php index e6fd68db5..56c286822 100644 --- a/src/Helpers/LocalMachineHelper.php +++ b/src/Helpers/LocalMachineHelper.php @@ -223,7 +223,7 @@ public static function getHomeDir(): string { $home = getenv('HOME'); if (!$home) { $system = ''; - if (getenv('MSYSTEM') !== NULL) { + if (getenv('MSYSTEM')) { $system = strtoupper(substr(getenv('MSYSTEM'), 0, 4)); } if ($system !== 'MING') { From 6f87f31bd0d3945d88d4f587c236dad6811bae54 Mon Sep 17 00:00:00 2001 From: Jura Khrapunov Date: Mon, 23 Oct 2023 17:08:23 -0400 Subject: [PATCH 2/2] CLI-1171: Updated tests coverage --- src/Helpers/LocalMachineHelper.php | 4 +-- .../src/Misc/LocalMachineHelperTest.php | 35 +++++++++++++++++++ tests/phpunit/src/TestBase.php | 14 ++++++-- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/Helpers/LocalMachineHelper.php b/src/Helpers/LocalMachineHelper.php index 56c286822..61d3874c2 100644 --- a/src/Helpers/LocalMachineHelper.php +++ b/src/Helpers/LocalMachineHelper.php @@ -215,9 +215,7 @@ private function fixFilename(string $filename): string { /** * Returns the appropriate home directory. * - * Adapted from Ads Package Manager by Ed Reel. - * - * @url https://github.com/uberhacker/tpm + * @see https://github.com/pantheon-systems/terminus/blob/1d89e20dd388dc08979a1bc52dfd142b26c03dcf/src/Config/DefaultsConfig.php#L99 */ public static function getHomeDir(): string { $home = getenv('HOME'); diff --git a/tests/phpunit/src/Misc/LocalMachineHelperTest.php b/tests/phpunit/src/Misc/LocalMachineHelperTest.php index 13d77b689..13407a942 100644 --- a/tests/phpunit/src/Misc/LocalMachineHelperTest.php +++ b/tests/phpunit/src/Misc/LocalMachineHelperTest.php @@ -4,6 +4,8 @@ namespace Acquia\Cli\Tests\Misc; +use Acquia\Cli\Exception\AcquiaCliException; +use Acquia\Cli\Helpers\LocalMachineHelper; use Acquia\Cli\Tests\TestBase; use Symfony\Component\Console\Output\BufferedOutput; @@ -61,4 +63,37 @@ public function testCommandExists(): void { $this->assertIsBool($exists); } + public function testHomeDirWindowsCmd(): void { + self::setEnvVars([ + 'HOMEPATH' => 'something', + ]); + self::unsetEnvVars([ + 'MSYSTEM', + 'HOME', + ]); + $home = LocalMachineHelper::getHomeDir(); + $this->assertEquals('something', $home); + } + + public function testHomeDirWindowsMsys2(): void { + self::setEnvVars([ + 'HOMEPATH' => 'something', + 'MSYSTEM' => 'MSYS2', + ]); + self::unsetEnvVars(['HOME']); + $home = LocalMachineHelper::getHomeDir(); + $this->assertEquals('something', $home); + } + + /** + * I don't know why, but apparently Ming is unsupported ¯\_(ツ)_/¯. + */ + public function testHomeDirWindowsMing(): void { + self::setEnvVars(['MSYSTEM' => 'MING']); + self::unsetEnvVars(['HOME']); + $this->expectException(AcquiaCliException::class); + $this->expectExceptionMessage('Could not determine $HOME directory. Ensure $HOME is set in your shell.'); + LocalMachineHelper::getHomeDir(); + } + } diff --git a/tests/phpunit/src/TestBase.php b/tests/phpunit/src/TestBase.php index e88bdb6a8..78d5de29f 100644 --- a/tests/phpunit/src/TestBase.php +++ b/tests/phpunit/src/TestBase.php @@ -164,7 +164,10 @@ public function setupFsFixture(): void { * This method is called before each test. */ protected function setUp(): void { - putenv('COLUMNS=85'); + self::setEnvVars([ + 'COLUMNS' => '85', + 'HOME' => '/home/test', + ]); $this->output = new BufferedOutput(); $this->input = new ArrayInput([]); @@ -241,9 +244,14 @@ public static function setEnvVars(array $envVars): void { } } - public static function unsetEnvVars(mixed $envVars): void { + public static function unsetEnvVars(array $envVars): void { foreach ($envVars as $key => $value) { - putenv($key); + if (is_int($key)) { + putenv($value); + } + else { + putenv($key); + } } }