From 85a33fa965adc1711601073442d3dc5c7a2a87bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 12 Feb 2014 17:31:47 +0100 Subject: [PATCH] Fix checking parent folders for Phakefile --- lib/phake/Builder.php | 13 +++++----- tests/BuilderTest.php | 57 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/lib/phake/Builder.php b/lib/phake/Builder.php index 1784653..dfcf9fc 100644 --- a/lib/phake/Builder.php +++ b/lib/phake/Builder.php @@ -72,20 +72,21 @@ private function assign_description($thing) { } public function resolve_runfile($directory) { - $directory = rtrim($directory, '/') . '/'; + $directory = rtrim($directory, '/'); $runfiles = array('Phakefile', 'Phakefile.php'); + do { foreach ($runfiles as $r) { - $candidate = $directory . $r; + $candidate = $directory . '/' . $r; if (file_exists($candidate)) { return $candidate; } } - if ($directory == '/') { - throw new \Exception("No Phakefile found"); - } + $previous = $directory; $directory = dirname($directory); - } while (true); + } while ($directory !== $previous); + + throw new \Exception("No Phakefile found"); } public function load_runfile($file) { diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 57e378a..ed034e0 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -24,6 +24,63 @@ public function testInvalidPath() $builder->load_runfile('does not exist'); } + /** + * @dataProvider provideResolveRunfile + */ + public function testResolveRunfile($search, $expected) + { + $builder = new Builder(); + + $this->assertEquals($expected, $builder->resolve_runfile($search)); + } + + public function provideResolveRunfile() + { + $root = __DIR__ . '/..'; + + return array( + array( + $root, + $root . '/Phakefile' + ), + array( + $root . '/bin', + $root . '/Phakefile' + ), + array( + $root . '/example', + $root . '/example/Phakefile' + ), + array( + $root . '/lib/phake', + $root . '/Phakefile' + ) + ); + } + + /** + * @expectedException Exception + * @dataProvider provideResolveRunfileNonExistant + */ + public function testResolveRunfileNonExistant($path) + { + $builder = new Builder(); + + $builder->resolve_runfile($path); + } + + public function provideResolveRunfileNonExistant() + { + return array( + array( + realpath('../../') + ), + array( + realpath(__DIR__ . '/../..') + ), + ); + } + public function testEmpty() { $builder = new Builder();