From 0ce0601fd5f892e8b58fa18619ee21c0c6c07468 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 6 Feb 2023 13:18:26 +0100 Subject: [PATCH] Finder: improved exception when directory is missing --- src/Utils/Finder.php | 6 +++++- tests/Utils/Finder.errors.phpt | 11 ++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Utils/Finder.php b/src/Utils/Finder.php index a17065464..1d38cc7d9 100644 --- a/src/Utils/Finder.php +++ b/src/Utils/Finder.php @@ -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 { @@ -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; diff --git a/tests/Utils/Finder.errors.phpt b/tests/Utils/Finder.errors.phpt index d31e3e6ce..f84897e0c 100644 --- a/tests/Utils/Finder.errors.phpt +++ b/tests/Utils/Finder.errors.phpt @@ -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.", ); }); @@ -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.", + ); +});