[Finder] Escape location for regex searches

This commit is contained in:
Daniel Beyer 2014-09-20 16:09:40 +02:00 committed by Fabien Potencier
parent 3da421f2c2
commit b63926b246
3 changed files with 22 additions and 8 deletions

View File

@ -216,7 +216,7 @@ abstract class AbstractFindAdapter extends AbstractAdapter
// Fixes 'not search' regex problems.
if ($expr->isRegex()) {
$regex = $expr->getRegex();
$regex->prepend($regex->hasStartFlag() ? $dir.DIRECTORY_SEPARATOR : '.*')->setEndJoker(!$regex->hasEndFlag());
$regex->prepend($regex->hasStartFlag() ? preg_quote($dir).DIRECTORY_SEPARATOR : '.*')->setEndJoker(!$regex->hasEndFlag());
} else {
$expr->prepend('*')->append('*');
}
@ -247,7 +247,7 @@ abstract class AbstractFindAdapter extends AbstractAdapter
$command->add('-size -'.($size->getTarget() + 1).'c');
break;
case '>=':
$command->add('-size +'. ($size->getTarget() - 1).'c');
$command->add('-size +'.($size->getTarget() - 1).'c');
break;
case '>':
$command->add('-size +'.$size->getTarget().'c');

View File

@ -569,6 +569,20 @@ class FinderTest extends Iterator\RealIteratorTestCase
}
}
/**
* @dataProvider getAdaptersTestData
*/
public function testRegexSpecialCharsLocationWithPathRestrictionContainingStartFlag(Adapter\AdapterInterface $adapter)
{
$finder = $this->buildFinder($adapter);
$finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'r+e.g?e*x[c]a(r)s')
->path('/^dir/');
$expected = array('r+e.g?e*x[c]a(r)s'.DIRECTORY_SEPARATOR.'dir',
'r+e.g?e*x[c]a(r)s'.DIRECTORY_SEPARATOR.'dir'.DIRECTORY_SEPARATOR.'bar.dat',);
$this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
}
public function testAdaptersOrdering()
{
$finder = Finder::create()
@ -673,7 +687,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
$tests = array(
array('', '', array()),
array('/^A\/B\/C/', '/C$/',
array('A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat')
array('A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat'),
),
array('/^A\/B/', 'foobar',
array(
@ -681,7 +695,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'ab.dat',
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
)
),
),
array('A/B/C', 'foobar',
array(
@ -689,7 +703,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat.copy',
)
),
),
array('A/B', 'foobar',
array(
@ -703,12 +717,12 @@ class FinderTest extends Iterator\RealIteratorTestCase
'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'ab.dat.copy',
'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat.copy',
)
),
),
array('/^with space\//', 'foobar',
array(
'with space'.DIRECTORY_SEPARATOR.'foo.txt',
)
),
),
);
@ -808,7 +822,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
array(
new Adapter\BsdFindAdapter(),
new Adapter\GnuFindAdapter(),
new Adapter\PhpAdapter()
new Adapter\PhpAdapter(),
),
function (Adapter\AdapterInterface $adapter) {
return $adapter->isSupported();