Skip to content

Commit

Permalink
Merge pull request #1656 from hydephp/build-task-skip-feature
Browse files Browse the repository at this point in the history
Add a skip feature to build tasks hydephp/develop@f9d1131
  • Loading branch information
github-actions committed Apr 13, 2024
1 parent eaac97c commit df39276
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/Framework/Features/BuildTasks/BuildTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ public function run(?OutputStyle $output = null): int
$this->handle();
$this->printFinishMessage();
} catch (Throwable $exception) {
$this->writeln('<error>Failed</error>');
$this->writeln("<error>{$exception->getMessage()}</error>");
if ($exception instanceof BuildTaskSkippedException) {
$this->writeln('<bg=yellow>Skipped</>');
$this->writeln("<fg=gray> > {$exception->getMessage()}</>");
} else {
$this->writeln('<error>Failed</error>');
$this->writeln("<error>{$exception->getMessage()}</error>");
}

$this->exitCode = $exception->getCode();
}

Expand Down Expand Up @@ -84,6 +90,16 @@ public function writeln(string $message): void
$this->output?->writeln($message);
}

/**
* Write a fluent message to the output that the task is skipping and halt the execution.
*
* @throws \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException
*/
public function skip(string $reason = 'Task was skipped'): void
{
throw new BuildTaskSkippedException($reason);
}

/** Write a fluent message to the output that the task created the specified file. */
public function createdSiteFile(string $path): static
{
Expand Down
15 changes: 15 additions & 0 deletions src/Framework/Features/BuildTasks/BuildTaskSkippedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Features\BuildTasks;

use RuntimeException;

class BuildTaskSkippedException extends RuntimeException
{
public function __construct(string $message = 'Task was skipped', int $code = 0)
{
parent::__construct($message, $code);
}
}
51 changes: 51 additions & 0 deletions tests/Unit/BuildTaskSkippedExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit;

use Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException;
use Hyde\Testing\UnitTestCase;

/**
* @covers \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException
*/
class BuildTaskSkippedExceptionTest extends UnitTestCase
{
public function testItCanBeInstantiated()
{
$exception = new BuildTaskSkippedException();

$this->assertInstanceOf(BuildTaskSkippedException::class, $exception);
}

public function testItThrowsAnExceptionWithDefaultMessage()
{
$this->expectException(BuildTaskSkippedException::class);
$this->expectExceptionMessage('Task was skipped');

throw new BuildTaskSkippedException();
}

public function testItThrowsAnExceptionWithCustomMessage()
{
$this->expectException(BuildTaskSkippedException::class);
$this->expectExceptionMessage('Custom message');

throw new BuildTaskSkippedException('Custom message');
}

public function testDefaultExceptionCode()
{
$exception = new BuildTaskSkippedException();

$this->assertSame(0, $exception->getCode());
}

public function testCustomExceptionCode()
{
$exception = new BuildTaskSkippedException('Custom message', 123);

$this->assertSame(123, $exception->getCode());
}
}
32 changes: 30 additions & 2 deletions tests/Unit/BuildTaskUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,32 @@ public function testWithExecutionTime()
$this->assertSame(' in 1,234.56ms', $task->buffer[0]);
}

public function testTaskSkipping()
{
$task = tap(new BufferedTestBuildTask(), function (BufferedTestBuildTask $task) {
$task->mockHandle(function (BufferedTestBuildTask $task) {
$task->skip();
})->run();
});

$this->assertSame(0, $task->property('exitCode'));
$this->assertSame('<bg=yellow>Skipped</>', $task->buffer[1]);
$this->assertSame('<fg=gray> > Task was skipped</>', $task->buffer[2]);
}

public function testTaskSkippingWithCustomMessage()
{
$task = tap(new BufferedTestBuildTask(), function (BufferedTestBuildTask $task) {
$task->mockHandle(function (BufferedTestBuildTask $task) {
$task->skip('Custom reason');
})->run();
});

$this->assertSame(0, $task->property('exitCode'));
$this->assertSame('<bg=yellow>Skipped</>', $task->buffer[1]);
$this->assertSame('<fg=gray> > Custom reason</>', $task->buffer[2]);
}

public function testExceptionHandling()
{
$task = new BufferedTestBuildTask();
Expand Down Expand Up @@ -217,7 +243,7 @@ class InspectableTestBuildTask extends BuildTask
public function handle(): void
{
if (isset($this->mockHandle)) {
($this->mockHandle)();
($this->mockHandle)($this);
} else {
$this->wasHandled = true;
}
Expand Down Expand Up @@ -248,9 +274,11 @@ public function mockClock(float $time): void
$this->mockedEndTime = $time;
}

public function mockHandle(Closure $handle): void
public function mockHandle(Closure $handle): static
{
$this->mockHandle = $handle;

return $this;
}

protected function stopClock(): float
Expand Down

0 comments on commit df39276

Please sign in to comment.