Skip to content

Commit 3678b20

Browse files
committed
Finder: improved exception when directory is missing
1 parent a0d2118 commit 3678b20

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Utils/Finder.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
345345
if ($this->maxDepth >= 0 && count($subdirs) > $this->maxDepth) {
346346
return;
347347
} elseif (!is_dir($dir)) {
348-
throw new Nette\InvalidStateException("Directory '$dir' not found.");
348+
throw new Nette\InvalidStateException(sprintf("Directory '%s' does not exist.", rtrim($dir, '/\\')));
349349
}
350350

351351
try {
@@ -450,6 +450,10 @@ private function buildPlan(): array
450450
? glob($base, GLOB_NOSORT | GLOB_ONLYDIR | GLOB_NOESCAPE)
451451
: [strtr($base, ['[[]' => '[', '[]]' => ']'])]; // unescape [ and ]
452452

453+
if (!$dirs) {
454+
throw new Nette\InvalidStateException(sprintf("Directory '%s' does not exist.", rtrim($base, '/\\')));
455+
}
456+
453457
$search = (object) ['pattern' => $this->buildPattern($rest), 'mode' => $mode, 'recursive' => $recursive];
454458
foreach ($dirs as $dir) {
455459
$plan[$dir][] = $search;

tests/Utils/Finder.errors.phpt

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('missing folder', function () {
1717
Assert::exception(
1818
fn() => iterator_to_array(Finder::findFiles('*')->in('unknown')),
1919
Nette\InvalidStateException::class,
20-
"Directory 'unknown/' not found.",
20+
"Directory 'unknown' does not exist.",
2121
);
2222
});
2323

@@ -29,3 +29,12 @@ test('absolute mask', function () {
2929
"You cannot combine the absolute path in the mask '/*' and the directory to search '.'.",
3030
);
3131
});
32+
33+
34+
test('globing', function () {
35+
Assert::exception(
36+
fn() => iterator_to_array(Finder::findFiles('fixtures.finder/*/unknown/*')),
37+
Nette\InvalidStateException::class,
38+
"Directory './fixtures.finder/*/unknown' does not exist.",
39+
);
40+
});

0 commit comments

Comments
 (0)