Skip to content

Commit

Permalink
CLI-1171: Updated tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Jura committed Oct 23, 2023
1 parent dc94e65 commit 6f87f31
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/Helpers/LocalMachineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/src/Misc/LocalMachineHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

}
14 changes: 11 additions & 3 deletions tests/phpunit/src/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);

Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 6f87f31

Please sign in to comment.