From 6c64f61a0f50294b381fc628c844d0c0fae90bdb Mon Sep 17 00:00:00 2001 From: Theo D Date: Fri, 1 Mar 2024 22:11:50 +0100 Subject: [PATCH] fix: Update `fingerprint` function to return execution status --- CHANGELOG.md | 1 + examples/fingerprint.php | 6 +++++- src/functions.php | 16 ++++++++-------- ...ngerprintTaskWithAFingerprintAndForceTest.php | 8 ++++---- ...ngerprintAndForceTest.php.output_runnable.txt | 1 + .../FingerprintTaskWithAFingerprintTest.php | 2 +- ...ingerprintTaskWithCompleteFingerprintTest.php | 2 +- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6649c7d..5111371e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/examples/fingerprint.php b/examples/fingerprint.php index 929e33b2..cacf843c 100644 --- a/examples/fingerprint.php +++ b/examples/fingerprint.php @@ -47,7 +47,7 @@ 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..."'); }, @@ -55,6 +55,10 @@ function task_with_a_fingerprint_and_force( 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!"'); } diff --git a/src/functions.php b/src/functions.php index 29e1cf67..19436ce2 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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; } /** diff --git a/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php b/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php index b02ccb7b..3b8074dc 100644 --- a/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php +++ b/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php @@ -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()); diff --git a/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php.output_runnable.txt b/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php.output_runnable.txt index 00f0ed61..4eb89b59 100644 --- a/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php.output_runnable.txt +++ b/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php.output_runnable.txt @@ -1,3 +1,4 @@ Hello Task with Fingerprint! Cool, no fingerprint! Executing... +Fingerprint has been executed! Cool! I finished! diff --git a/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintTest.php b/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintTest.php index d57cf7b7..3bfddefe 100644 --- a/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintTest.php +++ b/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintTest.php @@ -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()); diff --git a/tests/Examples/Fingerprint/FingerprintTaskWithCompleteFingerprintTest.php b/tests/Examples/Fingerprint/FingerprintTaskWithCompleteFingerprintTest.php index 1b286ed9..7c5465df 100644 --- a/tests/Examples/Fingerprint/FingerprintTaskWithCompleteFingerprintTest.php +++ b/tests/Examples/Fingerprint/FingerprintTaskWithCompleteFingerprintTest.php @@ -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());