Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c4676a

Browse files
committed
bug #26763 [Finder] Remove duplicate slashes in filenames (helhum)
This PR was squashed before being merged into the 2.7 branch (closes #26763). Discussion ---------- [Finder] Remove duplicate slashes in filenames | Q | A | ------------- | --- | Branch? | 2.7 up to 4.0 | Bug fix? | yes | New feature? | no | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26757 | License | MIT This PR takes another approach to fix in excess slashes in Finder than #26337 which does a simple rtrim instead of the breaking realpath. Commits ------- cdde6d9 [Finder] Remove duplicate slashes in filenames
2 parents eac5ede + cdde6d9 commit 7c4676a

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

src/Symfony/Component/Finder/Finder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,9 @@ public function in($dirs)
629629

630630
foreach ((array) $dirs as $dir) {
631631
if (is_dir($dir)) {
632-
$resolvedDirs[] = $dir;
632+
$resolvedDirs[] = $this->normalizeDir($dir);
633633
} elseif ($glob = glob($dir, (defined('GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) {
634-
$resolvedDirs = array_merge($resolvedDirs, $glob);
634+
$resolvedDirs = array_merge($resolvedDirs, array_map(array($this, 'normalizeDir'), $glob));
635635
} else {
636636
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
637637
}
@@ -794,4 +794,16 @@ private function resetAdapterSelection()
794794
return $properties;
795795
}, $this->adapters);
796796
}
797+
798+
/**
799+
* Normalizes given directory names by removing trailing slashes.
800+
*
801+
* @param string $dir
802+
*
803+
* @return string
804+
*/
805+
private function normalizeDir($dir)
806+
{
807+
return rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
808+
}
797809
}

src/Symfony/Component/Finder/SplFileInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SplFileInfo extends \SplFileInfo
2828
*/
2929
public function __construct($file, $relativePath, $relativePathname)
3030
{
31-
parent::__construct(realpath($file) ?: $file);
31+
parent::__construct($file);
3232
$this->relativePath = $relativePath;
3333
$this->relativePathname = $relativePathname;
3434
}

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,40 @@ public function testFiles()
5050

5151
public function testRemoveTrailingSlash()
5252
{
53-
if ('\\' === \DIRECTORY_SEPARATOR) {
54-
$this->markTestSkipped('This test cannot be run on Windows.');
53+
$finder = $this->buildFinder();
54+
55+
$expected = $this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar'));
56+
$in = self::$tmpDir.'//';
57+
58+
$this->assertIterator($expected, $finder->in($in)->files()->getIterator());
59+
}
60+
61+
public function testSymlinksNotResolved()
62+
{
63+
if ('\\' === DIRECTORY_SEPARATOR) {
64+
$this->markTestSkipped('symlinks are not supported on Windows');
5565
}
5666

5767
$finder = $this->buildFinder();
5868

59-
$expected = $this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar'));
60-
$in = '//'.realpath(self::$tmpDir).'//';
69+
symlink($this->toAbsolute('foo'), $this->toAbsolute('baz'));
70+
$expected = $this->toAbsolute(array('baz/bar.tmp'));
71+
$in = self::$tmpDir.'/baz/';
72+
try {
73+
$this->assertIterator($expected, $finder->in($in)->files()->getIterator());
74+
unlink($this->toAbsolute('baz'));
75+
} catch (\Exception $e) {
76+
unlink($this->toAbsolute('baz'));
77+
throw $e;
78+
}
79+
}
80+
81+
public function testBackPathNotNormalized()
82+
{
83+
$finder = $this->buildFinder();
6184

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

@@ -279,7 +304,7 @@ public function testInWithNonExistentDirectory()
279304
public function testInWithGlob()
280305
{
281306
$finder = $this->buildFinder();
282-
$finder->in(array(__DIR__.'/Fixtures/*/B/C', __DIR__.'/Fixtures/*/*/B/C'))->getIterator();
307+
$finder->in(array(__DIR__.'/Fixtures/*/B/C/', __DIR__.'/Fixtures/*/*/B/C/'))->getIterator();
283308

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

520545
$expected = array(
521-
__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
522-
__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
546+
__DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
547+
__DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
523548
);
524549

525550
$this->assertIterator($expected, $finder);

src/Symfony/Component/Finder/Tests/Iterator/FilePathsIteratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testSubPath($baseDir, array $paths, array $subPaths, array $subP
3131

3232
public function getSubPathData()
3333
{
34-
$tmpDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'symfony_finder';
34+
$tmpDir = sys_get_temp_dir().'/symfony_finder';
3535

3636
return array(
3737
array(

0 commit comments

Comments
 (0)