fix #26 validate path

This commit is contained in:
DavidBadura 2015-07-07 21:27:06 +00:00
parent 1fa3824ea5
commit 855cfad124
3 changed files with 60 additions and 7 deletions

View File

@ -22,7 +22,8 @@
"symfony/filesystem": "^2.3",
"nesbot/carbon": "^1.14",
"doctrine/collections": "^1.3",
"webmozart/path-util": "^2.0"
"webmozart/path-util": "^2.0",
"webmozart/assert": "^1.0.1"
},
"require-dev": {
"phpunit/phpunit": "^4.0",

View File

@ -7,6 +7,7 @@ use DavidBadura\Taskwarrior\Exception\CommandException;
use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;
use Webmozart\Assert\Assert;
use Webmozart\PathUtil\Path;
/**
@ -15,14 +16,24 @@ use Webmozart\PathUtil\Path;
class Taskwarrior
{
/**
* @var array
* @var string
*/
private $rcOptions;
private $bin;
/**
* @var string
*/
private $bin;
private $taskrc;
/**
* @var string
*/
private $taskData;
/**
* @var array
*/
private $rcOptions;
/**
* @var string
@ -44,10 +55,13 @@ class Taskwarrior
public function __construct($taskrc = '~/.taskrc', $taskData = '~/.task', $rcOptions = [], $bin = 'task')
{
$this->bin = Path::canonicalize($bin);
$this->taskrc = Path::canonicalize($taskrc);
$this->taskData = Path::canonicalize($taskData);
$this->rcOptions = array_merge(
array(
'rc:' . Path::canonicalize($taskrc),
'rc.data.location=' . Path::canonicalize($taskData),
'rc:' . $this->taskrc,
'rc.data.location=' . $this->taskData,
'rc.json.array=true',
'rc.confirmation=no',
),
@ -57,6 +71,14 @@ class Taskwarrior
if (version_compare($this->version(), '2.4.3') < 0) {
throw new TaskwarriorException(sprintf("Taskwarrior version %s isn't supported", $this->version()));
}
try {
Assert::readable($this->taskrc);
Assert::readable($this->taskData);
Assert::writable($this->taskData);
} catch (\InvalidArgumentException $e) {
throw new TaskwarriorException($e->getMessage(), $e->getCode(), $e);
}
}
/**
@ -218,6 +240,22 @@ class Taskwarrior
return $this->version;
}
/**
* @return string
*/
public function getTaskrcPath()
{
return $this->taskrc;
}
/**
* @return string
*/
public function getTaskDataPath()
{
return $this->taskData;
}
/**
* @return Config
* @throws CommandException

View File

@ -37,4 +37,18 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($config->has('urgency.age.max'));
$this->assertEquals('365', $config->get('urgency.age.max'));
}
public function testTaskrcNotFound()
{
$this->setExpectedException('DavidBadura\Taskwarrior\Exception\TaskwarriorException');
new Taskwarrior('/not/found/.taskrc', __DIR__ . '/.task');
}
public function testTaskDataNotFound()
{
$this->setExpectedException('DavidBadura\Taskwarrior\Exception\TaskwarriorException');
new Taskwarrior(__DIR__ . '/.taskrc', '/not/found/.task');
}
}