Skip to content

[Finder] Remove duplicate slashes in filenames #26763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,9 @@ public function in($dirs)

foreach ((array) $dirs as $dir) {
if (is_dir($dir)) {
$resolvedDirs[] = $dir;
$resolvedDirs[] = $this->normalizeDir($dir);
} elseif ($glob = glob($dir, (defined('GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) {
$resolvedDirs = array_merge($resolvedDirs, $glob);
$resolvedDirs = array_merge($resolvedDirs, array_map(array($this, 'normalizeDir'), $glob));
} else {
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
}
Expand Down Expand Up @@ -794,4 +794,16 @@ private function resetAdapterSelection()
return $properties;
}, $this->adapters);
}

/**
* Normalizes given directory names by removing trailing slashes.
*
* @param string $dir
*
* @return string
*/
private function normalizeDir($dir)
{
return rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Finder/SplFileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SplFileInfo extends \SplFileInfo
*/
public function __construct($file, $relativePath, $relativePathname)
{
parent::__construct(realpath($file) ?: $file);
parent::__construct($file);
$this->relativePath = $relativePath;
$this->relativePathname = $relativePathname;
}
Expand Down
39 changes: 32 additions & 7 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,40 @@ public function testFiles()

public function testRemoveTrailingSlash()
{
if ('\\' === \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('This test cannot be run on Windows.');
$finder = $this->buildFinder();

$expected = $this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar'));
$in = self::$tmpDir.'//';

$this->assertIterator($expected, $finder->in($in)->files()->getIterator());
}

public function testSymlinksNotResolved()
{
if ('\\' === DIRECTORY_SEPARATOR) {
$this->markTestSkipped('symlinks are not supported on Windows');
}

$finder = $this->buildFinder();

$expected = $this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar'));
$in = '//'.realpath(self::$tmpDir).'//';
symlink($this->toAbsolute('foo'), $this->toAbsolute('baz'));
$expected = $this->toAbsolute(array('baz/bar.tmp'));
$in = self::$tmpDir.'/baz/';
try {
$this->assertIterator($expected, $finder->in($in)->files()->getIterator());
unlink($this->toAbsolute('baz'));
} catch (\Exception $e) {
unlink($this->toAbsolute('baz'));
throw $e;
}
}

public function testBackPathNotNormalized()
{
$finder = $this->buildFinder();

$expected = $this->toAbsolute(array('foo/../foo/bar.tmp'));
$in = self::$tmpDir.'/foo/../foo/';
$this->assertIterator($expected, $finder->in($in)->files()->getIterator());
}

Expand Down Expand Up @@ -279,7 +304,7 @@ public function testInWithNonExistentDirectory()
public function testInWithGlob()
{
$finder = $this->buildFinder();
$finder->in(array(__DIR__.'/Fixtures/*/B/C', __DIR__.'/Fixtures/*/*/B/C'))->getIterator();
$finder->in(array(__DIR__.'/Fixtures/*/B/C/', __DIR__.'/Fixtures/*/*/B/C/'))->getIterator();

$this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
}
Expand Down Expand Up @@ -518,8 +543,8 @@ public function testMultipleLocationsWithSubDirectories()
$finder->in($locations)->depth('< 10')->name('*.neon');

$expected = array(
__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
__DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
__DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
);

$this->assertIterator($expected, $finder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testSubPath($baseDir, array $paths, array $subPaths, array $subP

public function getSubPathData()
{
$tmpDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'symfony_finder';
$tmpDir = sys_get_temp_dir().'/symfony_finder';

return array(
array(
Expand Down