mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-16 07:11:19 +08:00
more unit tests for helpers/FileHelper::findFiles using new wildcard exceptions matching
This commit is contained in:
@ -259,6 +259,7 @@ class BaseFileHelper
|
|||||||
if (!is_dir($dir)) {
|
if (!is_dir($dir)) {
|
||||||
throw new InvalidParamException('The dir argument must be a directory.');
|
throw new InvalidParamException('The dir argument must be a directory.');
|
||||||
}
|
}
|
||||||
|
$dir = rtrim($dir, DIRECTORY_SEPARATOR);
|
||||||
if (!isset($options['basePath'])) {
|
if (!isset($options['basePath'])) {
|
||||||
$options['basePath'] = realpath($dir);
|
$options['basePath'] = realpath($dir);
|
||||||
// this should also be done only once
|
// this should also be done only once
|
||||||
|
@ -253,23 +253,58 @@ class FileHelperTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testFindFilesExclude()
|
public function testFindFilesExclude()
|
||||||
{
|
{
|
||||||
$dirName = 'test_dir';
|
$basePath = $this->testFilePath . DIRECTORY_SEPARATOR;
|
||||||
$fileName = 'test_file.txt';
|
$dirs = array('', 'one', 'one'.DIRECTORY_SEPARATOR.'two', 'three');
|
||||||
$excludeFileName = 'exclude_file.txt';
|
$files = array_fill_keys(array_map(function($n){return "a.$n";}, range(1,8)), 'file contents');
|
||||||
$this->createFileStructure([
|
|
||||||
$dirName => [
|
|
||||||
$fileName => 'file content',
|
|
||||||
$excludeFileName => 'exclude file content',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
$basePath = $this->testFilePath;
|
|
||||||
$dirName = $basePath . DIRECTORY_SEPARATOR . $dirName;
|
|
||||||
|
|
||||||
$options = [
|
$tree = $files;
|
||||||
'except' => [$excludeFileName],
|
$root = $files;
|
||||||
];
|
$flat = array();
|
||||||
$foundFiles = FileHelper::findFiles($dirName, $options);
|
foreach($dirs as $dir) {
|
||||||
$this->assertEquals([$dirName . DIRECTORY_SEPARATOR . $fileName], $foundFiles);
|
foreach($files as $fileName=>$contents) {
|
||||||
|
$flat[] = rtrim($basePath.$dir,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$fileName;
|
||||||
|
}
|
||||||
|
if ($dir === '') continue;
|
||||||
|
$parts = explode(DIRECTORY_SEPARATOR, $dir);
|
||||||
|
$last = array_pop($parts);
|
||||||
|
$parent = array_pop($parts);
|
||||||
|
$tree[$last] = $files;
|
||||||
|
if ($parent !== null) {
|
||||||
|
$tree[$parent][$last] = &$tree[$last];
|
||||||
|
} else {
|
||||||
|
$root[$last] = &$tree[$last];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->createFileStructure($root);
|
||||||
|
|
||||||
|
// range
|
||||||
|
$foundFiles = FileHelper::findFiles($basePath, ['except' => ['a.[2-8]']]);
|
||||||
|
sort($foundFiles);
|
||||||
|
$expect = array_values(array_filter($flat, function($p){return substr($p, -3)==='a.1';}));
|
||||||
|
$this->assertEquals($expect, $foundFiles);
|
||||||
|
|
||||||
|
// suffix
|
||||||
|
$foundFiles = FileHelper::findFiles($basePath, ['except' => ['*.1']]);
|
||||||
|
sort($foundFiles);
|
||||||
|
$expect = array_values(array_filter($flat, function($p){return substr($p, -3)!=='a.1';}));
|
||||||
|
$this->assertEquals($expect, $foundFiles);
|
||||||
|
|
||||||
|
// dir
|
||||||
|
$foundFiles = FileHelper::findFiles($basePath, ['except' => ['/one']]);
|
||||||
|
sort($foundFiles);
|
||||||
|
$expect = array_values(array_filter($flat, function($p){return strpos($p, DIRECTORY_SEPARATOR.'one')===false;}));
|
||||||
|
$this->assertEquals($expect, $foundFiles);
|
||||||
|
|
||||||
|
// dir contents
|
||||||
|
$foundFiles = FileHelper::findFiles($basePath, ['except' => ['?*/a.1']]);
|
||||||
|
sort($foundFiles);
|
||||||
|
$expect = array_values(array_filter($flat, function($p){
|
||||||
|
return substr($p, -11, 10)==='one'.DIRECTORY_SEPARATOR.'two'.DIRECTORY_SEPARATOR.'a.' || (
|
||||||
|
substr($p, -8)!==DIRECTORY_SEPARATOR.'one'.DIRECTORY_SEPARATOR.'a.1' &&
|
||||||
|
substr($p, -10)!==DIRECTORY_SEPARATOR.'three'.DIRECTORY_SEPARATOR.'a.1'
|
||||||
|
);
|
||||||
|
}));
|
||||||
|
$this->assertEquals($expect, $foundFiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateDirectory()
|
public function testCreateDirectory()
|
||||||
|
Reference in New Issue
Block a user