Skip to content

Commit 685e349

Browse files
committed
chore: improve tests common
Signed-off-by: Josef Andersson <josef.andersson@digg.se>
1 parent 8f95bb1 commit 685e349

File tree

5 files changed

+108
-4
lines changed

5 files changed

+108
-4
lines changed

linters/shell.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ main() {
5050

5151
if [[ -n "$bats_files" ]]; then
5252
print_info "Checking bats test files..."
53-
if ! echo "$bats_files" | xargs -r shellcheck --shell=bats --severity=info --exclude=SC1091,SC2034,SC2155,SC2164; then
53+
if ! echo "$bats_files" | xargs -r shellcheck --shell=bats --severity=info --exclude=SC1090,SC1091,SC2016,SC2030,SC2031,SC2034,SC2123,SC2155,SC2164,SC2218; then
5454
failed=1
5555
fi
5656
fi

tests/linters-commits.bats

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ load "${BATS_TEST_DIRNAME}/libs/bats-support/load.bash"
1010
load "${BATS_TEST_DIRNAME}/libs/bats-assert/load.bash"
1111
load "${BATS_TEST_DIRNAME}/libs/bats-file/load.bash"
1212
load "${BATS_TEST_DIRNAME}/libs/bats-mock/stub.bash"
13+
load "${BATS_TEST_DIRNAME}/test_helper.bash"
1314

1415
setup() {
1516
TEST_DIR="$(temp_make)"
@@ -21,7 +22,7 @@ setup() {
2122

2223
teardown() {
2324
unstub conform 2>/dev/null || true
24-
temp_del "$TEST_DIR"
25+
safe_temp_del "$TEST_DIR"
2526
}
2627

2728
@test "commits.sh skips on default branch" {

tests/linters-secrets.bats

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ load "${BATS_TEST_DIRNAME}/libs/bats-support/load.bash"
1010
load "${BATS_TEST_DIRNAME}/libs/bats-assert/load.bash"
1111
load "${BATS_TEST_DIRNAME}/libs/bats-file/load.bash"
1212
load "${BATS_TEST_DIRNAME}/libs/bats-mock/stub.bash"
13+
load "${BATS_TEST_DIRNAME}/test_helper.bash"
1314

1415
setup() {
1516
TEST_DIR="$(temp_make)"
@@ -21,7 +22,7 @@ setup() {
2122

2223
teardown() {
2324
unstub gitleaks 2>/dev/null || true
24-
temp_del "$TEST_DIR"
25+
safe_temp_del "$TEST_DIR"
2526
}
2627

2728
@test "secrets.sh runs gitleaks" {

tests/test_helper.bash

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

tests/verify.bats

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bats_require_minimum_version 1.13.0
99
load "${BATS_TEST_DIRNAME}/libs/bats-support/load.bash"
1010
load "${BATS_TEST_DIRNAME}/libs/bats-assert/load.bash"
1111
load "${BATS_TEST_DIRNAME}/libs/bats-file/load.bash"
12+
load "${BATS_TEST_DIRNAME}/test_helper.bash"
1213

1314
setup() {
1415
TEST_DIR="$(temp_make)"
@@ -20,7 +21,7 @@ setup() {
2021
}
2122

2223
teardown() {
23-
temp_del "$TEST_DIR"
24+
safe_temp_del "$TEST_DIR"
2425
}
2526

2627
@test "verify.sh runs base linters" {

0 commit comments

Comments
 (0)