Skip to content

Commit

Permalink
fix: Update fingerprint function to return execution status
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo D committed Mar 4, 2024
1 parent b0913f6 commit 6c64f61
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
* Add a `yaml_dump()` function to dump any PHP value to a YAML string
* Add a `yaml_parse()` function to parse a YAML string to a PHP value
* Remove the default timeout of 60 seconds from the Context
* Add `bool` return type to `fingerprint()` function to indicate if the callable was run

## 0.13.1 (2024-02-27)

Expand Down
6 changes: 5 additions & 1 deletion examples/fingerprint.php
Expand Up @@ -47,14 +47,18 @@ function task_with_a_fingerprint_and_force(
): void {
run('echo "Hello Task with Fingerprint!"');

fingerprint(
$hasRun = fingerprint(
callback: function () {
run('echo "Cool, no fingerprint! Executing..."');
},
fingerprint: my_fingerprint_check(),
force: $force // This option will force the task to run even if the fingerprint has not changed
);

if ($hasRun) {
run('echo "Fingerprint has been executed!"');
}

run('echo "Cool! I finished!"');
}

Expand Down
16 changes: 8 additions & 8 deletions src/functions.php
Expand Up @@ -885,16 +885,16 @@ function fingerprint_save(string $fingerprint): void
GlobalHelper::getApplication()->fingerprintHelper->postProcessFingerprintForHash($fingerprint);
}

function fingerprint(callable $callback, string $fingerprint, bool $force = false): void
function fingerprint(callable $callback, string $fingerprint, bool $force = false): bool
{
if (!fingerprint_exists($fingerprint) || $force) {
try {
$callback();
fingerprint_save($fingerprint);
} catch (\Throwable $e) {
throw $e;
}
if ($force || !fingerprint_exists($fingerprint)) {
$callback();
fingerprint_save($fingerprint);

return true;
}

return false;
}

/**
Expand Down
Expand Up @@ -19,14 +19,14 @@ public function test(): void
$processSecondRun = $this->runTask(['fingerprint:task-with-a-fingerprint-and-force', '--force']);
$processThirdRun = $this->runTask(['fingerprint:task-with-a-fingerprint-and-force']);

$this->assertStringEqualsFile(__FILE__ . '.output_runnable.txt', $processFirstRun->getOutput());
$this->assertStringEqualsFile(__FILE__ . '.output_runnable.txt', $processSecondRun->getOutput());
$this->assertStringEqualsFile(__FILE__ . '.output_not_runnable.txt', $processThirdRun->getOutput());
self::assertStringEqualsFile(__FILE__ . '.output_runnable.txt', $processFirstRun->getOutput());
self::assertStringEqualsFile(__FILE__ . '.output_runnable.txt', $processSecondRun->getOutput());
self::assertStringEqualsFile(__FILE__ . '.output_not_runnable.txt', $processThirdRun->getOutput());

file_put_contents($filepath, 'Hello World');
// If we don't force, it should re-run the task
$processFourthRun = $this->runTask(['fingerprint:task-with-a-fingerprint-and-force']);
$this->assertStringEqualsFile(__FILE__ . '.output_runnable.txt', $processFourthRun->getOutput());
self::assertStringEqualsFile(__FILE__ . '.output_runnable.txt', $processFourthRun->getOutput());

foreach ([$processFirstRun, $processSecondRun, $processThirdRun, $processFourthRun] as $process) {
$this->assertSame(0, $process->getExitCode());
Expand Down
@@ -1,3 +1,4 @@
Hello Task with Fingerprint!
Cool, no fingerprint! Executing...
Fingerprint has been executed!
Cool! I finished!
Expand Up @@ -29,7 +29,7 @@ private function runProcessAndExpect(string $expectedOutputFilePath, string $wit
$process = $this->runTask(['fingerprint:task-with-complete-fingerprint-check']);

if (file_exists($expectedOutputFilePath)) {
$this->assertStringEqualsFile($expectedOutputFilePath, $process->getOutput());
self::assertStringEqualsFile($expectedOutputFilePath, $process->getOutput());
}

$this->assertSame(0, $process->getExitCode());
Expand Down
Expand Up @@ -29,7 +29,7 @@ private function runProcessAndExpect(string $expectedOutputFilePath, string $wit
$process = $this->runTask(['fingerprint:task-with-complete-fingerprint']);

if (file_exists($expectedOutputFilePath)) {
$this->assertStringEqualsFile($expectedOutputFilePath, $process->getOutput());
self::assertStringEqualsFile($expectedOutputFilePath, $process->getOutput());
}

$this->assertSame(0, $process->getExitCode());
Expand Down

0 comments on commit 6c64f61

Please sign in to comment.