diff --git a/__tests__/darwin-version.test.ts b/__tests__/darwin-version.test.ts new file mode 100644 index 00000000..38aa2710 --- /dev/null +++ b/__tests__/darwin-version.test.ts @@ -0,0 +1,62 @@ +import {execFileSync} from 'node:child_process'; +import * as fs from 'node:fs'; + +describe('macOS installed PHP version comparison', () => { + const functions = fs + .readFileSync('src/scripts/darwin.sh', 'utf8') + .split('# Variables')[0]; + + it.each([ + ['8.7.0-dev', '8.7.0', 'Found'], + ['8.6.0-dev', '8.6.0', 'Found'], + ['8.7.0-dev', '8.7.0-dev', 'Found'], + ['8.5.9', '8.5.9', 'Found'], + ['8.5.9', '8.5.11', 'Upgraded'], + ['8.6.0RC1', '8.6.0', 'Upgraded'], + ['8.7.0-dev', '8.7.1', 'Upgraded'] + ])('handles runtime %s with formula %s', (runtime, formula, expected) => { + // Exercise setup_php's real selection path while isolating system writes + // and the configuration stage from this version-comparison regression. + const output = execFileSync( + 'bash', + [ + '-c', + `${functions} +version="$REQUESTED" +debug=release ts=nts old_versions='^5\\.[3-5]$' +src=/unused RUNNER_TOOL_CACHE=/unused tool_path_dir=/unused tick=ok cross=error +step_log() { :; } +check_pre_installed() { :; } +get_brewed_php() { echo "$RUNTIME"; } +php_semver() { echo "$RUNTIME"; } +brew() { test "$1" = info || exit 2; echo "$FORMULA"; } +jq() { cat; } +add_php() { printf '%s\n' "$1" >&3; } +php-config() { :; } +sed() { echo /unused; } +sudo() { :; } +php_ini_path() { echo /unused; } +get_scan_dir() { echo /unused; } +php_extra_version() { :; } +configure_php() { :; } +link_opcache() { :; } +set_output() { :; } +add_log() { printf '%s\n' "$*"; } +setup_php 3>&1 +` + ], + { + encoding: 'utf8', + env: { + ...process.env, + REQUESTED: runtime.split('.').slice(0, 2).join('.'), + RUNTIME: runtime, + FORMULA: formula + } + } + ); + expect(output).toContain(`${expected} PHP ${runtime}`); + if (expected === 'Upgraded') expect(output).toContain('upgrade\n'); + else expect(output).not.toContain('upgrade\n'); + }); +}); diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index f10fda4a..f8145a12 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -286,7 +286,8 @@ setup_php() { status="Installed" elif [[ "${existing_version:0:3}" = "$version" && "${update:?}" = "true" ]]; then brew_php_version="$(brew info --json "php@$version" 2>/dev/null | jq -r '.[].versions.stable')" - if [ "$brew_php_version" != "$existing_version" ]; then + # Homebrew records development snapshots without PHP's runtime -dev suffix. + if [ "$brew_php_version" != "$existing_version" ] && [ "$brew_php_version" != "${existing_version%-dev}" ]; then add_php "upgrade" "$existing_version" >/dev/null 2>&1 || { add_log "${cross:?}" "PHP" "Could not upgrade PHP $version" exit 1