diff --git a/.circleci/config.yml b/.circleci/config.yml index c25979d45ac..7eb6c82b489 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -200,6 +200,10 @@ jobs: - run: name: Test Go module cache setup command: python3 .circleci/scripts/test-go-mod-cache.py + # Change detection must fail safe: a failed base diff runs the full CI. + - run: + name: Test change detection + command: .circleci/scripts/test-collect-params.sh # Forward string pipeline parameters to /tmp/pipeline-parameters.json. # collect-params.sh picks up all env vars starting with "c-" and writes them as JSON strings. # Add a line here when a new string value needs to reach continuation config jobs diff --git a/.circleci/scripts/collect-params.sh b/.circleci/scripts/collect-params.sh index a8500d94f91..cb6b76dfa4d 100755 --- a/.circleci/scripts/collect-params.sh +++ b/.circleci/scripts/collect-params.sh @@ -9,11 +9,12 @@ # # str/bool read c-* env vars (CircleCI pipeline params). detect/detect_all read # their ERE patterns from routing.yml so the patterns are declarative data. -# Each invocation appends to /tmp/pipeline-parameters.json. +# Each invocation appends to ${OUTPUT} (default /tmp/pipeline-parameters.json; +# overridable so tests can use an isolated file). set -euo pipefail MODE="${1:?Usage: collect-params.sh }" -OUTPUT="/tmp/pipeline-parameters.json" +OUTPUT="${OUTPUT:-/tmp/pipeline-parameters.json}" ROUTING="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/routing.yml" [ -f "${OUTPUT}" ] || echo '{}' > "${OUTPUT}" @@ -44,10 +45,17 @@ case "${MODE}" in detect | detect_all) [[ "${MODE}" == "detect_all" ]] && section=".change_patterns.all" || section=".change_patterns.any" - CHANGED=$(git diff --name-only --no-renames "origin/${BASE_REVISION}...HEAD" 2> /dev/null \ - || git diff --name-only --no-renames HEAD~1 HEAD || true) - echo "=== Changed files ===" - echo "${CHANGED:-}" + # A failed base diff must not shrink the change set, or path gating would skip jobs (and + # pass their required gates) for changes it never saw. Treat every path as changed instead: + # every "any" pattern matches and no "all" fast path applies, so the full CI runs. + echo "=== Changed files (origin/${BASE_REVISION}...HEAD) ===" + if CHANGED=$(git diff --name-only --no-renames "origin/${BASE_REVISION}...HEAD"); then + every_path_changed=false + echo "${CHANGED:-}" + else + every_path_changed=true + echo "WARNING: diff against origin/${BASE_REVISION} failed; treating every path as changed" >&2 + fi echo "=====================" while IFS= read -r name; do @@ -55,7 +63,9 @@ case "${MODE}" in pattern=$(yq -r "${section}.\"${name}\"" "${ROUTING}") # Here-strings, not pipes: `grep -q` exits on the first hit, and under pipefail the # writer's SIGPIPE would turn a large file list into a false result. - if [ -z "${CHANGED}" ]; then + if ${every_path_changed}; then + [[ "${MODE}" == "detect" ]] && result=true || result=false + elif [ -z "${CHANGED}" ]; then result=false elif [[ "${MODE}" == "detect_all" ]]; then # True iff every changed file matches the pattern (i.e., no file fails to match). diff --git a/.circleci/scripts/test-collect-params.sh b/.circleci/scripts/test-collect-params.sh new file mode 100755 index 00000000000..e26418744fc --- /dev/null +++ b/.circleci/scripts/test-collect-params.sh @@ -0,0 +1,124 @@ +#!/usr/bin/env bash +# Tests change detection in collect-params.sh (detect and detect_all modes). +# Builds a throwaway git repo, points origin/ at its first commit, runs +# the real script against the real routing.yml, and asserts the c-* flags. +# +# Usage: +# bash .circleci/scripts/test-collect-params.sh +# +# Requires: git, jq, yq (same version used in CI) +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +COLLECT_SCRIPT="${SCRIPT_DIR}/collect-params.sh" + +WORK_DIR="$(mktemp -d)" +REPO_DIR="${WORK_DIR}/repo" +OUTPUT="${WORK_DIR}/params.json" +export OUTPUT +trap 'rm -rf "${WORK_DIR}"' EXIT + +git_in_repo() { + git -C "${REPO_DIR}" -c user.name=test -c user.email=test@example.com -c commit.gpgsign=false "$@" +} + +commit_file() { # + mkdir -p "$(dirname "${REPO_DIR}/${1}")" + echo "${RANDOM}" > "${REPO_DIR}/${1}" + git_in_repo add "${1}" + git_in_repo commit --quiet --message "change ${1}" +} + +git init --quiet "${REPO_DIR}" +commit_file README.md +BASE_COMMIT=$(git_in_repo rev-parse HEAD) +git_in_repo update-ref refs/remotes/origin/base "${BASE_COMMIT}" + +# --- Test harness --- +PASS=0 +FAIL=0 + +# Runs detect and detect_all for HEAD = base + (space-separated, +# none if empty), then asserts each expected flag given as name=value. +run_scenario() { # ... + local name="${1}" base_revision="${2}" changed_paths="${3}" + shift 3 + + git_in_repo checkout --quiet --detach "${BASE_COMMIT}" + local changed_path + for changed_path in ${changed_paths}; do + commit_file "${changed_path}" + done + + echo '{}' > "${OUTPUT}" + local log="${WORK_DIR}/scenario.log" mode + : > "${log}" + for mode in detect detect_all; do + if ! (cd "${REPO_DIR}" && BASE_REVISION="${base_revision}" bash "${COLLECT_SCRIPT}" "${mode}" >> "${log}" 2>&1); then + echo "FAIL: ${name} (collect-params.sh ${mode} exited non-zero)" + cat "${log}" + FAIL=$((FAIL + 1)) + return + fi + done + + local all_pass=true expectation flag expected actual + for expectation in "$@"; do + flag="${expectation%%=*}" + expected="${expectation#*=}" + actual=$(jq -r ".\"c-${flag}\"" "${OUTPUT}") + if [[ "${actual}" != "${expected}" ]]; then + echo " FAIL: expected c-${flag}=${expected}, got ${actual}" + all_pass=false + fi + done + + if ${all_pass}; then + echo "PASS: ${name}" + PASS=$((PASS + 1)) + else + echo "FAIL: ${name}" + cat "${log}" + FAIL=$((FAIL + 1)) + fi +} + +# --- Scenarios --- + +run_scenario \ + "Docs-only change takes the docs fast path" \ + base docs/public-docs/page.md \ + only_docs_changes=true docs_changes_detected=true rust_changes_detected=false contracts_changed=false + +run_scenario \ + "Rust change is detected" \ + base rust/kona/lib.rs \ + only_docs_changes=false rust_changes_detected=true contracts_changed=false + +run_scenario \ + "Code that no pattern matches skips the docs fast path" \ + base op-node/main.go \ + only_docs_changes=false docs_changes_detected=false rust_changes_detected=false contracts_changed=false + +run_scenario \ + "Docs plus code skips the docs fast path" \ + base "docs/public-docs/page.md rust/kona/lib.rs" \ + only_docs_changes=false docs_changes_detected=true rust_changes_detected=true + +run_scenario \ + "No change detects nothing" \ + base "" \ + only_docs_changes=false rust_changes_detected=false contracts_changed=false + +run_scenario \ + "Missing base ref treats every path as changed" \ + missing-branch docs/public-docs/page.md \ + only_docs_changes=false docs_changes_detected=true rust_changes_detected=true contracts_changed=true circleci_changed=true + +# --- Summary --- +echo "" +echo "=== Results: ${PASS} passed, ${FAIL} failed ===" + +if [[ ${FAIL} -gt 0 ]]; then + exit 1 +fi