diff --git a/src/filesystem/FileFinder.php b/src/filesystem/FileFinder.php --- a/src/filesystem/FileFinder.php +++ b/src/filesystem/FileFinder.php @@ -21,6 +21,7 @@ private $root; private $exclude = array(); private $paths = array(); + private $name = array(); private $suffix = array(); private $type; private $generateChecksums = false; @@ -49,6 +50,14 @@ /** * @task config */ + public function withName($name) { + $this->name[] = $name; + return $this; + } + + /** + * @task config + */ public function withSuffix($suffix) { $this->suffix[] = '*.'.$suffix; return $this; @@ -99,7 +108,13 @@ * @task internal */ public function validateFile($file) { - $matches = (count($this->suffix) == 0); + $matches = !count($this->name) && !count($this->suffix); + foreach ($this->name as $curr_name) { + if (basename($file) === $curr_name) { + $matches = true; + break; + } + } foreach ($this->suffix as $curr_suffix) { if (fnmatch($curr_suffix, $file)) { $matches = true; @@ -208,8 +223,10 @@ $args[] = $this->type; } - if ($this->suffix) { - $command[] = $this->generateList('name', $this->suffix); + if ($this->name || $this->suffix) { + $command[] = $this->generateList('name', array_merge( + $this->name, + $this->suffix)); } if ($this->paths) { diff --git a/src/filesystem/__tests__/FileFinderTestCase.php b/src/filesystem/__tests__/FileFinderTestCase.php --- a/src/filesystem/__tests__/FileFinderTestCase.php +++ b/src/filesystem/__tests__/FileFinderTestCase.php @@ -5,23 +5,22 @@ */ final class FileFinderTestCase extends PhutilTestCase { - protected function findFiles($root, $checksums, $type, $path, $mode) { - $finder = new FileFinder($root); - $finder->setGenerateChecksums($checksums) - ->excludePath('./exclude') - ->excludePath('subdir.txt') - ->withType($type) - ->withPath($path) - ->withSuffix('txt') - ->setForceMode($mode); - $files = $finder->find(); - return $files; + protected function getFinder() { + $finder = new FileFinder(dirname(__FILE__) . '/data'); + $finder->excludePath('./exclude') + ->excludePath('subdir.txt'); + return $finder; } public function testFinderWithChecksums() { - $root = dirname(__FILE__) . '/data'; foreach (array('php', 'shell') as $mode) { - $files = $this->findFiles($root, true, 'f', '*', $mode); + $files = $this->getFinder() + ->setGenerateChecksums(true) + ->withType('f') + ->withPath('*') + ->withSuffix('txt') + ->setForceMode($mode) + ->find(); // Test whether correct files were found. $this->assertTrue(array_key_exists('test.txt', $files)); @@ -51,9 +50,13 @@ } public function testFinderWithoutChecksums() { - $root = dirname(__FILE__) . '/data'; foreach (array('php', 'shell') as $mode) { - $files = $this->findFiles($root, false, 'f', '*', $mode); + $files = $this->getFinder() + ->withType('f') + ->withPath('*') + ->withSuffix('txt') + ->setForceMode($mode) + ->find(); // Test whether correct files were found. $this->assertTrue(in_array('test.txt', $files)); @@ -72,9 +75,13 @@ } public function testFinderWithDirectories() { - $root = dirname(__FILE__) . '/data'; foreach (array('php', 'shell') as $mode) { - $files = $this->findFiles($root, true, '', '*', $mode); + $files = $this->getFinder() + ->setGenerateChecksums(true) + ->withPath('*') + ->withSuffix('txt') + ->setForceMode($mode) + ->find(); // Test whether the correct files were found. $this->assertTrue(array_key_exists('test.txt', $files)); @@ -95,10 +102,14 @@ } public function testFinderWithPath() { - $root = dirname(__FILE__) . '/data'; foreach (array('php', 'shell') as $mode) { - $files = $this->findFiles($root, true, 'f', - '*/include_dir.txt/subdir.txt/alsoinclude.txt', $mode); + $files = $this->getFinder() + ->setGenerateChecksums(true) + ->withType('f') + ->withPath('*/include_dir.txt/subdir.txt/alsoinclude.txt') + ->withSuffix('txt') + ->setForceMode($mode) + ->find(); // Test whether the correct files were found. $this->assertTrue( @@ -109,4 +120,22 @@ } } + public function testFinderWithNames() { + foreach (array('php', 'shell') as $mode) { + $files = $this->getFinder() + ->withType('f') + ->withPath('*') + ->withName('test') + ->setForceMode($mode) + ->find(); + + // Test whether the correct files were found. + $this->assertTrue(in_array('test', $files)); + $this->assertFalse(in_array('exclude/test', $files)); + $this->assertTrue(in_array('include_dir.txt/test', $files)); + $this->assertTrue(in_array('include_dir.txt/subdir.txt/test', $files)); + $this->assertEqual(3, count($files)); + } + } + } diff --git a/src/filesystem/__tests__/data/exclude/test b/src/filesystem/__tests__/data/exclude/test new file mode 100644 --- /dev/null +++ b/src/filesystem/__tests__/data/exclude/test @@ -0,0 +1 @@ +Test file. diff --git a/src/filesystem/__tests__/data/include_dir.txt/subdir.txt/test b/src/filesystem/__tests__/data/include_dir.txt/subdir.txt/test new file mode 100644 --- /dev/null +++ b/src/filesystem/__tests__/data/include_dir.txt/subdir.txt/test @@ -0,0 +1 @@ +Test file. diff --git a/src/filesystem/__tests__/data/include_dir.txt/test b/src/filesystem/__tests__/data/include_dir.txt/test new file mode 100644 --- /dev/null +++ b/src/filesystem/__tests__/data/include_dir.txt/test @@ -0,0 +1 @@ +Test file.