diff --git a/Iterator/RecursiveDirectoryIterator.php b/Iterator/RecursiveDirectoryIterator.php index 34cced6..f5fd2d4 100644 --- a/Iterator/RecursiveDirectoryIterator.php +++ b/Iterator/RecursiveDirectoryIterator.php @@ -63,8 +63,9 @@ public function current(): SplFileInfo $subPathname .= $this->directorySeparator; } $subPathname .= $this->getFilename(); + $basePath = $this->rootPath; - if ('/' !== $basePath = $this->rootPath) { + if ('/' !== $basePath && !str_ends_with($basePath, $this->directorySeparator) && !str_ends_with($basePath, '/')) { $basePath .= $this->directorySeparator; } diff --git a/Tests/Iterator/RecursiveDirectoryIteratorTest.php b/Tests/Iterator/RecursiveDirectoryIteratorTest.php index 4914450..353a919 100644 --- a/Tests/Iterator/RecursiveDirectoryIteratorTest.php +++ b/Tests/Iterator/RecursiveDirectoryIteratorTest.php @@ -55,4 +55,31 @@ public function testSeekOnFtp() $this->assertEquals($contains, $actual); } + + public function testTrailingDirectorySeparatorIsStripped() + { + $fixturesDirectory = __DIR__ . '/../Fixtures/'; + $actual = []; + + foreach (new RecursiveDirectoryIterator($fixturesDirectory, RecursiveDirectoryIterator::SKIP_DOTS) as $file) { + $actual[] = $file->getPathname(); + } + + sort($actual); + + $expected = [ + $fixturesDirectory.'.dot', + $fixturesDirectory.'A', + $fixturesDirectory.'copy', + $fixturesDirectory.'dolor.txt', + $fixturesDirectory.'gitignore', + $fixturesDirectory.'ipsum.txt', + $fixturesDirectory.'lorem.txt', + $fixturesDirectory.'one', + $fixturesDirectory.'r+e.gex[c]a(r)s', + $fixturesDirectory.'with space', + ]; + + $this->assertEquals($expected, $actual); + } }