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
5 changes: 5 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"Fidry\\FileSystem\\Test\\FileSystemTestCase::tearDown"
],

"FalseValue": {
"ignore": [
"Fidry\\FileSystem\\Test\\FileSystemTestCase::safeGetCurrentWorkingDirectory"
]
},
"FunctionCallRemoval": false,
"MethodCallRemoval": false,
"PublicVisibility": false,
Expand Down
36 changes: 32 additions & 4 deletions src/Test/FileSystemTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@

use Fidry\FileSystem\FS;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Stringable;
use function array_map;
use function array_values;
use function chdir;
use function dirname;
use function getcwd;
use function is_array;
use function iterator_to_array;
use function natcasesort;
use function natsort;
use function Safe\getcwd;
use function sprintf;
use function str_replace;
use const DIRECTORY_SEPARATOR;

Expand Down Expand Up @@ -74,21 +77,21 @@ public static function tearDownAfterClass(): void

protected function setUp(): void
{
$this->cwd = getcwd();
$this->cwd = self::safeGetCurrentWorkingDirectory();
$this->tmp = FS::makeTmpDir(
static::getTmpDirNamespace(),
static::class,
);
static::$lastKnownTmpNamespace = dirname($this->tmp);
chdir($this->tmp);
self::safeChdir($this->tmp);
}

protected function tearDown(): void
{
$wasSetupSkipped = '' === $this->cwd && '' === $this->tmp;

if (!$wasSetupSkipped) {
chdir($this->cwd);
self::safeChdir($this->cwd);
FS::remove($this->tmp);
}
}
Expand All @@ -114,4 +117,29 @@ final protected function normalizePaths(iterable $paths): array

return array_values($normalizedPaths);
}

private static function safeChdir(string $directory): void
{
$chdirResult = chdir($directory);

if (!$chdirResult) {
throw new RuntimeException(
sprintf(
'Could not change the current working directory to "%s".',
$directory,
),
);
}
}

private static function safeGetCurrentWorkingDirectory(): string
{
$result = getcwd();

if (false === $result) {
throw new RuntimeException('Could not get the current working directory.');
}

return $result;
}
}
Loading