Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 49 additions & 10 deletions src/ProjectFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,60 @@

class ProjectFinder
{
public function find($taskfile = './Taskfile')
/**
* Directory to search for Taskfiles
*
* @var string
*/
private $cwd;

/**
* Taskfile basename variants, in priority order.
*
* @var array
*/
private $variants = ['Taskfile', 'taskfile', 'taskfile.php'];

public function __construct($cwd = null)
{
if (!file_exists($taskfile)) {
throw new \InvalidArgumentException("$taskfile not found");
}
$this->cwd = $cwd ?: getcwd();
}

public function getCwd()
{
return $this->cwd;
}

if (filesize($taskfile) === 0) {
throw new \LogicException("Taskfile is empty");
/**
* @return string|false
*/
public function findTaskfile()
{
foreach ($this->variants as $variant) {
$file = $this->getCwd() . DIRECTORY_SEPARATOR . $variant;

if (is_file($file)) {
return $file;
}
}

return false;
}

/**
* @return Task\Project
* @throws RuntimeException
*/
public function find()
{
if (!$taskfile = $this->findTaskfile()) {
throw new \RuntimeException("No Taskfile found");
}

$project = require $taskfile;

$cls = 'Task\Project';
if (!($project instanceof $cls)) {
throw new \LogicException("Taskfile must return a Project");

if (!($project instanceof Project)) {
throw new \UnexpectedValueException("Taskfile must return an instance of 'Task\Project'");
}

return $project;
Expand Down
5 changes: 0 additions & 5 deletions tests/fixtures/Taskfile

This file was deleted.

Empty file.
3 changes: 3 additions & 0 deletions tests/fixtures/invalid-taskfile/Taskfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return "foo";
Empty file.
5 changes: 5 additions & 0 deletions tests/fixtures/valid-taskfile/Taskfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require __DIR__.'/../../../vendor/autoload.php';

return new Task\Project('cli');
37 changes: 17 additions & 20 deletions tests/src/ProjectFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,34 @@ class ProjectFinderTest extends \PHPUnit_Framework_TestCase
{
public function testFind()
{
$taskfile = __DIR__.'/../fixtures/Taskfile';
$project = (new ProjectFinder)->find($taskfile);
$this->assertEquals(require $taskfile, $project);
$dir = dirname(__DIR__) . '/fixtures/valid-taskfile';

$taskfile = $dir . '/Taskfile';

$expected = require $taskfile;

$actual = (new ProjectFinder($dir))->find();

$this->assertEquals($expected, $actual);
}

/**
* @expectedException InvalidArgumentException
* @expectedException RuntimeException
*/
public function testFindThrowsOnNoTaskFile()
{
(new ProjectFinder)->find('nope');
$dir = dirname(__DIR__) . '/fixtures/no-taskfile';

(new ProjectFinder($dir))->find();
}

/**
* @expectedException LogicException
*/
public function testFindThrowsOnEmptyTaskFile()
{
$taskfile = tempnam(sys_get_temp_dir(), 'taskfile');
(new ProjectFinder)->find($taskfile);
unlink($taskfile);
}

/**
* @expectedException LogicException
* @expectedException UnexpectedValueException
*/
public function testFindThrowsOnNotProject()
{
$taskfile = tempnam(sys_get_temp_dir(), 'taskfile');
file_put_contents($taskfile, '<?php return "foo";');
(new ProjectFinder)->find($taskfile);
unlink($taskfile);
$dir = dirname(__DIR__) . '/fixtures/invalid-taskfile';

(new ProjectFinder($dir))->find();
}
}