forked from ksred/ccswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_wrapper_test.sh
More file actions
executable file
·126 lines (109 loc) · 3.53 KB
/
bash_wrapper_test.sh
File metadata and controls
executable file
·126 lines (109 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
# Test script for the bash wrapper function
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Test counter
TESTS_RUN=0
TESTS_PASSED=0
# Mock ccswitch command for testing
mock_ccswitch() {
case "$1" in
"list")
echo "Listing sessions..."
;;
"cleanup")
echo "Cleaning up session: $2"
;;
*)
# Mock session creation output
echo "🚀 What are you working on? ✓ Created session: feature/test-feature"
echo " Branch: feature/test-feature"
echo " Path: /path/to/test-feature"
echo ""
echo "# Run this to enter the session:"
echo "cd ../test-feature"
;;
esac
}
# Define the wrapper function manually for testing (simulates shell-init output)
ccswitch() {
case "$1" in
list|cleanup|info|shell-init)
# These commands don't need special handling
mock_ccswitch "$@"
;;
switch)
# For switch command, capture output and execute cd command
local output=$(mock_ccswitch "$@")
echo "$output"
# Extract and execute the cd command if switch was successful
local cd_cmd=$(echo "$output" | grep "^cd " | tail -1)
if [ -n "$cd_cmd" ]; then
eval "$cd_cmd"
fi
;;
create|*)
# For session creation (default command and explicit create)
local output=$(mock_ccswitch "$@")
echo "$output"
# Extract and execute the cd command if session was created successfully
local cd_cmd=$(echo "$output" | grep "^cd " | tail -1)
if [ -n "$cd_cmd" ]; then
eval "$cd_cmd"
fi
;;
esac
}
# Test function
run_test() {
local test_name="$1"
local expected="$2"
local actual="$3"
TESTS_RUN=$((TESTS_RUN + 1))
if [ "$expected" = "$actual" ]; then
echo -e "${GREEN}✓${NC} $test_name"
TESTS_PASSED=$((TESTS_PASSED + 1))
else
echo -e "${RED}✗${NC} $test_name"
echo " Expected: $expected"
echo " Actual: $actual"
fi
}
# Test 1: List command should pass through directly
echo "Running bash wrapper tests..."
echo
output=$(ccswitch list 2>&1)
run_test "List command passthrough" "Listing sessions..." "$output"
# Test 2: Cleanup command should pass through directly
output=$(ccswitch cleanup test-session 2>&1)
run_test "Cleanup command passthrough" "Cleaning up session: test-session" "$output"
# Test 3: Session creation should capture and execute cd command
# This is harder to test directly since we can't actually change directories in a subshell
# We'll test that the output contains the expected text
output=$(ccswitch 2>&1)
if echo "$output" | grep -q "Created session: feature/test-feature" && \
echo "$output" | grep -q "cd ../test-feature"; then
run_test "Session creation output" "success" "success"
else
run_test "Session creation output" "success" "failure"
fi
# Test 4: Empty/no arguments should work
output=$(ccswitch 2>&1)
if [ -n "$output" ]; then
run_test "No arguments handling" "success" "success"
else
run_test "No arguments handling" "success" "failure"
fi
# Summary
echo
echo "Tests run: $TESTS_RUN"
echo "Tests passed: $TESTS_PASSED"
if [ "$TESTS_RUN" -eq "$TESTS_PASSED" ]; then
echo -e "${GREEN}All tests passed!${NC}"
exit 0
else
echo -e "${RED}Some tests failed!${NC}"
exit 1
fi