@@ -29,34 +29,139 @@ function fail() {
2929 exit 1
3030}
3131
32+ # Test helper function to source gitstack.sh and make functions available for testing
33+ function source_gitstack() {
34+ # Source the main script to get access to internal functions
35+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
36+ echo " Sourcing from: $SCRIPT_DIR /gitstack.sh"
37+ source " $SCRIPT_DIR /gitstack.sh"
38+ }
39+
40+ # Test get_stack_info functionality
41+ function test_get_stack_info() {
42+ echo " Testing get_stack_info..."
43+
44+ # Create and checkout a test branch
45+ git checkout -b test-123 2> /dev/null
46+ echo " Created test-123 branch"
47+
48+ if get_stack_info; then
49+ echo " Stack info: BASE=$STACK_BASE NUM=$STACK_NUM "
50+ if [ " $STACK_BASE " = " test" ] && [ " $STACK_NUM " = " 123" ]; then
51+ echo " ✅ get_stack_info correctly parsed base='test' and num='123'"
52+ else
53+ fail " get_stack_info parsed incorrect values: base='$STACK_BASE ', num='$STACK_NUM '"
54+ fi
55+ else
56+ fail " get_stack_info failed to parse test-123"
57+ fi
58+
59+ # Test with non-stack branch
60+ git checkout -b not-a-stack-branch 2> /dev/null
61+ echo " Created not-a-stack-branch"
62+ if get_stack_info; then
63+ fail " get_stack_info incorrectly identified not-a-stack-branch as a stack branch"
64+ else
65+ echo " ✅ get_stack_info correctly rejected non-stack branch"
66+ fi
67+
68+ # Cleanup test branches
69+ git checkout main 2> /dev/null || git checkout master 2> /dev/null
70+ git branch -D test-123 not-a-stack-branch 2> /dev/null || true
71+ }
72+
73+ # Test get_stack_branches functionality
74+ function test_get_stack_branches() {
75+ echo " Testing get_stack_branches..."
76+
77+ # Create test branches
78+ git checkout -b bar-1 2> /dev/null
79+ git checkout -b bar-2 2> /dev/null
80+ git checkout -b bar-3 2> /dev/null
81+ git checkout -b other-1 2> /dev/null
82+ echo " Created test branches"
83+
84+ # Get branches and check count
85+ local branches
86+ branches=$( get_stack_branches " bar" )
87+ echo " Found branches: $branches "
88+ local count
89+ count=$( echo " $branches " | grep -v ' ^$' | wc -l | tr -d ' ' )
90+ echo " Branch count: $count "
91+
92+ if [ " $count " -eq 3 ]; then
93+ echo " ✅ get_stack_branches found correct number of branches"
94+ else
95+ fail " get_stack_branches found $count branches, expected 3"
96+ fi
97+
98+ # Check specific branches
99+ if echo " $branches " | grep -q " bar-1" && \
100+ echo " $branches " | grep -q " bar-2" && \
101+ echo " $branches " | grep -q " bar-3" ; then
102+ echo " ✅ get_stack_branches found all expected branches"
103+ else
104+ fail " get_stack_branches missing some expected branches"
105+ fi
106+
107+ # Cleanup test branches
108+ git checkout main 2> /dev/null || git checkout master 2> /dev/null
109+ git branch -D bar-1 bar-2 bar-3 other-1 2> /dev/null || true
110+ }
111+
112+ # Get the absolute path of the script directory
113+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
114+
115+ # Create a temporary test directory
116+ TEST_DIR=$( mktemp -d)
117+ echo " Creating test repository in $TEST_DIR "
118+ cd " $TEST_DIR " || exit 1
119+
120+ # Initialize test repository
121+ git init
122+ git config --local user.email " test@example.com"
123+ git config --local user.name " Test User"
124+
125+ # Create initial commit
126+ touch README.md
127+ git add README.md
128+ git commit -m " Initial commit"
129+
32130echo " Starting git stack tests..."
33131
132+ # Source gitstack.sh to get access to internal functions
133+ source " $SCRIPT_DIR /gitstack.sh"
134+
135+ # Run new tests first
136+ test_get_stack_info
137+ test_get_stack_branches
138+
34139# Optional: Clean up any existing test branches from previous runs
35140git branch -D foo-0 foo-1 foo-2 2> /dev/null || true
36141
37142# 1. Create stack with base name 'foo'
38- git stack create foo
143+ " $SCRIPT_DIR /gitstack.sh " create foo
39144if [ " $( current_branch) " != " foo-0" ]; then
40145 fail " Expected current branch to be 'foo-0' after create, got '$( current_branch) '"
41146fi
42147echo " ✅ Successfully created and checked out 'foo-0'"
43148
44149# 2. Increment -> foo-1
45- git stack increment
150+ " $SCRIPT_DIR /gitstack.sh " increment
46151if [ " $( current_branch) " != " foo-1" ]; then
47152 fail " Expected current branch to be 'foo-1' after increment, got '$( current_branch) '"
48153fi
49154echo " ✅ Successfully incremented to 'foo-1'"
50155
51156# 3. Increment -> foo-2
52- git stack increment
157+ " $SCRIPT_DIR /gitstack.sh " increment
53158if [ " $( current_branch) " != " foo-2" ]; then
54159 fail " Expected current branch to be 'foo-2' after increment, got '$( current_branch) '"
55160fi
56161echo " ✅ Successfully incremented to 'foo-2'"
57162
58- echo " Deleting stack with 'git stack delete -f foo'"
59- git stack delete -f foo
163+ echo " Deleting stack with '$SCRIPT_DIR /gitstack.sh delete -f foo'"
164+ " $SCRIPT_DIR /gitstack.sh " delete -f foo
60165
61166# Verify no foo-* branches remain
62167if git rev-parse --verify foo-0 & > /dev/null || \
@@ -69,7 +174,7 @@ echo "✅ Successfully deleted all foo-* branches"
69174echo
70175echo " 🎉 All tests passed!"
71176
72- # Optional: Clean up test branches
73- # Uncomment if you want to remove these branches automatically:
74- # git checkout main || git checkout master || true
75- # git branch -D foo-0 foo-1 foo-2
177+ # Clean up
178+ echo " Cleaning up test repository... "
179+ cd - > /dev/null || exit 1
180+ rm -rf " $TEST_DIR "
0 commit comments