From b5bfbff2d7d60d10db8d555297f8e64c7926a7fd Mon Sep 17 00:00:00 2001 From: Michael Day Date: Thu, 2 Jan 2025 15:51:26 -0600 Subject: [PATCH] [CICD-761] Glob the current dir instead of checking mount paths Simplifies the logic for determining what files were copied to workdir by globbing for certain dirs instead of parsing proc/self/mountinfo. This is also better for other mounted dir structures (such as when a GHA runner mounts the image); we are simply checking what is in the current dir instead of inferring the structure based off mounted source path. --- tests/fixtures/wpengine-common/.gitkeep | 0 tests/test_generate_path_excludes.sh | 124 ++++++------------------ utils/generate_path_excludes.sh | 76 ++++++++------- utils/print_mount_paths.sh | 9 -- 4 files changed, 75 insertions(+), 134 deletions(-) create mode 100644 tests/fixtures/wpengine-common/.gitkeep delete mode 100644 utils/print_mount_paths.sh diff --git a/tests/fixtures/wpengine-common/.gitkeep b/tests/fixtures/wpengine-common/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_generate_path_excludes.sh b/tests/test_generate_path_excludes.sh index 2f00f2a..c15a012 100755 --- a/tests/test_generate_path_excludes.sh +++ b/tests/test_generate_path_excludes.sh @@ -21,91 +21,37 @@ test_determine_source_exclude_paths() { } <<< "$(determine_source_exclude_paths)" if [[ "${base_path}" != "${expected_base_path}" || "${mu_dir_path}" != "${expected_mu_dir_path}" ]]; then - read -r mounted_source_path _ <<< "$(print_mount_paths "tests/fixtures/src")" - echo -e "${RED}Test failed for SRC_PATH='${SRC_PATH}': expected '$expected_base_path, $expected_mu_dir_path', got '$base_path, $mu_dir_path'.${NC}" - echo -e "${BLUE}INFO: mounted_source_path='${mounted_source_path}, pwd=$(pwd)'${NC}" + echo -e "${RED}Test failed for SRC_PATH='${SRC_PATH}', pwd='$(pwd)': expected '$expected_base_path, $expected_mu_dir_path', got '$base_path, $mu_dir_path'.${NC}" exit 1 fi } - mount_from_root() { - echo -e "${BLUE}INFO: mounting from /site...${NC}" - - # Mock the print_mount_paths function - print_mount_paths() { - # shellcheck disable=SC2317 - printf "/path/to/site /site" - } - - # Workdir -> src directory - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content" "/wp-content/" "/wp-content/mu-plugins/" - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/" "/" "/mu-plugins/" - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/mu-plugins" "/" "/mu-plugins/" - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/mu-plugins/" "" "/" - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/plugins" "" "" - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/plugins/" "" "" - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "/wp-content/" "/wp-content/mu-plugins/" - - # Workdir -> wp-content directory - change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "mu-plugins" "/" "/mu-plugins/" - change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "mu-plugins/" "" "/" - change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "plugins" "" "" - change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "plugins/" "" "" - change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "." "/" "/mu-plugins/" - } - - mount_from_wp_content() { - echo -e "${BLUE}INFO: mounting from /site/wp-content...${NC}" - - # Mock the print_mount_paths function - print_mount_paths() { - # shellcheck disable=SC2317 - printf "/path/to/site/wp-content /site" - } - - # Workdir -> src directory - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "/" "/mu-plugins/" - - # Workdir -> wp-content directory - change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "." "/" "/mu-plugins/" - } - - mount_from_mu_plugins() { - echo -e "${BLUE}INFO: mounting from /site/mu-plugins...${NC}" - - # Mock the print_mount_paths function - print_mount_paths() { - # shellcheck disable=SC2317 - printf "/path/to/site/wp-content/mu-plugins /site" - } - - # Workdir -> src directory - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "" "/" - - # Workdir -> mu-plugins directory - change_working_dir "tests/fixtures/src/wp-content/mu-plugins" run_test_determine_source_exclude_paths "." "" "/" - } - - mount_from_plugins() { - echo -e "${BLUE}INFO: mounting from /site/plugins...${NC}" - - # Mock the print_mount_paths function - print_mount_paths() { - # shellcheck disable=SC2317 - printf "/path/to/site/wp-content/plugins /site" - } - - # Workdir -> src directory - change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "" "" - - # Workdir -> plugins directory - change_working_dir "tests/fixtures/src/wp-content/plugins" run_test_determine_source_exclude_paths "." "" "" - } - - mount_from_root - mount_from_wp_content - mount_from_mu_plugins - mount_from_plugins + # Workdir -> src directory + change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content" "/wp-content/" "/wp-content/mu-plugins/" + change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/" "/" "/mu-plugins/" + change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/mu-plugins" "/" "/mu-plugins/" + change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/mu-plugins/" "" "/" + change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/plugins" "" "" + change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/plugins/" "" "" + change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "/wp-content/" "/wp-content/mu-plugins/" + change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "" "/wp-content/" "/wp-content/mu-plugins/" + + # Workdir -> wp-content directory + change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "mu-plugins" "/" "/mu-plugins/" + change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "mu-plugins/" "" "/" + change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "plugins" "" "" + change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "plugins/" "" "" + change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "." "/" "/mu-plugins/" + change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "" "/" "/mu-plugins/" + + # Workdir -> mu-plugins directory + change_working_dir "tests/fixtures/src/wp-content/mu-plugins" run_test_determine_source_exclude_paths "." "" "/" + + # Workdir -> plugins directory + change_working_dir "tests/fixtures/src/wp-content/plugins" run_test_determine_source_exclude_paths "." "" "" + + # Workdir -> fixtures directory (checks that glob_current_dir is working (wpengine-common should be detected)) + change_working_dir "tests/fixtures" run_test_determine_source_exclude_paths "." "" "/" echo -e "${GREEN}All tests passed for determining the source excludes path.${NC}" } @@ -122,7 +68,7 @@ test_determine_remote_exclude_paths() { } <<< "$(determine_remote_exclude_paths)" if [[ "${base_path}" != "${expected_base_path}" || "${mu_dir_path}" != "${expected_mu_dir_path}" ]]; then - echo -e "${RED}Test failed for REMOTE_PATH='${SRC_PATH}': expected '$expected_base_path, $expected_mu_dir_path', got '$base_path, $mu_dir_path'.${NC}" + echo -e "${RED}Test failed for REMOTE_PATH='${SRC_PATH}', pwd='$(pwd)': expected '$expected_base_path, $expected_mu_dir_path', got '$base_path, $mu_dir_path'.${NC}" exit 1 fi } @@ -142,17 +88,11 @@ test_generate_source_exclude_from() { local output local expected_output - # Mock the print_mount_paths function - print_mount_paths() { - # shellcheck disable=SC2317 - printf "/path/to/site /site" - } - - output=$(generate_source_exclude_from) + output=$(change_working_dir "tests/fixtures/src" generate_source_exclude_from) expected_output=$(cat "tests/fixtures/excludes/source_exclude_from.txt") if [[ "$output" != "$expected_output" ]]; then - echo -e "${RED}Test failed: generated output does not match expected output.${NC}" + echo -e "${RED}Test failed for pwd='$(pwd)': generated output does not match expected output.${NC}" echo -e "${BLUE}Generated output:${NC}" echo "$output" echo -e "${BLUE}Expected output:${NC}" @@ -168,11 +108,11 @@ test_generate_remote_excludes() { local output local expected_output - output=$(generate_remote_excludes) + output=$(change_working_dir "tests/fixtures/src" generate_remote_excludes) expected_output=$(cat "tests/fixtures/excludes/remote_excludes.txt") if [[ "$output" != "$expected_output" ]]; then - echo -e "${RED}Test failed: generated output does not match expected output.${NC}" + echo -e "${RED}Test failed for pwd='$(pwd)': generated output does not match expected output.${NC}" echo -e "${BLUE}Generated output:${NC}" echo "$output" echo -e "${BLUE}Expected output:${NC}" diff --git a/utils/generate_path_excludes.sh b/utils/generate_path_excludes.sh index e219d4c..f937b99 100644 --- a/utils/generate_path_excludes.sh +++ b/utils/generate_path_excludes.sh @@ -31,20 +31,11 @@ shopt -s extglob # (such as some of the WP Engine managed plugins) might be useful in rare # circumstances to have as a reference for debugging purposes. -# Get the directory of the current script -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - -# Source the print_mount_paths.sh file relative to the current script's location -source "${SCRIPT_DIR}/print_mount_paths.sh" - -# Determine the source paths to exclude from the deployment determine_source_exclude_paths() { local base_path local mu_dir_path - read -r mounted_source_path _ <<< "$(print_mount_paths "wp-content")" - - if [[ -n "${SRC_PATH}" && "${SRC_PATH}" != '.' ]]; then + parse_src_path() { case "${SRC_PATH}" in wp-content ) base_path="/wp-content/" @@ -58,25 +49,40 @@ determine_source_exclude_paths() { mu_dir_path="/" ;; esac - elif [[ "$(pwd)" != *wp-content* && "${mounted_source_path}" != *wp-content* ]]; then - base_path="/wp-content/" - mu_dir_path="/wp-content/mu-plugins/" + } + + parse_current_path() { + case "$(pwd)" in + *wp-content ) + base_path="/" + mu_dir_path="/mu-plugins/" + ;; + *mu-plugins ) + mu_dir_path="/" + ;; + esac + } + + glob_current_dir() { + if [[ -d wp-content ]]; then + base_path="/wp-content/" + mu_dir_path="/wp-content/mu-plugins/" + elif [[ -d mu-plugins ]]; then + base_path="/" + mu_dir_path="/mu-plugins/" + elif [[ -d wpengine-common ]]; then + mu_dir_path="/" + fi + } + + # If SRC_PATH is set, use it to determine base and mu-plugins paths. + # Otherwise, use the current directory or check its contents to set the paths. + if [[ -n "${SRC_PATH}" && "${SRC_PATH}" != '.' ]]; then + parse_src_path + elif [[ "$(pwd)" == *wp-content* ]]; then + parse_current_path else - # Iterate over the possible paths and break when a match is found - # !!! Ordering is important for the switch cases !!! - for value in "$(pwd)" "$mounted_source_path"; do - case "$value" in - *wp-content ) - base_path="/" - mu_dir_path="/mu-plugins/" - break - ;; - *mu-plugins ) - mu_dir_path="/" - break - ;; - esac - done + glob_current_dir fi printf "%s\n%s\n" "$base_path" "$mu_dir_path" @@ -86,10 +92,7 @@ determine_remote_exclude_paths() { local base_path local mu_dir_path - if [[ -z "${REMOTE_PATH}" ]]; then - base_path="/wp-content/" - mu_dir_path="/wp-content/mu-plugins/" - else + parse_remote_path() { case "$REMOTE_PATH" in wp-content?(/) ) base_path="/" @@ -99,12 +102,19 @@ determine_remote_exclude_paths() { mu_dir_path="/" ;; esac + } + + if [[ -z "${REMOTE_PATH}" ]]; then + base_path="/wp-content/" + mu_dir_path="/wp-content/mu-plugins/" + else + parse_remote_path fi printf "%s\n%s\n" "$base_path" "$mu_dir_path" } -# Generate the dynamic list of files to exclude from the deployment +# Generate the dynamic list of paths to exclude from the deployment print_dynamic_excludes() { local func=$1 local delimiter=$2 diff --git a/utils/print_mount_paths.sh b/utils/print_mount_paths.sh deleted file mode 100644 index 426616c..0000000 --- a/utils/print_mount_paths.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -set -e - -print_mount_paths() { - local mount_path=$1 - # Print the mounted source path and base mount path, listed in positions 4 and 5 of the resulting string - grep "$mount_path" /proc/self/mountinfo | awk '{print $4, $5}' -}