aboutsummaryrefslogtreecommitdiff
path: root/spec/cli/cli_test_helper.sh
diff options
context:
space:
mode:
Diffstat (limited to 'spec/cli/cli_test_helper.sh')
-rwxr-xr-xspec/cli/cli_test_helper.sh183
1 files changed, 183 insertions, 0 deletions
diff --git a/spec/cli/cli_test_helper.sh b/spec/cli/cli_test_helper.sh
new file mode 100755
index 0000000..ade1546
--- /dev/null
+++ b/spec/cli/cli_test_helper.sh
@@ -0,0 +1,183 @@
1#!/bin/bash
2# CLI Test Helper for YueScript
3# Provides utility functions for testing the yue command line tool
4
5# Colors for output
6RED='\033[0;31m'
7GREEN='\033[0;32m'
8YELLOW='\033[1;33m'
9NC='\033[0m' # No Color
10
11# Test counters
12TESTS_RUN=0
13TESTS_PASSED=0
14TESTS_FAILED=0
15
16# Get the yue binary path
17YUE_BIN="${YUE_BIN:-./bin/debug/yue}"
18
19# Check if yue binary exists
20check_yue_binary() {
21 if [ ! -f "$YUE_BIN" ]; then
22 echo -e "${RED}Error: yue binary not found at $YUE_BIN${NC}"
23 echo "Please build the project first or set YUE_BIN environment variable"
24 exit 1
25 fi
26 if [ ! -x "$YUE_BIN" ]; then
27 echo -e "${RED}Error: yue binary is not executable${NC}"
28 exit 1
29 fi
30}
31
32# Assert that a command succeeds
33assert_success() {
34 local description="$1"
35 shift
36 TESTS_RUN=$((TESTS_RUN + 1))
37
38 if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then
39 echo -e "${GREEN}✓${NC} $description"
40 TESTS_PASSED=$((TESTS_PASSED + 1))
41 return 0
42 else
43 echo -e "${RED}✗${NC} $description"
44 echo -e " ${YELLOW}Exit code: $?${NC}"
45 echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}"
46 echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}"
47 TESTS_FAILED=$((TESTS_FAILED + 1))
48 return 1
49 fi
50}
51
52# Assert that a command fails
53assert_failure() {
54 local description="$1"
55 shift
56 TESTS_RUN=$((TESTS_RUN + 1))
57
58 if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then
59 echo -e "${RED}✗${NC} $description (expected failure but succeeded)"
60 echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}"
61 echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}"
62 TESTS_FAILED=$((TESTS_FAILED + 1))
63 return 1
64 else
65 echo -e "${GREEN}✓${NC} $description"
66 TESTS_PASSED=$((TESTS_PASSED + 1))
67 return 0
68 fi
69}
70
71# Assert that output contains expected string
72assert_output_contains() {
73 local description="$1"
74 local expected="$2"
75 shift 2
76 TESTS_RUN=$((TESTS_RUN + 1))
77
78 if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then
79 if grep -qF -- "$expected" /tmp/test_stdout.txt || grep -qF -- "$expected" /tmp/test_stderr.txt; then
80 echo -e "${GREEN}✓${NC} $description"
81 TESTS_PASSED=$((TESTS_PASSED + 1))
82 return 0
83 else
84 echo -e "${RED}✗${NC} $description (output doesn't contain '$expected')"
85 echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}"
86 echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}"
87 TESTS_FAILED=$((TESTS_FAILED + 1))
88 return 1
89 fi
90 else
91 echo -e "${RED}✗${NC} $description (command failed)"
92 echo -e " ${YELLOW}Exit code: $?${NC}"
93 TESTS_FAILED=$((TESTS_FAILED + 1))
94 return 1
95 fi
96}
97
98# Assert that output equals expected string
99assert_output_equals() {
100 local description="$1"
101 local expected="$2"
102 shift 2
103 TESTS_RUN=$((TESTS_RUN + 1))
104
105 if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then
106 local actual=$(cat /tmp/test_stdout.txt)
107 if [ "$actual" = "$expected" ]; then
108 echo -e "${GREEN}✓${NC} $description"
109 TESTS_PASSED=$((TESTS_PASSED + 1))
110 return 0
111 else
112 echo -e "${RED}✗${NC} $description (output mismatch)"
113 echo -e " ${YELLOW}Expected: '$expected'${NC}"
114 echo -e " ${YELLOW}Actual: '$actual'${NC}"
115 TESTS_FAILED=$((TESTS_FAILED + 1))
116 return 1
117 fi
118 else
119 echo -e "${RED}✗${NC} $description (command failed)"
120 echo -e " ${YELLOW}Exit code: $?${NC}"
121 TESTS_FAILED=$((TESTS_FAILED + 1))
122 return 1
123 fi
124}
125
126# Assert file exists
127assert_file_exists() {
128 local description="$1"
129 local filepath="$2"
130 TESTS_RUN=$((TESTS_RUN + 1))
131
132 if [ -f "$filepath" ]; then
133 echo -e "${GREEN}✓${NC} $description"
134 TESTS_PASSED=$((TESTS_PASSED + 1))
135 return 0
136 else
137 echo -e "${RED}✗${NC} $description (file not found: $filepath)"
138 TESTS_FAILED=$((TESTS_FAILED + 1))
139 return 1
140 fi
141}
142
143# Print test summary
144print_summary() {
145 echo ""
146 echo "======================================"
147 echo "Test Summary"
148 echo "======================================"
149 echo "Total tests: $TESTS_RUN"
150 echo -e "Passed: ${GREEN}$TESTS_PASSED${NC}"
151 echo -e "Failed: ${RED}$TESTS_FAILED${NC}"
152 echo "======================================"
153
154 if [ $TESTS_FAILED -eq 0 ]; then
155 echo -e "${GREEN}All tests passed!${NC}"
156 return 0
157 else
158 echo -e "${RED}Some tests failed!${NC}"
159 return 1
160 fi
161}
162
163# Setup test environment
164setup_test_env() {
165 # Create temporary directory for test files
166 TEST_TMP_DIR=$(mktemp -d)
167 export TEST_TMP_DIR
168 trap "rm -rf $TEST_TMP_DIR" EXIT
169 echo "Test directory: $TEST_TMP_DIR"
170}
171
172# Get test tmp dir
173get_test_tmp_dir() {
174 echo "$TEST_TMP_DIR"
175}
176
177# Create a test yue file
178create_test_file() {
179 local filepath="$1"
180 local content="$2"
181 mkdir -p "$(dirname "$filepath")"
182 echo "$content" > "$filepath"
183}