Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type hasfreespace condition #1653

Merged
merged 40 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fe38a90
Merge remote-tracking branch 'upstream/master'
jawira Feb 11, 2019
974a2f1
Merge remote-tracking branch 'upstream/master'
jawira Jul 1, 2019
c3b1e48
Merge remote-tracking branch 'upstream/master'
jawira Jul 12, 2019
32e4914
Merge remote-tracking branch 'upstream/master'
jawira Jul 26, 2019
275cc96
Merge remote-tracking branch 'upstream/master'
jawira Oct 23, 2019
4de09d3
ComposerTask: update code & doc #1163
jawira Oct 23, 2019
64aefbf
Merge branch 'master' of github.com:jawira/fork-phing
jawira Nov 19, 2019
d769c90
Merge remote-tracking branch 'upstream/master'
jawira Nov 19, 2019
9d8da67
Merge remote-tracking branch 'upstream/master'
jawira Aug 8, 2020
2549e00
Merge remote-tracking branch 'upstream/master'
jawira Sep 5, 2020
b57b6da
Merge remote-tracking branch 'upstream/master'
jawira Sep 29, 2020
ec4ab93
Merge remote-tracking branch 'upstream/master'
jawira Oct 17, 2020
8f5343c
Merge remote-tracking branch 'upstream/master'
jawira Oct 29, 2020
d098a0f
Merge remote-tracking branch 'upstream/master'
jawira Dec 14, 2020
c7bcabf
Merge remote-tracking branch 'upstream/master'
jawira Dec 19, 2020
919e771
Merge remote-tracking branch 'upstream/main' into main
jawira Feb 2, 2021
424a754
Merge remote-tracking branch 'upstream/main'
jawira Mar 9, 2021
f79dc57
Merge remote-tracking branch 'upstream/main'
jawira Mar 16, 2021
12c46e9
Merge remote-tracking branch 'upstream/main' into main
jawira Apr 19, 2021
605d358
Merge remote-tracking branch 'upstream/main' into main
jawira Apr 26, 2021
342c9ac
Merge remote-tracking branch 'upstream/main' into main
jawira May 4, 2021
8451ae1
Merge remote-tracking branch 'upstream/main' into main
jawira May 16, 2021
bd5938e
Merge remote-tracking branch 'upstream/main' into main
jawira Jun 12, 2021
180cea1
Merge branch 'phingofficial:main' into main
jawira Jul 15, 2021
3fc32f0
Merge branch 'main' of github.com:jawira/fork-phing into main
jawira Jul 15, 2021
7f4b766
Merge branch 'phingofficial:main' into main
jawira Sep 1, 2021
be081b8
Merge branch 'phingofficial:main' into main
jawira Sep 2, 2021
642f834
Merge branch 'phingofficial:main' into main
jawira Sep 5, 2021
32abe87
Merge branch 'phingofficial:main' into main
jawira Sep 11, 2021
4874f43
Merge branch 'phingofficial:main' into main
jawira Sep 19, 2021
552975b
Merge branch 'phingofficial:main' into main
jawira Sep 21, 2021
d0f2c6f
Merge branch 'phingofficial:main' into main
jawira Oct 7, 2021
e620b41
Merge branch 'phingofficial:main' into main
jawira Nov 23, 2021
d20c869
Merge branch 'phingofficial:main' into main
jawira Dec 2, 2021
00658ec
HasFreeSpace: add types and tests
jawira Dec 16, 2021
b07d760
Merge branch 'main' into has-free-space
jawira Dec 16, 2021
f2a1815
Check disk_free_space returns float
jawira Dec 27, 2021
c60d1ae
Catch throwable when error reporting is enabled
jawira Jan 1, 2022
2d679a2
Add rationale behind "catch"
jawira Jan 18, 2022
2bf5e64
Merge branch 'main' into has-free-space
jawira Jan 18, 2022
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
22 changes: 15 additions & 7 deletions src/Phing/Task/System/Condition/HasFreeSpaceCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -44,38 +45,45 @@ 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);
}

/**
* Set the partition/device to check.
*/
public function setPartition(string $partition)
public function setPartition(string $partition): void
{
$this->partition = $partition;
}

/**
* Set the amount of free space required.
*/
public function setNeeded(string $needed)
public function setNeeded(string $needed): void
{
$this->needed = $needed;
}

/**
* @throws BuildException
*/
private function validate()
private function validate(): void
{
if (null == $this->partition) {
throw new BuildException('Please set the partition attribute.');
Expand Down
40 changes: 40 additions & 0 deletions tests/Phing/Task/System/Condition/HasFreeSpaceConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Phing\Task\System\Condition;

use Phing\Test\Support\BuildFileTest;

class HasFreeSpaceConditionTest extends BuildFileTest
{
public function setUp(): void
{
$this->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.');
}
}
53 changes: 53 additions & 0 deletions tests/etc/tasks/system/HasFreeSpaceConditionTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8" ?>

<project name="HasFreeSpaceConditionTest" default="testPartitionNotSet">

<if>
<os family="windows"/>
<then>
<property name="my.partition" value="C:"/>
</then>
<else>
<property name="my.partition" value="/"/>
</else>
</if>

<target name="testPartitionNotSet">
<if>
<hasfreespace/>
</if>
</target>

<target name="testNeededNotSet">
<if>
<hasfreespace partition="${my.partition}"/>
</if>
</target>

<target name="testInvalidPartition">
<if>
<hasfreespace partition="foo" needed="1M"/>
</if>
</target>

<target name="testEnoughSpace">
<if>
<hasfreespace partition="${my.partition}" needed="1M"/>
<then>
<echo>HasFreeSpaceConditionTest: Enough space in disk.</echo>
</then>
</if>
</target>

<target name="testNotEnoughSpace">
<if>
<not>
<hasfreespace partition="${my.partition}" needed="900TiB"/>
</not>
<then>
<echo>HasFreeSpaceConditionTest: Not enough space in disk.</echo>
</then>
</if>
</target>

</project>