Skip to content

Commit

Permalink
Finder: improved exception when directory is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 30, 2023
1 parent dfda0ca commit 0ce0601
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Utils/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
if ($this->maxDepth >= 0 && count($subdirs) > $this->maxDepth) {
return;
} elseif (!is_dir($dir)) {
throw new Nette\InvalidStateException("Directory '$dir' not found.");
throw new Nette\InvalidStateException(sprintf("Directory '%s' does not exist.", rtrim($dir, '/\\')));
}

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

if (!$dirs) {
throw new Nette\InvalidStateException(sprintf("Directory '%s' does not exist.", rtrim($base, '/\\')));
}

$search = (object) ['pattern' => $this->buildPattern($rest), 'mode' => $mode, 'recursive' => $recursive];
foreach ($dirs as $dir) {
$plan[$dir][] = $search;
Expand Down
11 changes: 10 additions & 1 deletion tests/Utils/Finder.errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('missing folder', function () {
Assert::exception(
fn() => iterator_to_array(Finder::findFiles('*')->in('unknown')),
Nette\InvalidStateException::class,
"Directory 'unknown/' not found.",
"Directory 'unknown' does not exist.",
);
});

Expand All @@ -29,3 +29,12 @@ test('absolute mask', function () {
"You cannot combine the absolute path in the mask '/*' and the directory to search '.'.",
);
});


test('globing', function () {
Assert::exception(
fn() => iterator_to_array(Finder::findFiles('fixtures.finder/*/unknown/*')),
Nette\InvalidStateException::class,
"Directory './fixtures.finder/*/unknown' does not exist.",
);
});

0 comments on commit 0ce0601

Please sign in to comment.