From 855cfad1240deec2a7455ce616579df9edff61f6 Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Tue, 7 Jul 2015 21:27:06 +0000 Subject: [PATCH] fix #26 validate path --- composer.json | 3 ++- src/Taskwarrior.php | 50 ++++++++++++++++++++++++++++++++++----- tests/TaskwarriorTest.php | 14 +++++++++++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index f2aa76e..f08e741 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index b643b71..0de5b32 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -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 @@ -43,11 +54,14 @@ class Taskwarrior */ public function __construct($taskrc = '~/.taskrc', $taskData = '~/.task', $rcOptions = [], $bin = 'task') { - $this->bin = Path::canonicalize($bin); + $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 diff --git a/tests/TaskwarriorTest.php b/tests/TaskwarriorTest.php index 7f3c59d..2556d95 100644 --- a/tests/TaskwarriorTest.php +++ b/tests/TaskwarriorTest.php @@ -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'); + } } \ No newline at end of file