|
| 1 | +#!/usr/bin/env bash |
| 2 | +# shellcheck shell=bash |
| 3 | +# Smoke test runner for chezmoi script templates. |
| 4 | +# |
| 5 | +# Layout convention: |
| 6 | +# tests/smoke/<script-name>/<scenario>/setup.sh |
| 7 | +# |
| 8 | +# <script-name> matches the basename of a template under |
| 9 | +# home/.chezmoiscripts/<script-name>.sh.tmpl. For each scenario: |
| 10 | +# 1. Allocate an isolated HOME directory (mktemp -d). |
| 11 | +# 2. Run setup.sh with HOME pointed at the tempdir to construct the |
| 12 | +# scenario state (e.g. write a dummy ~/.apm/apm.lock.yaml). |
| 13 | +# 3. chezmoi execute-template the target script. |
| 14 | +# 4. Execute the rendered script under the same isolated HOME. |
| 15 | +# 5. Pass if exit code is 0; fail with a stderr dump of the rendered |
| 16 | +# script otherwise. |
| 17 | + |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +RED='\033[0;31m' |
| 21 | +GREEN='\033[0;32m' |
| 22 | +YELLOW='\033[1;33m' |
| 23 | +NC='\033[0m' |
| 24 | + |
| 25 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 26 | +CHEZMOI_SOURCE_PATH="${REPO_ROOT}/home" |
| 27 | +SMOKE_ROOT="${REPO_ROOT}/tests/smoke" |
| 28 | + |
| 29 | +if [[ ! -d "$SMOKE_ROOT" ]]; then |
| 30 | + echo -e "${YELLOW}No smoke fixtures found at ${SMOKE_ROOT}${NC}" |
| 31 | + exit 0 |
| 32 | +fi |
| 33 | + |
| 34 | +if ! command -v chezmoi >/dev/null 2>&1; then |
| 35 | + echo -e "${RED}chezmoi is required but not found${NC}" |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +# Single tmp parent dir cleaned up on exit, regardless of which iteration |
| 40 | +# the script dies in. |
| 41 | +SMOKE_TMP_ROOT=$(mktemp -d -t chezmoi-smoke-XXXXXX) |
| 42 | +trap 'rm -rf "${SMOKE_TMP_ROOT}"' EXIT |
| 43 | + |
| 44 | +PASS_COUNT=0 |
| 45 | +FAIL_COUNT=0 |
| 46 | + |
| 47 | +shopt -s nullglob |
| 48 | + |
| 49 | +for script_dir in "${SMOKE_ROOT}"/*/; do |
| 50 | + script_name="$(basename "$script_dir")" |
| 51 | + template_path="${CHEZMOI_SOURCE_PATH}/.chezmoiscripts/${script_name}.sh.tmpl" |
| 52 | + |
| 53 | + if [[ ! -f "$template_path" ]]; then |
| 54 | + echo -e "${RED}Target template not found for fixture group '${script_name}': ${template_path}${NC}" |
| 55 | + FAIL_COUNT=$((FAIL_COUNT + 1)) |
| 56 | + continue |
| 57 | + fi |
| 58 | + |
| 59 | + for scenario_dir in "${script_dir}"*/; do |
| 60 | + scenario_name="$(basename "$scenario_dir")" |
| 61 | + setup_script="${scenario_dir}setup.sh" |
| 62 | + |
| 63 | + if [[ ! -f "$setup_script" ]]; then |
| 64 | + echo -e "${YELLOW}No setup.sh in ${scenario_dir}, skipping${NC}" |
| 65 | + continue |
| 66 | + fi |
| 67 | + |
| 68 | + echo "═══════════════════════════════════════════════" |
| 69 | + echo "Smoke: ${script_name} / ${scenario_name}" |
| 70 | + echo "═══════════════════════════════════════════════" |
| 71 | + |
| 72 | + smoke_home="${SMOKE_TMP_ROOT}/${script_name}__${scenario_name}" |
| 73 | + mkdir -p "$smoke_home" |
| 74 | + rendered="${smoke_home}/__rendered.sh" |
| 75 | + |
| 76 | + if ! HOME="$smoke_home" bash "$setup_script"; then |
| 77 | + echo -e "${RED}✗ setup.sh failed${NC}" |
| 78 | + FAIL_COUNT=$((FAIL_COUNT + 1)) |
| 79 | + continue |
| 80 | + fi |
| 81 | + |
| 82 | + if ! chezmoi execute-template --source "$CHEZMOI_SOURCE_PATH" < "$template_path" > "$rendered"; then |
| 83 | + echo -e "${RED}✗ template render failed${NC}" |
| 84 | + FAIL_COUNT=$((FAIL_COUNT + 1)) |
| 85 | + continue |
| 86 | + fi |
| 87 | + |
| 88 | + if HOME="$smoke_home" bash "$rendered"; then |
| 89 | + echo -e "${GREEN}✓ PASS${NC}" |
| 90 | + PASS_COUNT=$((PASS_COUNT + 1)) |
| 91 | + else |
| 92 | + exit_code=$? |
| 93 | + echo -e "${RED}✗ FAIL (rendered script exited with ${exit_code})${NC}" |
| 94 | + { |
| 95 | + echo "--- rendered script (${template_path}) ---" |
| 96 | + cat "$rendered" |
| 97 | + echo "--- end rendered ---" |
| 98 | + } >&2 |
| 99 | + FAIL_COUNT=$((FAIL_COUNT + 1)) |
| 100 | + fi |
| 101 | + done |
| 102 | +done |
| 103 | + |
| 104 | +echo "" |
| 105 | +echo "Smoke summary: ${PASS_COUNT} passed, ${FAIL_COUNT} failed" |
| 106 | + |
| 107 | +if [[ $FAIL_COUNT -gt 0 ]]; then |
| 108 | + exit 1 |
| 109 | +fi |
0 commit comments