|
| 1 | +#!/usr/bin/env bash |
| 2 | +# shellcheck disable=SC2016,SC2154,SC2164,SC2268 |
| 3 | +# SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | +# |
| 6 | +# Shared test helper functions for BATS tests |
| 7 | +# |
| 8 | +# Shellcheck disabled: |
| 9 | +# SC2016 - Expressions don't expand in single quotes (intentional in mock scripts) |
| 10 | +# SC2154 - Variables like $output/$stderr are set by bats, not this script |
| 11 | +# SC2164 - cd without || exit is fine in test helpers (bats handles failures) |
| 12 | +# SC2268 - x-prefix in comparisons is a common bats pattern for empty checks |
| 13 | + |
| 14 | +# ============================================================================= |
| 15 | +# Safe Temp Directory Cleanup |
| 16 | +# ============================================================================= |
| 17 | + |
| 18 | +# Safely delete a temp directory, handling git's write-protected objects |
| 19 | +# This wraps temp_del but makes files writable first to avoid interactive prompts |
| 20 | +# SAFETY: Only deletes directories under /tmp or $BATS_TMPDIR |
| 21 | +# Usage: safe_temp_del <path> |
| 22 | +safe_temp_del() { |
| 23 | + local path="$1" |
| 24 | + [[ -z "$path" ]] && return 0 |
| 25 | + [[ ! -d "$path" ]] && return 0 |
| 26 | + |
| 27 | + # Resolve to absolute path |
| 28 | + local abs_path |
| 29 | + abs_path="$(cd "$path" 2>/dev/null && pwd)" || return 0 |
| 30 | + |
| 31 | + # SAFETY: Only allow deletion in /tmp or BATS_TMPDIR |
| 32 | + local allowed_base="${BATS_TMPDIR:-/tmp}" |
| 33 | + if [[ "$abs_path" != /tmp/* && "$abs_path" != "$allowed_base"/* ]]; then |
| 34 | + echo "ERROR: safe_temp_del refuses to delete '$abs_path' - not in /tmp or BATS_TMPDIR" >&2 |
| 35 | + return 1 |
| 36 | + fi |
| 37 | + |
| 38 | + # Extra safety: refuse to delete if path is too short (e.g., /tmp itself) |
| 39 | + if [[ "${#abs_path}" -lt 10 ]]; then |
| 40 | + echo "ERROR: safe_temp_del refuses to delete '$abs_path' - path too short" >&2 |
| 41 | + return 1 |
| 42 | + fi |
| 43 | + |
| 44 | + # Make all files writable to avoid rm prompting on git objects |
| 45 | + chmod -R u+w "$abs_path" 2>/dev/null || true |
| 46 | + temp_del "$abs_path" |
| 47 | +} |
| 48 | + |
| 49 | +# ============================================================================= |
| 50 | +# Git Repository Setup Helpers |
| 51 | +# ============================================================================= |
| 52 | + |
| 53 | +# Initialize a minimal git repository for testing |
| 54 | +# Usage: init_git_repo |
| 55 | +init_git_repo() { |
| 56 | + git init -q |
| 57 | + git config user.email "test@example.com" |
| 58 | + git config user.name "Test User" |
| 59 | + echo "initial" >file.txt |
| 60 | + git add file.txt |
| 61 | + git commit -q -m "Initial commit" |
| 62 | +} |
| 63 | + |
| 64 | +# Initialize git repo with isolated HOME and config |
| 65 | +# Usage: init_isolated_git_repo |
| 66 | +init_isolated_git_repo() { |
| 67 | + export HOME="$TEST_DIR/home" |
| 68 | + export GIT_CONFIG_NOSYSTEM=1 |
| 69 | + export GIT_CONFIG_GLOBAL="$TEST_DIR/home/.gitconfig" |
| 70 | + mkdir -p "$HOME" |
| 71 | + init_git_repo |
| 72 | +} |
| 73 | + |
| 74 | +# ============================================================================= |
| 75 | +# Debug Helpers |
| 76 | +# ============================================================================= |
| 77 | + |
| 78 | +# Standard debug output for failed tests |
| 79 | +# Usage: debug_output (call after 'run' command) |
| 80 | +debug_output() { |
| 81 | + [ "x$BATS_TEST_COMPLETED" = "x" ] && echo "o:'${output}' e:'${stderr}'" |
| 82 | +} |
| 83 | + |
| 84 | +# ============================================================================= |
| 85 | +# Mock Helpers |
| 86 | +# ============================================================================= |
| 87 | + |
| 88 | +# Create repeated stub that always returns the same result |
| 89 | +# Usage: stub_repeated <command> <behavior> |
| 90 | +stub_repeated() { |
| 91 | + local cmd="$1" |
| 92 | + local behavior="$2" |
| 93 | + |
| 94 | + mkdir -p "${TEST_DIR}/bin" |
| 95 | + cat >"${TEST_DIR}/bin/${cmd}" <<SCRIPT |
| 96 | +#!/usr/bin/env bash |
| 97 | +${behavior} |
| 98 | +SCRIPT |
| 99 | + chmod +x "${TEST_DIR}/bin/${cmd}" |
| 100 | + export PATH="${TEST_DIR}/bin:${PATH}" |
| 101 | +} |
0 commit comments