1+ #! /usr/bin/env bash
2+
3+ # Test CI environment bash fallback behavior
4+
5+ # Minimal test framework for standalone running
6+ TESTS_PASSED=0
7+ TESTS_FAILED=0
8+
9+ run_test () {
10+ local test_name=" $1 "
11+ echo " "
12+ echo " Running: $test_name "
13+ if $test_name ; then
14+ (( TESTS_PASSED++ ))
15+ echo " ✓ $test_name passed"
16+ else
17+ (( TESTS_FAILED++ ))
18+ echo " ✗ $test_name failed"
19+ fi
20+ }
21+
22+ test_ci_environment_detection () {
23+ echo " Testing CI environment detection..."
24+
25+ # Test 1: CI=true should trigger fallback mode
26+ local output=$( CI=true /bin/bash -c ' source ./lib/common.sh 2>&1' || true)
27+ if echo " $output " | grep -q " Running with bash .* in CI environment" ; then
28+ echo " ✓ CI=true detected correctly"
29+ else
30+ echo " ✗ CI=true not detected"
31+ return 1
32+ fi
33+
34+ # Test 2: GITHUB_ACTIONS=true should trigger fallback mode
35+ output=$( GITHUB_ACTIONS=true /bin/bash -c ' source ./lib/common.sh 2>&1' || true)
36+ if echo " $output " | grep -q " Running with bash .* in CI environment" ; then
37+ echo " ✓ GITHUB_ACTIONS=true detected correctly"
38+ else
39+ echo " ✗ GITHUB_ACTIONS=true not detected"
40+ return 1
41+ fi
42+
43+ # Test 3: Both CI and GITHUB_ACTIONS should work
44+ output=$( CI=true GITHUB_ACTIONS=true /bin/bash -c ' source ./lib/common.sh 2>&1' || true)
45+ if echo " $output " | grep -q " Running with bash .* in CI environment" ; then
46+ echo " ✓ Both CI and GITHUB_ACTIONS detected correctly"
47+ else
48+ echo " ✗ Both flags not detected"
49+ return 1
50+ fi
51+
52+ # Test 4: Check specific disabled features message
53+ if echo " $output " | grep -q " Disabled features: MCP server management, associative arrays, indirect expansion" ; then
54+ echo " ✓ Disabled features message is correct"
55+ else
56+ echo " ✗ Disabled features message is missing or incorrect"
57+ return 1
58+ fi
59+
60+ # Test 5: Check available features message
61+ if echo " $output " | grep -q " Available features: Basic setup preview, simple commands, file operations" ; then
62+ echo " ✓ Available features message is correct"
63+ else
64+ echo " ✗ Available features message is missing or incorrect"
65+ return 1
66+ fi
67+ }
68+
69+ test_ci_minimal_functions () {
70+ echo " Testing CI minimal functions..."
71+
72+ # Create a test script that uses CI functions
73+ local test_script=$( mktemp)
74+ cat > " $test_script " << 'EOF '
75+ #!/bin/bash
76+ export CI=true
77+ source ./lib/common.sh
78+
79+ # Test the minimal functions exist and work
80+ ci_print_info "Test info message"
81+ ci_print_success "Test success message"
82+ ci_print_warning "Test warning message"
83+ ci_print_step "Test step message"
84+ ci_print_error "Test error message"
85+ EOF
86+
87+ # Run with system bash and check output
88+ local output=$( /bin/bash " $test_script " 2>&1 )
89+ rm -f " $test_script "
90+
91+ # Check each function output
92+ if echo " $output " | grep -q " ℹ Test info message" ; then
93+ echo " ✓ ci_print_info works"
94+ else
95+ echo " ✗ ci_print_info failed"
96+ return 1
97+ fi
98+
99+ if echo " $output " | grep -q " ✓ Test success message" ; then
100+ echo " ✓ ci_print_success works"
101+ else
102+ echo " ✗ ci_print_success failed"
103+ return 1
104+ fi
105+
106+ if echo " $output " | grep -q " ⚠ Test warning message" ; then
107+ echo " ✓ ci_print_warning works"
108+ else
109+ echo " ✗ ci_print_warning failed"
110+ return 1
111+ fi
112+
113+ if echo " $output " | grep -q " → Test step message" ; then
114+ echo " ✓ ci_print_step works"
115+ else
116+ echo " ✗ ci_print_step failed"
117+ return 1
118+ fi
119+
120+ if echo " $output " | grep -q " ✗ Test error message" ; then
121+ echo " ✓ ci_print_error works"
122+ else
123+ echo " ✗ ci_print_error failed"
124+ return 1
125+ fi
126+ }
127+
128+ test_ci_logging_support () {
129+ echo " Testing CI logging support..."
130+
131+ local log_file=$( mktemp)
132+ local test_script=$( mktemp)
133+
134+ cat > " $test_script " << EOF
135+ #!/bin/bash
136+ export CI=true
137+ export LOG_FILE="$log_file "
138+ export SCRIPT_NAME="test_ci"
139+ source ./lib/common.sh
140+
141+ ci_print_info "Test log message"
142+ EOF
143+
144+ # Run the script
145+ /bin/bash " $test_script " > /dev/null 2>&1
146+
147+ # Check if log file contains the message
148+ if [[ -f " $log_file " ]] && grep -q " INFO: Test log message" " $log_file " ; then
149+ echo " ✓ Logging works in CI mode"
150+ else
151+ echo " ✗ Logging failed in CI mode"
152+ cat " $log_file " 2> /dev/null || echo " Log file not created"
153+ rm -f " $test_script " " $log_file "
154+ return 1
155+ fi
156+
157+ # Check log format
158+ if grep -q " \[test_ci\] INFO: Test log message" " $log_file " ; then
159+ echo " ✓ Log format is correct"
160+ else
161+ echo " ✗ Log format is incorrect"
162+ rm -f " $test_script " " $log_file "
163+ return 1
164+ fi
165+
166+ rm -f " $test_script " " $log_file "
167+ }
168+
169+ test_non_ci_behavior () {
170+ echo " Testing non-CI behavior..."
171+
172+ # Without CI flags, system bash should fail
173+ local output=$( /bin/bash -c ' source ./lib/common.sh 2>&1' || true)
174+
175+ if echo " $output " | grep -q " Error: This script requires bash 4.0 or higher" ; then
176+ echo " ✓ Non-CI mode correctly requires bash 4+"
177+ else
178+ echo " ✗ Non-CI mode didn't enforce bash version"
179+ return 1
180+ fi
181+ }
182+
183+ test_bash_version_logging () {
184+ echo " Testing bash version logging..."
185+
186+ local log_file=$( mktemp)
187+ local test_script=$( mktemp)
188+
189+ cat > " $test_script " << EOF
190+ #!/bin/bash
191+ export CI=true
192+ export LOG_FILE="$log_file "
193+ source ./lib/common.sh
194+ EOF
195+
196+ # Run the script
197+ /bin/bash " $test_script " > /dev/null 2>&1
198+
199+ # Check if bash version was logged
200+ if [[ -f " $log_file " ]] && grep -q " CI Mode: bash .* detected" " $log_file " ; then
201+ echo " ✓ Bash version logged for debugging"
202+ else
203+ echo " ✗ Bash version not logged"
204+ rm -f " $test_script " " $log_file "
205+ return 1
206+ fi
207+
208+ rm -f " $test_script " " $log_file "
209+ }
210+
211+ # Run all tests
212+ run_test test_ci_environment_detection
213+ run_test test_ci_minimal_functions
214+ run_test test_ci_logging_support
215+ run_test test_non_ci_behavior
216+ run_test test_bash_version_logging
217+
218+ # Summary
219+ echo " "
220+ echo " CI Bash Fallback Test Summary:"
221+ echo " Passed: $TESTS_PASSED "
222+ echo " Failed: $TESTS_FAILED "
223+
224+ [[ $TESTS_FAILED -eq 0 ]]
0 commit comments