Changeset View
Changeset View
Standalone View
Standalone View
src/filesystem/__tests__/FileFinderTestCase.php
| <?php | <?php | ||||
| /** | /** | ||||
| * @group testcase | * @group testcase | ||||
| */ | */ | ||||
| final class FileFinderTestCase extends PhutilTestCase { | final class FileFinderTestCase extends PhutilTestCase { | ||||
| protected function findFiles($root, $checksums, $type, $path, $mode) { | protected function getFinder() { | ||||
| $finder = new FileFinder($root); | $finder = new FileFinder(dirname(__FILE__) . '/data'); | ||||
| $finder->setGenerateChecksums($checksums) | $finder->excludePath('./exclude') | ||||
| ->excludePath('./exclude') | ->excludePath('subdir.txt'); | ||||
| ->excludePath('subdir.txt') | return $finder; | ||||
| ->withType($type) | |||||
| ->withPath($path) | |||||
| ->withSuffix('txt') | |||||
| ->setForceMode($mode); | |||||
| $files = $finder->find(); | |||||
| return $files; | |||||
| } | } | ||||
| public function testFinderWithChecksums() { | public function testFinderWithChecksums() { | ||||
| $root = dirname(__FILE__) . '/data'; | |||||
| foreach (array('php', 'shell') as $mode) { | 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. | // Test whether correct files were found. | ||||
| $this->assertTrue(array_key_exists('test.txt', $files)); | $this->assertTrue(array_key_exists('test.txt', $files)); | ||||
| $this->assertTrue(array_key_exists('file.txt', $files)); | $this->assertTrue(array_key_exists('file.txt', $files)); | ||||
| $this->assertTrue( | $this->assertTrue( | ||||
| array_key_exists('include_dir.txt/subdir.txt/alsoinclude.txt', | array_key_exists('include_dir.txt/subdir.txt/alsoinclude.txt', | ||||
| $files)); | $files)); | ||||
| $this->assertFalse(array_key_exists('test', $files)); | $this->assertFalse(array_key_exists('test', $files)); | ||||
| Show All 13 Lines | foreach (array('php', 'shell') as $mode) { | ||||
| $this->assertEqual($files['.hidden.txt'], | $this->assertEqual($files['.hidden.txt'], | ||||
| 'b6cfc9ce9afe12b258ee1c19c235aa27'); | 'b6cfc9ce9afe12b258ee1c19c235aa27'); | ||||
| $this->assertEqual($files['include_dir.txt/subdir.txt/alsoinclude.txt'], | $this->assertEqual($files['include_dir.txt/subdir.txt/alsoinclude.txt'], | ||||
| '91e5c1ad76ff229c6456ac92e74e1d9f'); | '91e5c1ad76ff229c6456ac92e74e1d9f'); | ||||
| } | } | ||||
| } | } | ||||
| public function testFinderWithoutChecksums() { | public function testFinderWithoutChecksums() { | ||||
| $root = dirname(__FILE__) . '/data'; | |||||
| foreach (array('php', 'shell') as $mode) { | 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. | // Test whether correct files were found. | ||||
| $this->assertTrue(in_array('test.txt', $files)); | $this->assertTrue(in_array('test.txt', $files)); | ||||
| $this->assertTrue(in_array('file.txt', $files)); | $this->assertTrue(in_array('file.txt', $files)); | ||||
| $this->assertTrue(in_array('.hidden.txt', $files)); | $this->assertTrue(in_array('.hidden.txt', $files)); | ||||
| $this->assertTrue( | $this->assertTrue( | ||||
| in_array('include_dir.txt/subdir.txt/alsoinclude.txt', $files)); | in_array('include_dir.txt/subdir.txt/alsoinclude.txt', $files)); | ||||
| $this->assertFalse(in_array('test', $files)); | $this->assertFalse(in_array('test', $files)); | ||||
| $this->assertFalse(in_array('exclude/file.txt', $files)); | $this->assertFalse(in_array('exclude/file.txt', $files)); | ||||
| $this->assertFalse(in_array('include_dir.txt', $files)); | $this->assertFalse(in_array('include_dir.txt', $files)); | ||||
| foreach ($files as $file => $checksum) { | foreach ($files as $file => $checksum) { | ||||
| $this->assertFalse(is_dir($file)); | $this->assertFalse(is_dir($file)); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| public function testFinderWithDirectories() { | public function testFinderWithDirectories() { | ||||
| $root = dirname(__FILE__) . '/data'; | |||||
| foreach (array('php', 'shell') as $mode) { | 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. | // Test whether the correct files were found. | ||||
| $this->assertTrue(array_key_exists('test.txt', $files)); | $this->assertTrue(array_key_exists('test.txt', $files)); | ||||
| $this->assertTrue(array_key_exists('file.txt', $files)); | $this->assertTrue(array_key_exists('file.txt', $files)); | ||||
| $this->assertTrue( | $this->assertTrue( | ||||
| array_key_exists('include_dir.txt/subdir.txt/alsoinclude.txt', | array_key_exists('include_dir.txt/subdir.txt/alsoinclude.txt', | ||||
| $files)); | $files)); | ||||
| $this->assertFalse(array_key_exists('test', $files)); | $this->assertFalse(array_key_exists('test', $files)); | ||||
| $this->assertTrue(array_key_exists('.hidden.txt', $files)); | $this->assertTrue(array_key_exists('.hidden.txt', $files)); | ||||
| $this->assertFalse(array_key_exists('exclude/file.txt', $files)); | $this->assertFalse(array_key_exists('exclude/file.txt', $files)); | ||||
| $this->assertTrue(array_key_exists('include_dir.txt', $files)); | $this->assertTrue(array_key_exists('include_dir.txt', $files)); | ||||
| // Test checksums. | // Test checksums. | ||||
| $this->assertEqual($files['test.txt'], | $this->assertEqual($files['test.txt'], | ||||
| 'aea46212fa8b8d0e0e6aa34a15c9e2f5'); | 'aea46212fa8b8d0e0e6aa34a15c9e2f5'); | ||||
| $this->assertEqual($files['include_dir.txt'], null); | $this->assertEqual($files['include_dir.txt'], null); | ||||
| } | } | ||||
| } | } | ||||
| public function testFinderWithPath() { | public function testFinderWithPath() { | ||||
| $root = dirname(__FILE__) . '/data'; | |||||
| foreach (array('php', 'shell') as $mode) { | foreach (array('php', 'shell') as $mode) { | ||||
| $files = $this->findFiles($root, true, 'f', | $files = $this->getFinder() | ||||
| '*/include_dir.txt/subdir.txt/alsoinclude.txt', $mode); | ->setGenerateChecksums(true) | ||||
| ->withType('f') | |||||
| ->withPath('*/include_dir.txt/subdir.txt/alsoinclude.txt') | |||||
| ->withSuffix('txt') | |||||
| ->setForceMode($mode) | |||||
| ->find(); | |||||
| // Test whether the correct files were found. | // Test whether the correct files were found. | ||||
| $this->assertTrue( | $this->assertTrue( | ||||
| array_key_exists('include_dir.txt/subdir.txt/alsoinclude.txt', | array_key_exists('include_dir.txt/subdir.txt/alsoinclude.txt', | ||||
| $files)); | $files)); | ||||
| // Ensure that only the one file was found. | // Ensure that only the one file was found. | ||||
| $this->assertEqual(1, count($files)); | $this->assertEqual(1, count($files)); | ||||
| } | } | ||||
| } | } | ||||
| 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)); | |||||
| } | |||||
| } | |||||
| } | } | ||||