diff --git a/src/Phing/Task/System/Condition/HasFreeSpaceCondition.php b/src/Phing/Task/System/Condition/HasFreeSpaceCondition.php index 6a7d177b4e..a3e9f73630 100644 --- a/src/Phing/Task/System/Condition/HasFreeSpaceCondition.php +++ b/src/Phing/Task/System/Condition/HasFreeSpaceCondition.php @@ -22,6 +22,7 @@ use Phing\Exception\BuildException; use Phing\Util\SizeHelper; +use Throwable; /** * Condition returns true if selected partition has the requested space, false otherwise. @@ -44,14 +45,21 @@ class HasFreeSpaceCondition implements Condition * {@inheritdoc} * * @throws BuildException - * - * @return bool */ - public function evaluate() + public function evaluate(): bool { $this->validate(); - $free = disk_free_space($this->partition); + try { + $free = disk_free_space($this->partition); + } catch (Throwable $throwable) { + // Only when "display errors" is enabled. + throw new BuildException($throwable->getMessage()); + } + + if (false === $free) { + throw new BuildException('Error while retrieving free space.'); + } return $free >= SizeHelper::fromHumanToBytes($this->needed); } @@ -59,7 +67,7 @@ public function evaluate() /** * Set the partition/device to check. */ - public function setPartition(string $partition) + public function setPartition(string $partition): void { $this->partition = $partition; } @@ -67,7 +75,7 @@ public function setPartition(string $partition) /** * Set the amount of free space required. */ - public function setNeeded(string $needed) + public function setNeeded(string $needed): void { $this->needed = $needed; } @@ -75,7 +83,7 @@ public function setNeeded(string $needed) /** * @throws BuildException */ - private function validate() + private function validate(): void { if (null == $this->partition) { throw new BuildException('Please set the partition attribute.'); diff --git a/tests/Phing/Task/System/Condition/HasFreeSpaceConditionTest.php b/tests/Phing/Task/System/Condition/HasFreeSpaceConditionTest.php new file mode 100644 index 0000000000..831e11b976 --- /dev/null +++ b/tests/Phing/Task/System/Condition/HasFreeSpaceConditionTest.php @@ -0,0 +1,40 @@ +configureProject(PHING_TEST_BASE . '/etc/tasks/system/HasFreeSpaceConditionTest.xml'); + } + + public function testPartitionNotSet() + { + $this->expectBuildExceptionContaining(__FUNCTION__, __FUNCTION__, 'Please set the partition attribute.'); + } + + public function testNeededNotSet() + { + $this->expectBuildExceptionContaining(__FUNCTION__, __FUNCTION__, 'Please set the needed attribute.'); + } + + public function testInvalidPartition() + { + $this->expectBuildExceptionContaining(__FUNCTION__, __FUNCTION__, 'disk_free_space(): No such file or directory'); + } + + public function testEnoughSpace() + { + $this->executeTarget(__FUNCTION__); + $this->assertInLogs('HasFreeSpaceConditionTest: Enough space in disk.'); + } + + public function testNotEnoughSpace() + { + $this->executeTarget(__FUNCTION__); + $this->assertInLogs('HasFreeSpaceConditionTest: Not enough space in disk.'); + } +} diff --git a/tests/etc/tasks/system/HasFreeSpaceConditionTest.xml b/tests/etc/tasks/system/HasFreeSpaceConditionTest.xml new file mode 100644 index 0000000000..d688bc43cf --- /dev/null +++ b/tests/etc/tasks/system/HasFreeSpaceConditionTest.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + HasFreeSpaceConditionTest: Enough space in disk. + + + + + + + + + + + HasFreeSpaceConditionTest: Not enough space in disk. + + + + +