Skip to content

Commit

Permalink
fix: Pre-commit script and static analysis error
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 30, 2024
1 parent 52442b5 commit c2dc212
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
22 changes: 10 additions & 12 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,32 @@ print_result() {
run_command() {
print_task "$1"
start_time=$(date +%s.%N)
output=$($2 2>&1) || true
output=$($2 2>&1)
exit_code=$?
end_time=$(date +%s.%N)
duration=$(echo "$end_time - $start_time" | bc)

# Debug: Print full command output
# echo "Debug: Command output for $1:" >&2
# echo "$output" >&2
# echo "Debug: End of command output for $1" >&2
#echo "Debug: Command output for $1:" >&2
#echo "$output" >&2
#echo "Debug: End of command output for $1" >&2

if [ "$1" = "Running static analysis" ]; then
if echo "$output" | grep -q "\[ERROR\]"; then
if [ "$1" = "Running Static Analysis" ]; then
if [ $exit_code -ne 0 ] || echo "$output" | grep -q "\[ERROR\]"; then
print_result 1
printf " ${RED}(%.2fs)${NC}\n" "$duration"
printf "\n${RED}Static Analysis Error:${NC}\n"
echo "$output" | sed -n '/^-----/,/^-----/p' | while IFS= read -r line; do
printf "${YELLOW}%s${NC}\n" "$line"
done
echo "$output" | grep "\[ERROR\]" | while IFS= read -r line; do
printf "${RED}%s${NC}\n" "$line"
done
return 1
elif [ $exit_code -eq 0 ]; then
else
print_result 0
printf " ${GREEN}(%.2fs) No errors found${NC}\n" "$duration"
return 0
else
print_result 1
printf " ${RED}(%.2fs)${NC}\n" "$duration"
printf "\n${RED}Error output:${NC}\n%s\n\n" "$output"
return 1
fi
elif [ "$1" = "Running Pest tests" ] || [ "$1" = "Running Dusk tests" ]; then
if echo "$output" | grep -q "No \"dirty\" tests found"; then
Expand Down
14 changes: 8 additions & 6 deletions app/Console/Commands/RefreshApplicationForDeployment.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use Exception;
Expand Down Expand Up @@ -117,7 +119,7 @@ private function performRefreshSteps(): void
*/
private function checkAndClearVersionCache(): void
{
$this->components->task('Checking version cache', function () {
$this->components->task('Checking version cache', function (): string|true {
$currentVersion = $this->getCurrentVersion();
$cachedVersion = Cache::get($this->versionCacheKey);

Expand Down Expand Up @@ -156,7 +158,7 @@ private function getCurrentVersion(): ?string

$version = trim(File::get($path));

if (empty($version)) {
if ($version === '' || $version === '0') {
$this->components->warn("Version file is empty: {$this->versionFile}");

return null;
Expand Down Expand Up @@ -231,13 +233,13 @@ private function optimizeApplication(): void
/**
* Run an Artisan command and handle its output.
*
* @param string $description The description of the task
* @param string $command The Artisan command to run
* @param array<string, mixed> $parameters The parameters for the Artisan command
* @param string $description The description of the task
* @param string $command The Artisan command to run
* @param array<string, mixed> $parameters The parameters for the Artisan command
*/
private function runArtisanCommand(string $description, string $command, array $parameters = []): void
{
$this->components->task($description, function () use ($command, $parameters) {
$this->components->task($description, function () use ($command, $parameters): bool {
try {
$this->info("Running Artisan command: {$command}");
Artisan::call($command, $parameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
<?php

declare(strict_types=1);

use App\Console\Commands\RefreshApplicationForDeployment;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

beforeEach(function () {
beforeEach(function (): void {
$this->command = new RefreshApplicationForDeployment;

// Create a new array cache store for testing
$this->cache = Cache::store('array');
});

it('fails when in debug mode without force option', function () {
it('fails when in debug mode without force option', function (): void {
config(['app.debug' => true]);

$this->artisan(RefreshApplicationForDeployment::class)
->assertFailed();
});

it('succeeds when not in debug mode', function () {
it('succeeds when not in debug mode', function (): void {
config(['app.debug' => false]);

$this->artisan(RefreshApplicationForDeployment::class)
->assertSuccessful();
});

it('skips migrations when --skip-migrations option is used', function () {
it('skips migrations when --skip-migrations option is used', function (): void {
config(['app.debug' => false]);

$this->artisan(RefreshApplicationForDeployment::class, ['--skip-migrations' => true])
->expectsOutputToContain('Skipping migrations.')
->assertSuccessful();
});

it('skips cache operations when --skip-cache option is used', function () {
it('skips cache operations when --skip-cache option is used', function (): void {
config(['app.debug' => false]);

$this->artisan(RefreshApplicationForDeployment::class, ['--skip-cache' => true])
->expectsOutputToContain('Skipping cache operations.')
->assertSuccessful();
});

it('clears version cache when versions do not match', function () {
it('clears version cache when versions do not match', function (): void {
config(['app.debug' => false]);

File::shouldReceive('exists')->andReturn(true);
Expand All @@ -55,7 +57,7 @@
expect($this->cache->has('vanguard_version'))->toBeFalse();
});

it('does not clear version cache when versions match', function () {
it('does not clear version cache when versions match', function (): void {
config(['app.debug' => false]);

File::shouldReceive('exists')->andReturn(true);
Expand All @@ -68,14 +70,14 @@
expect($this->cache->has('vanguard_version'))->toBeTrue();
});

it('logs deployment details', function () {
it('logs deployment details', function (): void {
config(['app.debug' => false]);
File::shouldReceive('exists')->andReturn(true);
File::shouldReceive('get')->andReturn('1.0.0');

Log::shouldReceive('info')
->once()
->withArgs(fn ($message) => str_contains($message, 'Deployment completed. Version: 1.0.0'));
->withArgs(fn ($message): bool => str_contains((string) $message, 'Deployment completed. Version: 1.0.0'));

$this->artisan(RefreshApplicationForDeployment::class)->assertSuccessful();
});

0 comments on commit c2dc212

Please sign in to comment.