From 3aec637e0d67089c6f844b8215dbe47e747448c4 Mon Sep 17 00:00:00 2001 From: Jean-Paul van Ravensberg <14926452+DevSecNinja@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:32:53 +0100 Subject: [PATCH] style: brace variable references for shellcheck SC2250 Adds braces to the 66 variable references that SC2250 flags, so lint passes under the central .shellcheckrc. Generated with `shellcheck -f diff -i SC2250` (0.11.0); every changed line differs only by the added braces. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .devcontainer/post-create.sh | 24 ++++++------ ansible/scripts/ansible-pull.sh | 36 +++++++++--------- tests/bash/run-tests.sh | 66 ++++++++++++++++----------------- 3 files changed, 63 insertions(+), 63 deletions(-) diff --git a/.devcontainer/post-create.sh b/.devcontainer/post-create.sh index 8c92ba8..f81e4eb 100644 --- a/.devcontainer/post-create.sh +++ b/.devcontainer/post-create.sh @@ -5,35 +5,35 @@ check_docker_sock() { local sock="/var/run/docker.sock" # Exists? - if [[ ! -S "$sock" ]]; then - echo "❌ Docker socket not found at $sock" + if [[ ! -S "${sock}" ]]; then + echo "❌ Docker socket not found at ${sock}" return 1 fi # Read actual values local owner group perms - owner=$(stat -c '%U' "$sock") - group=$(stat -c '%G' "$sock") - perms=$(stat -c '%a' "$sock") + owner=$(stat -c '%U' "${sock}") + group=$(stat -c '%G' "${sock}") + perms=$(stat -c '%a' "${sock}") local ok=true - if [[ "$owner" != "root" ]]; then - echo "❌ Owner is '$owner' (expected: root)" + if [[ "${owner}" != "root" ]]; then + echo "❌ Owner is '${owner}' (expected: root)" ok=false fi - if [[ "$group" != "docker" ]]; then - echo "❌ Group is '$group' (expected: docker)" + if [[ "${group}" != "docker" ]]; then + echo "❌ Group is '${group}' (expected: docker)" ok=false fi - if [[ "$perms" != "660" ]]; then - echo "❌ Permissions are '$perms' (expected: 660)" + if [[ "${perms}" != "660" ]]; then + echo "❌ Permissions are '${perms}' (expected: 660)" ok=false fi - if [[ "$ok" == true ]]; then + if [[ "${ok}" == true ]]; then echo "✅ Docker socket permissions are correct" return 0 else diff --git a/ansible/scripts/ansible-pull.sh b/ansible/scripts/ansible-pull.sh index 3712180..5f4dea9 100755 --- a/ansible/scripts/ansible-pull.sh +++ b/ansible/scripts/ansible-pull.sh @@ -13,7 +13,7 @@ TARGET_HOST="${ANSIBLE_PULL_TARGET:-$(hostname | tr '[:upper:]' '[:lower:]')}" APT_LOCK_TIMEOUT="${ANSIBLE_PULL_APT_LOCK_TIMEOUT:-600}" # Applied to every apt-get invocation so apt waits for the lock instead of # failing outright when another package manager is running. -APT_LOCK_OPTS=(-o "DPkg::Lock::Timeout=$APT_LOCK_TIMEOUT") +APT_LOCK_OPTS=(-o "DPkg::Lock::Timeout=${APT_LOCK_TIMEOUT}") # Function to log messages (output goes to journalctl) log() { @@ -40,13 +40,13 @@ wait_for_apt_locks() { log "Waiting up to ${APT_LOCK_TIMEOUT}s for apt/dpkg locks to be released..." output=$(apt-get "${APT_LOCK_OPTS[@]}" check 2>&1) || rc=$? - if [ "$rc" -eq 0 ]; then + if [ "${rc}" -eq 0 ]; then log "apt/dpkg locks are available" - elif grep -qiE 'could not get lock|unable to acquire|frontend lock|temporarily unavailable' <<<"$output"; then + elif grep -qiE 'could not get lock|unable to acquire|frontend lock|temporarily unavailable' <<<"${output}"; then log "WARNING: apt/dpkg still locked after ${APT_LOCK_TIMEOUT}s, continuing anyway" else - log "WARNING: 'apt-get check' failed for a non-lock reason (rc=$rc), continuing anyway:" - log "$output" + log "WARNING: 'apt-get check' failed for a non-lock reason (rc=${rc}), continuing anyway:" + log "${output}" fi } @@ -56,7 +56,7 @@ log "Starting ansible-pull run" if ! command -v ansible-pull &>/dev/null; then log "Ansible not found, installing..." # Check if running as root - if [ "$EUID" -ne 0 ]; then + if [ "${EUID}" -ne 0 ]; then echo "Error: Script must be run as root or with sudo" >&2 exit 1 fi @@ -67,21 +67,21 @@ fi # Clone or update the repository first so requirements.yml is available # before running ansible-pull (which would fail on missing roles otherwise) -if [ -d "$WORKDIR/.git" ]; then +if [ -d "${WORKDIR}/.git" ]; then log "Updating existing repository..." - git -C "$WORKDIR" fetch origin - git -C "$WORKDIR" reset --hard origin/main + git -C "${WORKDIR}" fetch origin + git -C "${WORKDIR}" reset --hard origin/main else log "Cloning repository..." - git clone --branch main "$REPO_URL" "$WORKDIR" + git clone --branch main "${REPO_URL}" "${WORKDIR}" fi # Install all required collections and roles from requirements.yml upfront -if [ -f "$WORKDIR/ansible/requirements.yml" ]; then +if [ -f "${WORKDIR}/ansible/requirements.yml" ]; then log "Ensuring required Ansible collections are installed..." - ansible-galaxy collection install -r "$WORKDIR/ansible/requirements.yml" 2>&1 + ansible-galaxy collection install -r "${WORKDIR}/ansible/requirements.yml" 2>&1 log "Ensuring required Ansible roles are installed..." - ansible-galaxy role install -r "$WORKDIR/ansible/requirements.yml" 2>&1 + ansible-galaxy role install -r "${WORKDIR}/ansible/requirements.yml" 2>&1 fi # Run ansible-pull to enforce configuration state @@ -90,11 +90,11 @@ fi wait_for_apt_locks ansible-pull \ - --url "$REPO_URL" \ + --url "${REPO_URL}" \ --checkout main \ - --directory "$WORKDIR" \ - --inventory "$INVENTORY_PATH" \ - --extra-vars "target_host=$TARGET_HOST" \ - "$PLAYBOOK_PATH" + --directory "${WORKDIR}" \ + --inventory "${INVENTORY_PATH}" \ + --extra-vars "target_host=${TARGET_HOST}" \ + "${PLAYBOOK_PATH}" log "Ansible-pull completed successfully" diff --git a/tests/bash/run-tests.sh b/tests/bash/run-tests.sh index da8dcf5..768d233 100755 --- a/tests/bash/run-tests.sh +++ b/tests/bash/run-tests.sh @@ -63,9 +63,9 @@ echo "" if command -v mise >/dev/null 2>&1; then echo -e "${BLUE}🔍 Checking tool versions via mise...${NC}" MISE_OUTDATED=$(mise ls --missing 2>/dev/null || true) - if [ -n "$MISE_OUTDATED" ]; then + if [ -n "${MISE_OUTDATED}" ]; then echo -e "${YELLOW}⚠️ Some mise tools are missing or outdated:${NC}" - echo "$MISE_OUTDATED" + echo "${MISE_OUTDATED}" echo -e "${YELLOW} Running 'mise install' to sync...${NC}" mise install 2>&1 fi @@ -82,7 +82,7 @@ fi if ! command -v bats >/dev/null 2>&1; then echo -e "${YELLOW}📦 Bats not found, installing...${NC}" - if [ "$CI_MODE" = true ]; then + if [ "${CI_MODE}" = true ]; then # Install in CI mode echo "Installing Bats from GitHub..." BATS_VERSION="1.14.0" @@ -127,7 +127,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" TESTS_DIR="${SCRIPT_DIR}" # Make OUTPUT_FILE path absolute if it's relative -if [[ ! "$OUTPUT_FILE" = /* ]]; then +if [[ ! "${OUTPUT_FILE}" = /* ]]; then OUTPUT_FILE="${SCRIPT_DIR}/${OUTPUT_FILE}" fi @@ -140,25 +140,25 @@ if [ ${#SPECIFIC_TESTS[@]} -gt 0 ]; then for test in "${SPECIFIC_TESTS[@]}"; do if [ -f "${TESTS_DIR}/${test}" ]; then TEST_FILES+=("${TESTS_DIR}/${test}") - echo " 📝 Found: $(basename "$test")" - elif [ -f "$test" ]; then - TEST_FILES+=("$test") - echo " 📝 Found: $(basename "$test")" + echo " 📝 Found: $(basename "${test}")" + elif [ -f "${test}" ]; then + TEST_FILES+=("${test}") + echo " 📝 Found: $(basename "${test}")" else - echo " ❌ Test not found: $test" + echo " ❌ Test not found: ${test}" exit 1 fi done else # Run all tests while IFS= read -r -d '' file; do - TEST_FILES+=("$file") - echo " 📝 Found: $(basename "$file")" - done < <(find "$TESTS_DIR" -name "*.bats" -print0) + TEST_FILES+=("${file}") + echo " 📝 Found: $(basename "${file}")" + done < <(find "${TESTS_DIR}" -name "*.bats" -print0) fi if [ ${#TEST_FILES[@]} -eq 0 ]; then - echo -e "${YELLOW}⚠️ No test files found in $TESTS_DIR${NC}" + echo -e "${YELLOW}⚠️ No test files found in ${TESTS_DIR}${NC}" exit 0 fi @@ -167,21 +167,21 @@ echo -e "${BLUE}🧪 Running ${#TEST_FILES[@]} test file(s)...${NC}" echo "" # Run tests -if [ "$CI_MODE" = true ]; then +if [ "${CI_MODE}" = true ]; then # In CI mode, produce human-readable output on terminal AND JUnit XML for reporting - if [ "$OUTPUT_FORMAT" = "junit" ]; then + if [ "${OUTPUT_FORMAT}" = "junit" ]; then # Run with pretty terminal output and a separate JUnit report file # --report-formatter writes JUnit XML to --output directory - REPORT_DIR="$(dirname "$OUTPUT_FILE")" - bats --formatter tap --report-formatter junit --output "$REPORT_DIR" "${TEST_FILES[@]}" + REPORT_DIR="$(dirname "${OUTPUT_FILE}")" + bats --formatter tap --report-formatter junit --output "${REPORT_DIR}" "${TEST_FILES[@]}" EXIT_CODE=$? # Rename the report file to the expected output filename if [ -f "${REPORT_DIR}/report.xml" ]; then - mv "${REPORT_DIR}/report.xml" "$OUTPUT_FILE" + mv "${REPORT_DIR}/report.xml" "${OUTPUT_FILE}" fi else # Use TAP format - bats --tap "${TEST_FILES[@]}" 2>&1 | tee "$OUTPUT_FILE" + bats --tap "${TEST_FILES[@]}" 2>&1 | tee "${OUTPUT_FILE}" EXIT_CODE=${PIPESTATUS[0]} fi else @@ -192,7 +192,7 @@ fi # Display results based on exit code echo "" -if [ "$EXIT_CODE" -eq 0 ]; then +if [ "${EXIT_CODE}" -eq 0 ]; then echo -e "${GREEN}╔════════════════════════════════════════╗${NC}" echo -e "${GREEN}║ ✅ All Tests Passed! ║${NC}" echo -e "${GREEN}╚════════════════════════════════════════╝${NC}" @@ -203,20 +203,20 @@ else fi # In CI mode, display full output and summary -if [ "$CI_MODE" = true ]; then +if [ "${CI_MODE}" = true ]; then echo "" echo -e "${BLUE}📊 Test Results Summary:${NC}" - if [ -f "$OUTPUT_FILE" ]; then - if [ "$OUTPUT_FORMAT" = "junit" ]; then - echo " JUnit XML report generated: $OUTPUT_FILE" + if [ -f "${OUTPUT_FILE}" ]; then + if [ "${OUTPUT_FORMAT}" = "junit" ]; then + echo " JUnit XML report generated: ${OUTPUT_FILE}" # Display a simple summary from JUnit XML if command -v xmllint >/dev/null 2>&1; then # Validate XML first - if xmllint --noout "$OUTPUT_FILE" 2>/dev/null; then - TOTAL=$(xmllint --xpath "count(//testcase)" "$OUTPUT_FILE" 2>/dev/null || echo "0") - FAILURES=$(xmllint --xpath "count(//testcase/failure)" "$OUTPUT_FILE" 2>/dev/null || echo "0") - ERRORS=$(xmllint --xpath "count(//testcase/error)" "$OUTPUT_FILE" 2>/dev/null || echo "0") - echo " Total: $TOTAL, Failures: $FAILURES, Errors: $ERRORS" + if xmllint --noout "${OUTPUT_FILE}" 2>/dev/null; then + TOTAL=$(xmllint --xpath "count(//testcase)" "${OUTPUT_FILE}" 2>/dev/null || echo "0") + FAILURES=$(xmllint --xpath "count(//testcase/failure)" "${OUTPUT_FILE}" 2>/dev/null || echo "0") + ERRORS=$(xmllint --xpath "count(//testcase/error)" "${OUTPUT_FILE}" 2>/dev/null || echo "0") + echo " Total: ${TOTAL}, Failures: ${FAILURES}, Errors: ${ERRORS}" else echo " ⚠️ Warning: XML report is malformed or empty. Check test execution for errors." fi @@ -225,15 +225,15 @@ if [ "$CI_MODE" = true ]; then fi else # TAP format summary - grep -E "^(ok|not ok)" "$OUTPUT_FILE" | sort | uniq -c || echo " No test results found" + grep -E "^(ok|not ok)" "${OUTPUT_FILE}" | sort | uniq -c || echo " No test results found" fi fi - if [ "$OUTPUT_FORMAT" = "tap" ]; then + if [ "${OUTPUT_FORMAT}" = "tap" ]; then echo "" echo -e "${BLUE}📋 Full Test Output:${NC}" - cat "$OUTPUT_FILE" + cat "${OUTPUT_FILE}" fi fi -exit "$EXIT_CODE" +exit "${EXIT_CODE}"