Skip to content

Commit 18902a2

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [Finder] Remove duplicate slashes in filenames
2 parents bd56f5f + 64c6348 commit 18902a2

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

Finder.php

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

651651
foreach ((array) $dirs as $dir) {
652652
if (is_dir($dir)) {
653-
$resolvedDirs[] = $dir;
653+
$resolvedDirs[] = $this->normalizeDir($dir);
654654
} elseif ($glob = glob($dir, (defined('GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) {
655-
$resolvedDirs = array_merge($resolvedDirs, $glob);
655+
$resolvedDirs = array_merge($resolvedDirs, array_map(array($this, 'normalizeDir'), $glob));
656656
} else {
657657
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
658658
}
@@ -901,4 +901,16 @@ private function initDefaultAdapters()
901901
;
902902
}
903903
}
904+
905+
/**
906+
* Normalizes given directory names by removing trailing slashes.
907+
*
908+
* @param string $dir
909+
*
910+
* @return string
911+
*/
912+
private function normalizeDir($dir)
913+
{
914+
return rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
915+
}
904916
}

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
}

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

@@ -280,7 +305,7 @@ public function testInWithNonExistentDirectory()
280305
public function testInWithGlob()
281306
{
282307
$finder = $this->buildFinder();
283-
$finder->in(array(__DIR__.'/Fixtures/*/B/C', __DIR__.'/Fixtures/*/*/B/C'))->getIterator();
308+
$finder->in(array(__DIR__.'/Fixtures/*/B/C/', __DIR__.'/Fixtures/*/*/B/C/'))->getIterator();
284309

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

521546
$expected = array(
522-
__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
523-
__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
547+
__DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
548+
__DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
524549
);
525550

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

Tests/Iterator/FilePathsIteratorTest.php

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

3535
public function getSubPathData()
3636
{
37-
$tmpDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'symfony_finder';
37+
$tmpDir = sys_get_temp_dir().'/symfony_finder';
3838

3939
return array(
4040
array(

0 commit comments

Comments
 (0)