diff --git a/scripts/install/pre-commit.sh b/scripts/install/pre-commit.sh index 18e2fd582f..4b5bc8cf86 100755 --- a/scripts/install/pre-commit.sh +++ b/scripts/install/pre-commit.sh @@ -5,6 +5,8 @@ set -euo pipefail if [[ -n "${VENV:-}" && -x "${VENV}/bin/python" && "${PYTHON:-}" != "${VENV}/bin/python" ]]; then unset INFINITO_ENV_LOADED unset INFINITO_ENV_PYTHON_LOADED + unset PYTHON + unset PIP # shellcheck source=scripts/meta/env/all.sh source "${ENV_SH:-scripts/meta/env/all.sh}" fi diff --git a/scripts/install/python.sh b/scripts/install/python.sh index 703219b37c..8ced327ee2 100755 --- a/scripts/install/python.sh +++ b/scripts/install/python.sh @@ -5,6 +5,8 @@ set -euo pipefail if [[ -n "${VENV:-}" && -x "${VENV}/bin/python" && "${PYTHON:-}" != "${VENV}/bin/python" ]]; then unset INFINITO_ENV_LOADED unset INFINITO_ENV_PYTHON_LOADED + unset PYTHON + unset PIP # shellcheck source=scripts/meta/env/all.sh source "${ENV_SH:-scripts/meta/env/all.sh}" fi diff --git a/scripts/tests/environment/00_orchestrator.sh b/scripts/tests/environment/00_orchestrator.sh index 41b90fe16e..508cf5b22f 100755 --- a/scripts/tests/environment/00_orchestrator.sh +++ b/scripts/tests/environment/00_orchestrator.sh @@ -16,6 +16,7 @@ cd "${REPO_ROOT}" for step in "${SCRIPT_DIR}"/[0-9][0-9]_*.sh; do [[ "${step}" == "${BASH_SOURCE[0]}" ]] && continue + load_repo_env ensure_git_safe_directory echo "============================================================" echo ">>> $(basename "${step}")" diff --git a/scripts/tests/environment/05_commit.sh b/scripts/tests/environment/05_commit.sh index c20d6ce9f4..4dee037022 100755 --- a/scripts/tests/environment/05_commit.sh +++ b/scripts/tests/environment/05_commit.sh @@ -8,6 +8,17 @@ source "${SCRIPT_DIR}/lib.sh" cd "${REPO_ROOT}" +ORIGINAL_GIT_USER_NAME="$(git config --local --get user.name || true)" +ORIGINAL_GIT_USER_EMAIL="$(git config --local --get user.email || true)" + +if [[ -z "${ORIGINAL_GIT_USER_NAME}" ]]; then + git config --local user.name "Environment Test" +fi + +if [[ -z "${ORIGINAL_GIT_USER_EMAIL}" ]]; then + git config --local user.email "environment-test@example.invalid" +fi + if ORIGINAL_REF="$(git symbolic-ref --quiet --short HEAD)"; then : else @@ -15,6 +26,14 @@ else fi TEST_BRANCH="test/pre-commit-hook-$(date +%s)" +WORKTREE_STASH="" + +if [[ -n "$(git status --short)" ]]; then + WORKTREE_STASH="environment-pre-commit-hook-$(date +%s)" + echo "Stashing existing repository changes before commit hook validation." + git stash push --include-untracked --message "${WORKTREE_STASH}" >/dev/null +fi + cleanup() { local exit_code="$1" set +e @@ -30,6 +49,25 @@ cleanup() { git branch -D "${TEST_BRANCH}" >/dev/null 2>&1 || true fi + if [[ -n "${ORIGINAL_GIT_USER_NAME}" ]]; then + git config --local user.name "${ORIGINAL_GIT_USER_NAME}" >/dev/null 2>&1 || true + else + git config --local --unset user.name >/dev/null 2>&1 || true + fi + + if [[ -n "${ORIGINAL_GIT_USER_EMAIL}" ]]; then + git config --local user.email "${ORIGINAL_GIT_USER_EMAIL}" >/dev/null 2>&1 || true + else + git config --local --unset user.email >/dev/null 2>&1 || true + fi + + if [[ -n "${WORKTREE_STASH}" ]] && git stash list | grep -F "${WORKTREE_STASH}" >/dev/null 2>&1; then + if ! git stash pop --index >/dev/null 2>&1; then + echo "[FAIL] Failed to restore the stashed repository changes after commit validation." >&2 + exit_code=1 + fi + fi + exit "${exit_code}" } trap 'cleanup "$?"' EXIT @@ -77,3 +115,4 @@ echo "Returning to the original ref and deleting the temporary test branch." git checkout "${ORIGINAL_REF}" git branch -D "${TEST_BRANCH}" trap - EXIT +cleanup 0 diff --git a/scripts/tests/environment/lib.sh b/scripts/tests/environment/lib.sh index 656b2964e0..2ef69394e7 100755 --- a/scripts/tests/environment/lib.sh +++ b/scripts/tests/environment/lib.sh @@ -39,6 +39,14 @@ load_repo_env() { local previous_pwd previous_pwd="$(pwd)" cd "${REPO_ROOT}" + unset INFINITO_ENV_LOADED + unset INFINITO_ENV_PYTHON_LOADED + unset INFINITO_ENV_RUNTIME_LOADED + unset INFINITO_ENV_DEFAULTS_LOADED + unset INFINITO_ENV_INVENTORY_LOADED + unset INFINITO_ENV_GITHUB_LOADED + unset PYTHON + unset PIP # shellcheck source=scripts/meta/env/all.sh source scripts/meta/env/all.sh cd "${previous_pwd}"