Use more advanced tests to cover full range of wordpress versions

This commit is contained in:
2025-02-09 18:09:50 +01:00
parent 21af566d20
commit 1d2bc2a087
3 changed files with 88 additions and 27 deletions

View File

@@ -1,9 +1,37 @@
#!/usr/bin/env sh #!/usr/bin/env bash
set -ex
#docker build -t localhost/tests-util build/tests-util build_tests_util() {
docker run --init \ docker build -t "${TESTS_UTIL_IMAGE:?}" build/tests-util
}
main() {
# Use docker BuildKit
DOCKER_BUILDKIT=1
export DOCKER_BUILDKIT
TESTS_UTIL_IMAGE="localhost/tests-util:latest"
export TESTS_UTIL_IMAGE
if [ "${1:-}" = "build" ]; then
echo "Rebuilding image..."
build_tests_util
elif ! docker inspect --type=image "${TESTS_UTIL_IMAGE}" > /dev/null 2>&1; then
echo "Image does not exist locally. Building..."
build_tests_util
fi
local runCommand="wp-patch-tests"
if [ "${1:-}" = "shell" ]; then
runCommand="/bin/bash"
fi
docker run --init \
--rm \ --rm \
-i \ --interactive \
-v "${PWD}:/var/www/html" \ --volume "./build/tests-util/wp-patch-tests.sh:/usr/local/bin/wp-patch-tests" \
localhost/tests-util wp-patch-tests --volume "./build/docker-bake.hcl:/data/docker-bake.hcl:ro" \
--volume "./patches:/data/patches:ro" \
"${TESTS_UTIL_IMAGE}" "${runCommand}"
}
main "${@}"

View File

@@ -30,7 +30,6 @@ COPY --from=wp-cli ["/usr/local/bin/wp", "/usr/local/bin/wp"]
# WP patch tests # WP patch tests
COPY --chmod=0777 ["./wp-patch-tests.sh", "/usr/local/bin/wp-patch-tests"] COPY --chmod=0777 ["./wp-patch-tests.sh", "/usr/local/bin/wp-patch-tests"]
# Build final image # Build final image
ARG PHP_VERSION ARG PHP_VERSION
ARG WP_VERSION ARG WP_VERSION
@@ -38,8 +37,11 @@ FROM --platform=${TARGETPLATFORM} wordpress:${WP_VERSION}-php${PHP_VERSION}-fpm-
RUN set -eux \ RUN set -eux \
&& apk add --update --no-cache git patch less \ && apk add --update --no-cache gawk git grep patch less \
&& git config --global --add safe.directory /var/www/html \ && git config --global --add safe.directory /var/www/html \
&& echo "memory_limit = 512M" > /usr/local/etc/php/conf.d/memory-limit.ini \
&& ln -sf /var/www/composer/vendor/bin/parallel-lint /usr/local/bin/php-parallel-lint && ln -sf /var/www/composer/vendor/bin/parallel-lint /usr/local/bin/php-parallel-lint
COPY --from=rootfs ["/", "/"] COPY --from=rootfs ["/", "/"]
WORKDIR /data

View File

@@ -21,14 +21,16 @@ function getFileCount() {
} }
function taskPrepareWpPatch() { function taskPrepareWpPatch() {
local patchDir="${1:?}" set -eou pipefail
local downloadRoot="${2:?}"
local wpLongVersion="${1:?}"
local patchDir="${2:?}"
local downloadRoot="${3:?}"
local wpLongVersion # Define variables
local wpShortVersion local wpShortVersion
local downloadPath local downloadPath
wpLongVersion="$(basename "${patchDir}")"
wpShortVersion="$(echo "${wpLongVersion}" | sed --expression='s/.0$//g')" wpShortVersion="$(echo "${wpLongVersion}" | sed --expression='s/.0$//g')"
downloadPath="${downloadRoot}/${wpLongVersion}" downloadPath="${downloadRoot}/${wpLongVersion}"
@@ -39,11 +41,23 @@ function taskPrepareWpPatch() {
echo "> Applying patch" echo "> Applying patch"
patch "${downloadPath}/wp-admin/update-core.php" <"${patchDir}/wp-admin-update-core.patch" patch "${downloadPath}/wp-admin/update-core.php" <"${patchDir}/wp-admin-update-core.patch"
mv -v "${downloadPath}/wp-admin/update-core.php" "${PHP_TESTS_DIR}/update-core-${wpLongVersion}.php" cp -v "${downloadPath}/wp-admin/update-core.php" "${PHP_TESTS_DIR}/update-core-${wpLongVersion}.php"
rm "${downloadPath}" -rf rm "${downloadPath}" -rf
} }
function extractVersionsFromHCL() {
local hclFile="${1:?HCL file path required}"
gawk '
/args *= *get-args/ {
if (match($0, /get-args\("([0-9]+\.[0-9]+\.[0-9]+)", *"([0-9]+\.[0-9]+\.[0-9]+)"\)/, arr)) {
print arr[1], arr[2];
}
}
' "${hclFile}"
}
main() { main() {
PHP_TESTS_DIR="/data/test_files" PHP_TESTS_DIR="/data/test_files"
export PHP_TESTS_DIR export PHP_TESTS_DIR
@@ -52,20 +66,35 @@ main() {
local patchDir local patchDir
# For each patch, download appropriate WP version, apply patch and check if file syntax is correct afterwards local wpLongVersion
for patchDir in patches/*/; do local wpPatchVersion
echo "> Deploying task ${patchDir}" local expectedPatchCount=0
declare -a versions
mapfile -t versions < <(extractVersionsFromHCL /data/docker-bake.hcl)
for version in "${versions[@]}"; do
wpLongVersion=$(echo "$version" | awk '{print $1}')
wpPatchVersion=$(echo "$version" | awk '{print $2}')
patchDir="/data/patches/${wpPatchVersion}"
printf "Deploying task [Version: %s, Patch: %s, Path: %s]\n" "${wpLongVersion}" "${wpPatchVersion}" "${patchDir}"
# Introduce ~50ms overhead before deploying another task # Introduce ~50ms overhead before deploying another task
# Even shorter overhead helps. but better to be on safe side. # Even shorter overhead helps. but better to be on safe side.
# This should prevent concurrency issues # This should prevent concurrency issues
sleep 0.05 sleep 0.05
# Run task concurrently if [ ! -d "${patchDir}" ]; then
taskPrepareWpPatch "${patchDir}" "/data/wp_src" & printf "Error: Patch directory not found: %s\n" "${patchDir}"
return 1
fi
# Start the task in the background and capture the PID
taskPrepareWpPatch "${wpLongVersion}" "${patchDir}" "/data/wp_src" &
((expectedPatchCount++))
done done
echo "Waiting for all tasks to finish..." echo "Waiting for all tasks to complete..."
wait wait
# Make sure that directory is not empty # Make sure that directory is not empty
@@ -74,21 +103,23 @@ main() {
return 1 return 1
fi fi
local numberOfPatches
local numberOfTestFiles local numberOfTestFiles
numberOfPatches="$(getFileCount patches)"
numberOfTestFiles="$(getFileCount "${PHP_TESTS_DIR}")" numberOfTestFiles="$(getFileCount "${PHP_TESTS_DIR}")"
if [ "${numberOfPatches}" != "${numberOfTestFiles}" ]; then if [ "${expectedPatchCount}" != "${numberOfTestFiles}" ]; then
echo "> Error - Unexpected number of files" echo "> Error - Unexpected number of files"
echo " Expected: ${numberOfPatches}" echo " Expected: ${expectedPatchCount}"
echo " Actual: ${numberOfTestFiles}" echo " Actual: ${numberOfTestFiles}"
return 1 return 1
fi fi
# Run php-lint on resulting patch files # Run php-lint on resulting patch files
php-parallel-lint "${PHP_TESTS_DIR}" -s --blame --exclude vendor -p php if php-parallel-lint "${PHP_TESTS_DIR}" -s --blame --exclude vendor -p php; then
return $? printf "> Success. All of the %d generated patch files were valid.\n" "${expectedPatchCount}"
return 0
fi
return 1
} }
main "${@}" main "${@}"