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); + } } }