Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Cli/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Igancev\WorkReporter\Cli;

use Igancev\WorkReporter\Platform\HomeDirectory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -55,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$home = (string) getenv('HOME');
$home = HomeDirectory::resolve();
$configDir = str_replace('~', $home, self::DEFAULT_CONFIG_DIR);
$configPath = str_replace('~', $home, self::DEFAULT_CONFIG_PATH);

Expand Down
3 changes: 2 additions & 1 deletion src/Config/YamlConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Igancev\WorkReporter\Config\SourceConfig\SourcesConfig;
use Igancev\WorkReporter\Config\SourceConfig\SuperProductivityConfig;
use Igancev\WorkReporter\Destination\DestinationType;
use Igancev\WorkReporter\Platform\HomeDirectory;
use Igancev\WorkReporter\Source\SourceType;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
Expand All @@ -28,7 +29,7 @@ public function getConfig(): Config
return $this->config;
}

$path = str_replace("~", (string)getenv("HOME"), $this->configPath);
$path = str_replace("~", HomeDirectory::resolve(), $this->configPath);

if (!file_exists($path)) {
throw new ConfigException(sprintf(
Expand Down
37 changes: 37 additions & 0 deletions src/Platform/HomeDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Igancev\WorkReporter\Platform;

/**
* Cross-platform helper to resolve the current user's home directory.
*
* `HOME` is the POSIX convention used by Linux and macOS. On Windows the
* canonical equivalent is `USERPROFILE`, with `HOMEDRIVE` + `HOMEPATH` as a
* last-resort fallback when shells / containers strip `USERPROFILE`. Reading
* `HOME` directly on Windows usually returns an empty string and silently
* resolves `~`-prefixed config paths to the filesystem root.
*/
final class HomeDirectory
{
public static function resolve(): string
{
if (PHP_OS_FAMILY === 'Windows') {
$userProfile = (string) getenv('USERPROFILE');
if ($userProfile !== '') {
return $userProfile;
}

$drive = (string) getenv('HOMEDRIVE');
$path = (string) getenv('HOMEPATH');
if ($drive !== '' || $path !== '') {
return $drive . $path;
}

return '';
}

return (string) getenv('HOME');
}
}
114 changes: 114 additions & 0 deletions tests/Unit/Platform/HomeDirectoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Platform;

use Igancev\WorkReporter\Platform\HomeDirectory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(HomeDirectory::class)]
final class HomeDirectoryTest extends TestCase
{
private string|false $originalHome;
private string|false $originalUserProfile;
private string|false $originalHomeDrive;
private string|false $originalHomePath;

protected function setUp(): void
{
// Arrange
$this->originalHome = getenv('HOME');
$this->originalUserProfile = getenv('USERPROFILE');
$this->originalHomeDrive = getenv('HOMEDRIVE');
$this->originalHomePath = getenv('HOMEPATH');
}

protected function tearDown(): void
{
$this->restoreEnv('HOME', $this->originalHome);
$this->restoreEnv('USERPROFILE', $this->originalUserProfile);
$this->restoreEnv('HOMEDRIVE', $this->originalHomeDrive);
$this->restoreEnv('HOMEPATH', $this->originalHomePath);
}

public function testResolveReturnsHomeOnPosix(): void
{
if (PHP_OS_FAMILY === 'Windows') {
self::markTestSkipped('POSIX-only path; Windows uses USERPROFILE.');
}

// Arrange
putenv('HOME=/home/posix-user');

// Act
$home = HomeDirectory::resolve();

// Assert
self::assertSame('/home/posix-user', $home);
}

public function testResolvePrefersUserProfileOnWindows(): void
{
if (PHP_OS_FAMILY !== 'Windows') {
self::markTestSkipped('Windows-only path; POSIX uses HOME.');
}

// Arrange
putenv('USERPROFILE=C:\\Users\\winuser');
putenv('HOMEDRIVE=Z:');
putenv('HOMEPATH=\\Z-path');

// Act
$home = HomeDirectory::resolve();

// Assert
self::assertSame('C:\\Users\\winuser', $home);
}

public function testResolveFallsBackToHomeDrivePathOnWindowsWhenUserProfileEmpty(): void
{
if (PHP_OS_FAMILY !== 'Windows') {
self::markTestSkipped('Windows-only fallback path.');
}

// Arrange
putenv('USERPROFILE');
putenv('HOMEDRIVE=C:');
putenv('HOMEPATH=\\Users\\fallback');

// Act
$home = HomeDirectory::resolve();

// Assert
self::assertSame('C:\\Users\\fallback', $home);
}

public function testResolveReturnsEmptyStringWhenEnvUnset(): void
{
// Arrange
if (PHP_OS_FAMILY === 'Windows') {
putenv('USERPROFILE');
putenv('HOMEDRIVE');
putenv('HOMEPATH');
} else {
putenv('HOME');
}

// Act
$home = HomeDirectory::resolve();

// Assert
self::assertSame('', $home);
}

private function restoreEnv(string $name, string|false $original): void
{
if ($original === false) {
putenv($name);
return;
}
putenv($name . '=' . $original);
}
}
Loading