Merge branch '2.7' into 2.8

* 2.7:
  [Finder] Remove duplicate slashes in filenames
This commit is contained in:
Fabien Potencier 2018-04-04 07:04:41 +02:00
commit d3374b229b
4 changed files with 48 additions and 11 deletions

View File

@ -650,9 +650,9 @@ class Finder implements \IteratorAggregate, \Countable
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));
}
@ -901,4 +901,16 @@ class Finder implements \IteratorAggregate, \Countable
;
}
}
/**
* Normalizes given directory names by removing trailing slashes.
*
* @param string $dir
*
* @return string
*/
private function normalizeDir($dir)
{
return rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
}
}

View File

@ -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;
}

View File

@ -50,15 +50,40 @@ class FinderTest extends Iterator\RealIteratorTestCase
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());
}
@ -280,7 +305,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
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);
}
@ -519,8 +544,8 @@ class FinderTest extends Iterator\RealIteratorTestCase
$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);

View File

@ -34,7 +34,7 @@ class FilePathsIteratorTest extends RealIteratorTestCase
public function getSubPathData()
{
$tmpDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'symfony_finder';
$tmpDir = sys_get_temp_dir().'/symfony_finder';
return array(
array(