Use composer.json as version/build source and stamp build in CI
All checks were successful
CI/CD Pipeline / stamp_build (push) Successful in 3s
CI/CD Pipeline / deploy (push) Successful in 24s
CI/CD Pipeline / promote_stable (push) Successful in 3s

This commit is contained in:
2026-02-24 22:55:49 +01:00
parent 16e0444fa3
commit 225dc391ff
3 changed files with 65 additions and 132 deletions

View File

@@ -11,17 +11,6 @@ namespace App\Console\Commands {
return \file_get_contents($path);
}
}
if (!function_exists(__NAMESPACE__ . '\\json_encode')) {
function json_encode($value, int $flags = 0): string|false
{
if (!empty($GLOBALS['version_fetch_json_encode_false']) && is_array($value) && array_key_exists('build', $value)) {
return false;
}
return \json_encode($value, $flags);
}
}
}
namespace {
@@ -40,87 +29,47 @@ namespace {
file_put_contents($path, $original);
}
$GLOBALS['version_fetch_file_get_contents_false'] = false;
$GLOBALS['version_fetch_json_encode_false'] = false;
$originalPath = $GLOBALS['version_fetch_path'] ?? null;
if ($originalPath !== null) {
putenv("PATH={$originalPath}");
$_ENV['PATH'] = $originalPath;
$_SERVER['PATH'] = $originalPath;
unset($GLOBALS['version_fetch_path']);
}
}
}
it('fetches build count and syncs composer metadata', function (): void {
it('syncs version and build from composer metadata', function (): void {
withComposerBackupForFetch(function (): void {
Setting::updateOrCreate(['key' => 'version'], ['value' => '0.0.0']);
Setting::updateOrCreate(['key' => 'build'], ['value' => '0']);
$composer = json_decode((string) file_get_contents(base_path('composer.json')), true);
$expectedVersion = (string) ($composer['version'] ?? '');
$expectedBuild = (string) ($composer['build'] ?? '');
$exitCode = Artisan::call('version:fetch');
expect($exitCode)->toBe(0);
$build = Setting::where('key', 'build')->value('value');
expect(is_numeric($build))->toBeTrue();
expect(Setting::where('key', 'version')->value('value'))->toBe($expectedVersion);
});
});
it('fails when build count cannot be resolved', function (): void {
withComposerBackupForFetch(function (): void {
$GLOBALS['version_fetch_path'] = getenv('PATH') ?: '';
putenv('PATH=/nope');
$_ENV['PATH'] = '/nope';
$_SERVER['PATH'] = '/nope';
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
$exitCode = Artisan::call('version:fetch');
expect($exitCode)->toBe(1);
expect(Setting::where('key', 'build')->value('value'))->toBe($expectedBuild);
});
});
it('fails when composer.json cannot be decoded', function (): void {
withComposerBackupForFetch(function (string $path): void {
file_put_contents($path, 'not-json');
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
$exitCode = Artisan::call('version:fetch');
expect($exitCode)->toBe(1);
});
});
it('fails when composer.json is not readable', function (): void {
it('fails when composer.json is missing build', function (): void {
withComposerBackupForFetch(function (string $path): void {
chmod($path, 0000);
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
$data = json_decode((string) file_get_contents($path), true);
unset($data['build']);
file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL);
$exitCode = Artisan::call('version:fetch');
expect($exitCode)->toBe(1);
chmod($path, 0644);
});
});
it('fails when file_get_contents returns false', function (): void {
withComposerBackupForFetch(function (): void {
$GLOBALS['version_fetch_file_get_contents_false'] = true;
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
$exitCode = Artisan::call('version:fetch');
expect($exitCode)->toBe(1);
});
});
it('fails when json_encode returns false', function (): void {
withComposerBackupForFetch(function (): void {
$GLOBALS['version_fetch_json_encode_false'] = true;
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
$exitCode = Artisan::call('version:fetch');
expect($exitCode)->toBe(1);
});