Skip to content

Commit 10a109b

Browse files
authored
feat: Remove thecodingmachine/safe dependency (#19)
1 parent a94fc00 commit 10a109b

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

infection.json.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
"Fidry\\FileSystem\\Test\\FileSystemTestCase::tearDown"
1919
],
2020

21+
"FalseValue": {
22+
"ignore": [
23+
"Fidry\\FileSystem\\Test\\FileSystemTestCase::safeGetCurrentWorkingDirectory"
24+
]
25+
},
2126
"FunctionCallRemoval": false,
2227
"MethodCallRemoval": false,
2328
"PublicVisibility": false,

src/Test/FileSystemTestCase.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@
3838

3939
use Fidry\FileSystem\FS;
4040
use PHPUnit\Framework\TestCase;
41+
use RuntimeException;
4142
use Stringable;
4243
use function array_map;
4344
use function array_values;
45+
use function chdir;
4446
use function dirname;
47+
use function getcwd;
4548
use function is_array;
4649
use function iterator_to_array;
4750
use function natcasesort;
4851
use function natsort;
49-
use function Safe\getcwd;
52+
use function sprintf;
5053
use function str_replace;
5154
use const DIRECTORY_SEPARATOR;
5255

@@ -74,21 +77,21 @@ public static function tearDownAfterClass(): void
7477

7578
protected function setUp(): void
7679
{
77-
$this->cwd = getcwd();
80+
$this->cwd = self::safeGetCurrentWorkingDirectory();
7881
$this->tmp = FS::makeTmpDir(
7982
static::getTmpDirNamespace(),
8083
static::class,
8184
);
8285
static::$lastKnownTmpNamespace = dirname($this->tmp);
83-
chdir($this->tmp);
86+
self::safeChdir($this->tmp);
8487
}
8588

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

9093
if (!$wasSetupSkipped) {
91-
chdir($this->cwd);
94+
self::safeChdir($this->cwd);
9295
FS::remove($this->tmp);
9396
}
9497
}
@@ -114,4 +117,29 @@ final protected function normalizePaths(iterable $paths): array
114117

115118
return array_values($normalizedPaths);
116119
}
120+
121+
private static function safeChdir(string $directory): void
122+
{
123+
$chdirResult = chdir($directory);
124+
125+
if (!$chdirResult) {
126+
throw new RuntimeException(
127+
sprintf(
128+
'Could not change the current working directory to "%s".',
129+
$directory,
130+
),
131+
);
132+
}
133+
}
134+
135+
private static function safeGetCurrentWorkingDirectory(): string
136+
{
137+
$result = getcwd();
138+
139+
if (false === $result) {
140+
throw new RuntimeException('Could not get the current working directory.');
141+
}
142+
143+
return $result;
144+
}
117145
}

0 commit comments

Comments
 (0)