#!/usr/bin/env bash # Unit tests for dns_enforcer.sh helper functions. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PASS=0 FAIL=0 _t_pass() { PASS=$((PASS + 1)) printf ' OK: %s\n' "$1" } _t_fail() { FAIL=$((FAIL + 1)) printf ' FAIL: %s\n' "$1" } TMPDIR_TEST="$(mktemp -d)" trap 'rm -rf "${TMPDIR_TEST}"' EXIT cat >"${TMPDIR_TEST}/config.sh" <"$block_file" append_unique_line "$block_file" "1.2.3.4" append_unique_line "$block_file" "1.2.3.4" append_unique_line "$block_file" "5.6.7.8" if [[ "$(wc -l < "$block_file" | tr -d ' ')" == "2" ]]; then _t_pass "append_unique_line de-duplicates values" else _t_fail "append_unique_line should avoid duplicates" fi printf '\nResults: %d passed, %d failed\n' "$PASS" "$FAIL" [[ "$FAIL" -eq 0 ]]