|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# test_utils.sh - Utilities for testing the context script |
| 4 | + |
| 5 | +# Initialize test counters |
| 6 | +TESTS_PASSED=0 |
| 7 | +TESTS_FAILED=0 |
| 8 | + |
| 9 | +# Get the directory where the test_utils.sh script is located |
| 10 | +TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 11 | +PROJECT_ROOT="$(cd "$TEST_DIR/.." && pwd)" |
| 12 | + |
| 13 | +# Function to run a test |
| 14 | +# Usage: run_test "test_name" "command_to_run" |
| 15 | +run_test() { |
| 16 | + local test_name=$1 |
| 17 | + local command=$2 |
| 18 | + |
| 19 | + echo -n "- Testing $test_name... " |
| 20 | + |
| 21 | + # Run the command and capture output and exit status |
| 22 | + output=$(eval $command 2>&1) |
| 23 | + status=$? |
| 24 | + |
| 25 | + if [ $status -eq 0 ]; then |
| 26 | + echo "PASSED" |
| 27 | + TESTS_PASSED=$((TESTS_PASSED + 1)) |
| 28 | + else |
| 29 | + echo "FAILED" |
| 30 | + echo " Command: $command" |
| 31 | + echo " Output: $output" |
| 32 | + TESTS_FAILED=$((TESTS_FAILED + 1)) |
| 33 | + fi |
| 34 | + |
| 35 | + return $status |
| 36 | +} |
| 37 | + |
| 38 | +# Function to create a temporary directory for tests |
| 39 | +# Usage: create_test_dir |
| 40 | +create_test_dir() { |
| 41 | + mktemp -d -t context-test-XXXXXX |
| 42 | +} |
| 43 | + |
| 44 | +# Function to clean up a temporary directory |
| 45 | +# Usage: cleanup_test_dir "$test_dir" |
| 46 | +cleanup_test_dir() { |
| 47 | + local test_dir=$1 |
| 48 | + rm -rf "$test_dir" |
| 49 | +} |
| 50 | + |
| 51 | +# Function to assert that a string contains another string |
| 52 | +# Usage: assert_contains "haystack" "needle" |
| 53 | +assert_contains() { |
| 54 | + local haystack=$1 |
| 55 | + local needle=$2 |
| 56 | + |
| 57 | + if [[ "$haystack" == *"$needle"* ]]; then |
| 58 | + return 0 |
| 59 | + else |
| 60 | + return 1 |
| 61 | + fi |
| 62 | +} |
| 63 | + |
| 64 | +# Function to assert that a string does not contain another string |
| 65 | +# Usage: assert_not_contains "haystack" "needle" |
| 66 | +assert_not_contains() { |
| 67 | + local haystack=$1 |
| 68 | + local needle=$2 |
| 69 | + |
| 70 | + if [[ "$haystack" != *"$needle"* ]]; then |
| 71 | + return 0 |
| 72 | + else |
| 73 | + return 1 |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +# Function to create a test file with a specific size |
| 78 | +# Usage: create_test_file "$dir/file.txt" 1024 # Creates a 1KB file |
| 79 | +create_test_file() { |
| 80 | + local file_path=$1 |
| 81 | + local size_bytes=$2 |
| 82 | + |
| 83 | + dd if=/dev/zero of="$file_path" bs=1 count=$size_bytes status=none |
| 84 | +} |
0 commit comments