From f1b25e7759451fea82b7b100b773fe60fccd0ec5 Mon Sep 17 00:00:00 2001 From: Lauris Binde Date: Fri, 30 May 2014 08:46:14 +0300 Subject: [PATCH 1/4] PR for taskphp/task#6 --- src/ProjectFinder.php | 51 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/ProjectFinder.php b/src/ProjectFinder.php index dfe89c1..4ac0a98 100644 --- a/src/ProjectFinder.php +++ b/src/ProjectFinder.php @@ -6,21 +6,58 @@ class ProjectFinder { - public function find($taskfile = './Taskfile') + /** + * Taskfile basename variants, in priority order. + * + * @var array + */ + private $variants = ['Taskfile', 'taskfile', 'taskfile.php']; + + /** + * @return string|null + */ + public function findTaskfile() { - if (!file_exists($taskfile)) { + $taskfile = null; + + $cwd = getcwd(); + + foreach($this->variants as $variant) + { + $file = $cwd . DIRECTORY_SEPARATOR . $variant; + + if(is_file($file)) + { + $taskfile = $file; + + break; + } + } + + return $taskfile; + } + + public function find() + { + $taskfile = $this->findTaskfile(); + + // FIXME: This can go in findTaskfile() and then we can get rid of returning NULL. + if($taskfile === null) + { throw new \InvalidArgumentException("$taskfile not found"); } - if (filesize($taskfile) === 0) { + // FIXME: This is redundant, because return check below will handle this. + if(filesize($taskfile) === 0) + { throw new \LogicException("Taskfile is empty"); } $project = require $taskfile; - - $cls = 'Task\Project'; - if (!($project instanceof $cls)) { - throw new \LogicException("Taskfile must return a Project"); + + if(!($project instanceof '\Task\Project')) + { + throw new \LogicException("Taskfile must return an instance of '\Task\Project'"); } return $project; From 0ffd690c7d8098fa9591e9b3e5df64f9fbbf164f Mon Sep 17 00:00:00 2001 From: Lauris Binde Date: Fri, 30 May 2014 11:42:09 +0300 Subject: [PATCH 2/4] Fixed syntax error. --- src/ProjectFinder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ProjectFinder.php b/src/ProjectFinder.php index 4ac0a98..357bd8e 100644 --- a/src/ProjectFinder.php +++ b/src/ProjectFinder.php @@ -55,7 +55,7 @@ public function find() $project = require $taskfile; - if(!($project instanceof '\Task\Project')) + if(!($project instanceof \Task\Project)) { throw new \LogicException("Taskfile must return an instance of '\Task\Project'"); } From fa7d3eb18c4669ba5daea4c14b5fcd6b228ef7ee Mon Sep 17 00:00:00 2001 From: Lauris Binde Date: Fri, 30 May 2014 12:38:01 +0300 Subject: [PATCH 3/4] Fixed and cleaned up tests for taskphp/cli#1 --- src/ProjectFinder.php | 3 ++ tests/fixtures/Taskfile | 5 ---- tests/fixtures/empty-taskfile/Taskfile | 0 tests/fixtures/invalid-taskfile/Taskfile | 3 ++ tests/fixtures/no-taskfile/.gitkeep | 0 tests/fixtures/valid-taskfile/Taskfile | 5 ++++ tests/src/ProjectFinderTest.php | 37 +++++++++++++++++------- 7 files changed, 37 insertions(+), 16 deletions(-) delete mode 100644 tests/fixtures/Taskfile create mode 100644 tests/fixtures/empty-taskfile/Taskfile create mode 100644 tests/fixtures/invalid-taskfile/Taskfile create mode 100644 tests/fixtures/no-taskfile/.gitkeep create mode 100644 tests/fixtures/valid-taskfile/Taskfile diff --git a/src/ProjectFinder.php b/src/ProjectFinder.php index 357bd8e..c875528 100644 --- a/src/ProjectFinder.php +++ b/src/ProjectFinder.php @@ -37,6 +37,9 @@ public function findTaskfile() return $taskfile; } + /** + * @return \Task\Project + */ public function find() { $taskfile = $this->findTaskfile(); diff --git a/tests/fixtures/Taskfile b/tests/fixtures/Taskfile deleted file mode 100644 index cfd5822..0000000 --- a/tests/fixtures/Taskfile +++ /dev/null @@ -1,5 +0,0 @@ -find($taskfile); - $this->assertEquals(require $taskfile, $project); + $dir = dirname(__DIR__) . '/fixtures/valid-taskfile'; + + chdir($dir); + + $taskfile = $dir . '/Taskfile'; + + $expected = require $taskfile; + + $actual = (new ProjectFinder)->find(); + + $this->assertEquals($expected, $actual); } /** @@ -16,7 +24,11 @@ public function testFind() */ public function testFindThrowsOnNoTaskFile() { - (new ProjectFinder)->find('nope'); + $dir = dirname(__DIR__) . '/fixtures/no-taskfile'; + + chdir($dir); + + (new ProjectFinder)->find(); } /** @@ -24,9 +36,11 @@ public function testFindThrowsOnNoTaskFile() */ public function testFindThrowsOnEmptyTaskFile() { - $taskfile = tempnam(sys_get_temp_dir(), 'taskfile'); - (new ProjectFinder)->find($taskfile); - unlink($taskfile); + $dir = dirname(__DIR__) . '/fixtures/empty-taskfile'; + + chdir($dir); + + (new ProjectFinder)->find(); } /** @@ -34,9 +48,10 @@ public function testFindThrowsOnEmptyTaskFile() */ public function testFindThrowsOnNotProject() { - $taskfile = tempnam(sys_get_temp_dir(), 'taskfile'); - file_put_contents($taskfile, 'find($taskfile); - unlink($taskfile); + $dir = dirname(__DIR__) . '/fixtures/invalid-taskfile'; + + chdir($dir); + + (new ProjectFinder)->find(); } } From 55c9d1c92e6d062ce55cc58c326efec6cccf3a22 Mon Sep 17 00:00:00 2001 From: Mike Fisher Date: Fri, 30 May 2014 11:25:06 +0100 Subject: [PATCH 4/4] Remove superfluous project checks --- src/ProjectFinder.php | 59 ++++++++++++++++----------------- tests/src/ProjectFinderTest.php | 28 +++------------- 2 files changed, 34 insertions(+), 53 deletions(-) diff --git a/src/ProjectFinder.php b/src/ProjectFinder.php index c875528..ad48ef7 100644 --- a/src/ProjectFinder.php +++ b/src/ProjectFinder.php @@ -6,6 +6,13 @@ class ProjectFinder { + /** + * Directory to search for Taskfiles + * + * @var string + */ + private $cwd; + /** * Taskfile basename variants, in priority order. * @@ -13,54 +20,46 @@ class ProjectFinder */ private $variants = ['Taskfile', 'taskfile', 'taskfile.php']; + public function __construct($cwd = null) + { + $this->cwd = $cwd ?: getcwd(); + } + + public function getCwd() + { + return $this->cwd; + } + /** - * @return string|null + * @return string|false */ public function findTaskfile() { - $taskfile = null; - - $cwd = getcwd(); - - foreach($this->variants as $variant) - { - $file = $cwd . DIRECTORY_SEPARATOR . $variant; + foreach ($this->variants as $variant) { + $file = $this->getCwd() . DIRECTORY_SEPARATOR . $variant; - if(is_file($file)) - { - $taskfile = $file; - - break; + if (is_file($file)) { + return $file; } } - return $taskfile; + return false; } /** - * @return \Task\Project + * @return Task\Project + * @throws RuntimeException */ public function find() { - $taskfile = $this->findTaskfile(); - - // FIXME: This can go in findTaskfile() and then we can get rid of returning NULL. - if($taskfile === null) - { - throw new \InvalidArgumentException("$taskfile not found"); - } - - // FIXME: This is redundant, because return check below will handle this. - if(filesize($taskfile) === 0) - { - throw new \LogicException("Taskfile is empty"); + if (!$taskfile = $this->findTaskfile()) { + throw new \RuntimeException("No Taskfile found"); } $project = require $taskfile; - if(!($project instanceof \Task\Project)) - { - throw new \LogicException("Taskfile must return an instance of '\Task\Project'"); + if (!($project instanceof Project)) { + throw new \UnexpectedValueException("Taskfile must return an instance of 'Task\Project'"); } return $project; diff --git a/tests/src/ProjectFinderTest.php b/tests/src/ProjectFinderTest.php index c96b9b4..b8a187f 100644 --- a/tests/src/ProjectFinderTest.php +++ b/tests/src/ProjectFinderTest.php @@ -8,50 +8,32 @@ public function testFind() { $dir = dirname(__DIR__) . '/fixtures/valid-taskfile'; - chdir($dir); - $taskfile = $dir . '/Taskfile'; $expected = require $taskfile; - $actual = (new ProjectFinder)->find(); + $actual = (new ProjectFinder($dir))->find(); $this->assertEquals($expected, $actual); } /** - * @expectedException InvalidArgumentException + * @expectedException RuntimeException */ public function testFindThrowsOnNoTaskFile() { $dir = dirname(__DIR__) . '/fixtures/no-taskfile'; - chdir($dir); - - (new ProjectFinder)->find(); - } - - /** - * @expectedException LogicException - */ - public function testFindThrowsOnEmptyTaskFile() - { - $dir = dirname(__DIR__) . '/fixtures/empty-taskfile'; - - chdir($dir); - - (new ProjectFinder)->find(); + (new ProjectFinder($dir))->find(); } /** - * @expectedException LogicException + * @expectedException UnexpectedValueException */ public function testFindThrowsOnNotProject() { $dir = dirname(__DIR__) . '/fixtures/invalid-taskfile'; - chdir($dir); - - (new ProjectFinder)->find(); + (new ProjectFinder($dir))->find(); } }