|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +failures=0 |
| 5 | +export LC_ALL=C |
| 6 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 7 | + |
| 8 | +RED='\033[0;31m' |
| 9 | +GREEN='\033[0;32m' |
| 10 | +YELLOW='\033[1;33m' |
| 11 | +NC='\033[0m' |
| 12 | + |
| 13 | +TEST_DIR=$(mktemp -d) |
| 14 | +echo -e "${YELLOW}Creating temporary test directory: $TEST_DIR${NC}" |
| 15 | +trap 'rm -rf "$TEST_DIR"' EXIT |
| 16 | + |
| 17 | +echo -e "${YELLOW}Copying test files to temporary directory${NC}" |
| 18 | +list_file="${SCRIPT_DIR}/../list" |
| 19 | +inactive_file="${SCRIPT_DIR}/../inactive" |
| 20 | + |
| 21 | +test_list_file="${TEST_DIR}/test_list" |
| 22 | +test_inactive_file="${TEST_DIR}/test_inactive" |
| 23 | + |
| 24 | +cp "$list_file" "$test_list_file" |
| 25 | +cp "$inactive_file" "$test_inactive_file" |
| 26 | + |
| 27 | +export LIST_FILE="$test_list_file" |
| 28 | +export INACTIVE_FILE="$test_inactive_file" |
| 29 | + |
| 30 | +# Test helper function |
| 31 | +run_test() { |
| 32 | + local name="$1" |
| 33 | + local code="$2" |
| 34 | + if [ "$code" -eq 0 ]; then |
| 35 | + echo -e "${GREEN}Test $name: PASSED${NC}" |
| 36 | + else |
| 37 | + echo -e "${RED}Test $name: FAILED${NC}" |
| 38 | + failures=$((failures+1)) |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +# Testing: Add domain |
| 43 | +echo |
| 44 | +echo -e "${YELLOW}Testing: Add domain${NC}" |
| 45 | +"${SCRIPT_DIR}/../manage_domains.sh" add "newdomain.com" |
| 46 | +run_test "add domain" "$(grep -q "^newdomain.com$" "$test_list_file"; echo $?)" |
| 47 | +echo |
| 48 | + |
| 49 | +# Testing: Add invalid domain |
| 50 | +echo -e "${YELLOW}Testing: Add invalid domain${NC}" |
| 51 | +if "${SCRIPT_DIR}/../manage_domains.sh" add "invalid..domain" 2>/dev/null; then |
| 52 | + run_test "reject invalid domain" 1 |
| 53 | +else |
| 54 | + run_test "reject invalid domain" 0 |
| 55 | +fi |
| 56 | +echo |
| 57 | + |
| 58 | +# Testing: Move domain to inactive |
| 59 | +echo -e "${YELLOW}Testing: Move domain to inactive${NC}" |
| 60 | +"${SCRIPT_DIR}/../manage_domains.sh" move "newdomain.com" |
| 61 | +run_test "move domain" "$(grep -q "^newdomain.com$" "$test_inactive_file" && ! grep -q "^newdomain.com$" "$test_list_file"; echo $?)" |
| 62 | +echo |
| 63 | + |
| 64 | +# Testing: Move non-existent domain |
| 65 | +echo -e "${YELLOW}Testing: Move non-existent domain${NC}" |
| 66 | +if "${SCRIPT_DIR}/../manage_domains.sh" move "nonexistentdomain.com" 2>/dev/null; then |
| 67 | + run_test "graceful handling of non-existent domain" "$( ! grep -q "^nonexistentdomain.com$" "$test_inactive_file"; echo $?)" |
| 68 | +else |
| 69 | + run_test "graceful handling of non-existent domain" 1 |
| 70 | +fi |
| 71 | +echo |
| 72 | + |
| 73 | +# Testing: File sorting and comment preservation |
| 74 | +echo -e "${YELLOW}Testing: File sorting and comment preservation${NC}" |
| 75 | +"${SCRIPT_DIR}/../manage_domains.sh" add "anotherdomain.com" |
| 76 | +"${SCRIPT_DIR}/../manage_domains.sh" move "anotherdomain.com" |
| 77 | +run_test "file sorting and comments" "$(diff -u <(grep -v -E '^(#|$)' "$test_list_file" | sort) <(grep -v -E '^(#|$)' "$test_list_file") && diff -u <(grep -v -E '^(#|$)' "$test_inactive_file" | sort) <(grep -v -E '^(#|$)' "$test_inactive_file"); echo $?)" |
| 78 | +echo |
| 79 | + |
| 80 | +# Testing: Comment preservation |
| 81 | +echo -e "${YELLOW}Testing: Comment preservation${NC}" |
| 82 | +orig_comments=$(grep '^#' "$list_file") |
| 83 | +new_comments=$(grep '^#' "$test_list_file") |
| 84 | +if [ "$orig_comments" = "$new_comments" ]; then |
| 85 | + echo -e "${GREEN}Test comment preservation (active): PASSED${NC}" |
| 86 | +else |
| 87 | + echo -e "${RED}Test comment preservation (active): FAILED${NC}" |
| 88 | + failures=$((failures+1)) |
| 89 | +fi |
| 90 | + |
| 91 | +orig_comments_inactive=$(grep '^#' "$inactive_file") |
| 92 | +new_comments_inactive=$(grep '^#' "$test_inactive_file") |
| 93 | +if [ "$orig_comments_inactive" = "$new_comments_inactive" ]; then |
| 94 | + echo -e "${GREEN}Test comment preservation (inactive): PASSED${NC}" |
| 95 | +else |
| 96 | + echo -e "${RED}Test comment preservation (inactive): FAILED${NC}" |
| 97 | + failures=$((failures+1)) |
| 98 | +fi |
| 99 | + |
| 100 | +echo |
| 101 | +echo -e "${YELLOW}Test summary:${NC}" |
| 102 | +if [ "$failures" -eq 0 ]; then |
| 103 | + echo -e "${GREEN}All tests passed${NC}" |
| 104 | +else |
| 105 | + echo -e "${RED}$failures tests failed${NC}" |
| 106 | +fi |
| 107 | + |
| 108 | +exit "$failures" |
0 commit comments