|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# test_utils.sh - Utilities for testing the context script |
| 3 | +# test_utils.sh - Utilities for testing the context script (TAP-compliant) |
| 4 | + |
| 5 | +# Get the directory where the test_utils.sh script is located |
| 6 | +TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
4 | 7 |
|
5 | 8 | # Initialize test counters |
6 | 9 | TESTS_PASSED=0 |
7 | 10 | TESTS_FAILED=0 |
| 11 | +TEST_NUMBER_INTERNAL=0 |
| 12 | + |
| 13 | +# Function to update test result counts |
| 14 | +update_test_results() { |
| 15 | + echo "PASSED:$TESTS_PASSED" > "$TEST_DIR/test_results.txt" |
| 16 | + echo "FAILED:$TESTS_FAILED" >> "$TEST_DIR/test_results.txt" |
| 17 | +} |
8 | 18 |
|
9 | | -# Function to run a test |
10 | | -# Usage: run_test "test_name" "command_to_run" |
| 19 | +# Function to run a test and output TAP-compliant results |
| 20 | +# Usage: run_test "test_description" "command_to_run" |
11 | 21 | run_test() { |
12 | | - local test_name=$1 |
13 | | - local command=$2 |
| 22 | + local description="$1" |
| 23 | + local command="$2" |
| 24 | + local skip_reason="${3:-}" |
| 25 | + |
| 26 | + TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1)) |
| 27 | + |
| 28 | + # Skip test if reason provided |
| 29 | + if [ -n "$skip_reason" ]; then |
| 30 | + echo "ok $TEST_NUMBER_INTERNAL # SKIP $description - $skip_reason" |
| 31 | + return 0 |
| 32 | + fi |
14 | 33 |
|
15 | | - echo -n "- Testing $test_name... " |
| 34 | + # Create temporary files for command output and error |
| 35 | + local output_file=$(mktemp) |
| 36 | + local error_file=$(mktemp) |
16 | 37 |
|
17 | | - # Run the command and capture output and exit status |
18 | | - output=$(eval "$command" 2>&1) |
19 | | - status=$? |
| 38 | + # Run the command |
| 39 | + eval "$command" > "$output_file" 2> "$error_file" |
| 40 | + local status=$? |
20 | 41 |
|
| 42 | + local output=$(cat "$output_file") |
| 43 | + local error=$(cat "$error_file") |
| 44 | + |
| 45 | + # Handle the result |
21 | 46 | if [ $status -eq 0 ]; then |
22 | | - echo "PASSED" |
| 47 | + echo "ok $TEST_NUMBER_INTERNAL - $description" |
23 | 48 | TESTS_PASSED=$((TESTS_PASSED + 1)) |
24 | 49 | else |
25 | | - echo "FAILED" |
26 | | - echo " Command: $command" |
27 | | - echo " Output: $output" |
| 50 | + echo "not ok $TEST_NUMBER_INTERNAL - $description" |
| 51 | + echo "# Command: $command" |
| 52 | + |
| 53 | + if [ -n "$output" ]; then |
| 54 | + echo "# Output:" |
| 55 | + echo "$output" | sed 's/^/# /' |
| 56 | + fi |
| 57 | + |
| 58 | + if [ -n "$error" ]; then |
| 59 | + echo "# Error:" |
| 60 | + echo "$error" | sed 's/^/# /' |
| 61 | + fi |
| 62 | + |
28 | 63 | TESTS_FAILED=$((TESTS_FAILED + 1)) |
29 | 64 | fi |
30 | 65 |
|
| 66 | + # Clean up temporary files |
| 67 | + rm -f "$output_file" "$error_file" |
| 68 | + update_test_results |
| 69 | + |
31 | 70 | return $status |
32 | 71 | } |
33 | 72 |
|
34 | 73 | # Function to create a temporary directory for tests |
35 | 74 | # Usage: create_test_dir |
36 | 75 | create_test_dir() { |
37 | | - mktemp -d -t context-test-XXXXXX |
| 76 | + local tmp_dir=$(mktemp -d -t "context-test-XXXXXX") |
| 77 | + echo "$tmp_dir" |
38 | 78 | } |
39 | 79 |
|
40 | 80 | # Function to clean up a temporary directory |
41 | 81 | # Usage: cleanup_test_dir "$test_dir" |
42 | 82 | cleanup_test_dir() { |
43 | | - local test_dir=$1 |
| 83 | + local test_dir="$1" |
44 | 84 | if [ -d "$test_dir" ]; then |
45 | 85 | rm -rf "$test_dir" |
46 | 86 | fi |
47 | 87 | } |
48 | 88 |
|
49 | | -# Function to assert that a string contains another string |
50 | | -# Usage: assert_contains "haystack" "needle" |
51 | | -assert_contains() { |
52 | | - local haystack=$1 |
53 | | - local needle=$2 |
54 | | - |
55 | | - if [[ "$haystack" == *"$needle"* ]]; then |
56 | | - return 0 |
57 | | - else |
58 | | - return 1 |
59 | | - fi |
| 89 | +# Function to print a message in TAP diagnostic format |
| 90 | +# Usage: tap_diag "message" |
| 91 | +tap_diag() { |
| 92 | + echo "# $1" |
60 | 93 | } |
61 | 94 |
|
62 | | -# Function to assert that a string does not contain another string |
63 | | -# Usage: assert_not_contains "haystack" "needle" |
64 | | -assert_not_contains() { |
65 | | - local haystack=$1 |
66 | | - local needle=$2 |
| 95 | +# Function to assert that a condition is true |
| 96 | +# Usage: assert "test expr" "description" |
| 97 | +assert() { |
| 98 | + local condition="$1" |
| 99 | + local description="$2" |
67 | 100 |
|
68 | | - if [[ "$haystack" != *"$needle"* ]]; then |
69 | | - return 0 |
| 101 | + TEST_NUMBER_INTERNAL=$((TEST_NUMBER_INTERNAL + 1)) |
| 102 | + |
| 103 | + if eval "$condition"; then |
| 104 | + echo "ok $TEST_NUMBER_INTERNAL - $description" |
| 105 | + TESTS_PASSED=$((TESTS_PASSED + 1)) |
70 | 106 | else |
71 | | - return 1 |
| 107 | + echo "not ok $TEST_NUMBER_INTERNAL - $description" |
| 108 | + echo "# Failed condition: $condition" |
| 109 | + TESTS_FAILED=$((TESTS_FAILED + 1)) |
72 | 110 | fi |
| 111 | + |
| 112 | + update_test_results |
73 | 113 | } |
74 | 114 |
|
75 | 115 | # Function to create a test file with a specific size |
76 | 116 | # Usage: create_test_file "$dir/file.txt" 1024 # Creates a 1KB file |
77 | 117 | create_test_file() { |
78 | | - local file_path=$1 |
79 | | - local size_bytes=$2 |
| 118 | + local file_path="$1" |
| 119 | + local size_bytes="$2" |
| 120 | + |
| 121 | + # Create a file with human-readable content at the start |
| 122 | + echo "This is a test file created for context script testing." > "$file_path" |
| 123 | + echo "It contains exactly $size_bytes bytes of data." >> "$file_path" |
80 | 124 |
|
81 | | - dd if=/dev/zero of="$file_path" bs=1 count="$size_bytes" status=none 2>/dev/null |
| 125 | + # Calculate how many bytes we need to add to reach the desired size |
| 126 | + local current_size=$(wc -c < "$file_path") |
| 127 | + local remaining_bytes=$((size_bytes - current_size)) |
| 128 | + |
| 129 | + if [ $remaining_bytes -gt 0 ]; then |
| 130 | + # Add remaining bytes to reach desired size |
| 131 | + dd if=/dev/zero bs=1 count=$remaining_bytes >> "$file_path" 2>/dev/null |
| 132 | + fi |
| 133 | + |
| 134 | + # Verify the file is the right size |
| 135 | + local final_size=$(wc -c < "$file_path") |
| 136 | + if [ $final_size -ne $size_bytes ]; then |
| 137 | + echo "# Warning: Created file size ($final_size) doesn't match requested size ($size_bytes)" >&2 |
| 138 | + fi |
| 139 | +} |
| 140 | + |
| 141 | +# Print TAP plan for individual test files |
| 142 | +plan() { |
| 143 | + local count="$1" |
| 144 | + echo "1..$count" |
82 | 145 | } |
83 | 146 |
|
84 | | -# Export variables to be available in subprocesses |
85 | | -export TESTS_PASSED |
86 | | -export TESTS_FAILED |
| 147 | +# Export functions and variables for use in subshells |
| 148 | +export -f run_test |
| 149 | +export -f create_test_dir |
| 150 | +export -f cleanup_test_dir |
| 151 | +export -f tap_diag |
| 152 | +export -f assert |
| 153 | +export -f create_test_file |
| 154 | +export -f plan |
| 155 | +export -f update_test_results |
0 commit comments