Skip to content

Commit

Permalink
do not duplicate directory separators
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jul 31, 2024
1 parent 0724c51 commit 4f3b0f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Iterator/RecursiveDirectoryIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ public function current()
$subPathname .= $this->directorySeparator;
}
$subPathname .= $this->getFilename();
$basePath = $this->rootPath;

if ('/' !== $basePath = $this->rootPath) {
if ('/' !== $basePath && !str_ends_with($basePath, $this->directorySeparator)) {
$basePath .= $this->directorySeparator;
}

Expand Down
27 changes: 27 additions & 0 deletions Tests/Iterator/RecursiveDirectoryIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4f3b0f7

Please sign in to comment.