Skip to content

Commit 0cb3151

Browse files
author
Dag Jomar Mersland
committed
test: add unit tests for stack detection functions
- Add test_get_stack_info to verify stack branch parsing - Add test_get_stack_branches to verify branch listing - Use temporary test repository to avoid conflicts - Fix function sourcing in test environment
1 parent 099df66 commit 0cb3151

2 files changed

Lines changed: 163 additions & 55 deletions

File tree

‎bin/gitstack.sh‎

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
# delete -> Looks at the current branch to determine the stack <base>, prompts to confirm, then force-deletes.
1616
# -----------------------------------------------------------------------------
1717

18-
subcommand="$1"
19-
shift
20-
2118
function usage() {
2219
echo "Git Branch Stack Management"
2320
echo
@@ -64,38 +61,38 @@ function create_branch() {
6461
# Returns the base name and number of the current branch if it's part of a stack
6562
# Usage: if get_stack_info; then echo "Base: $STACK_BASE, Number: $STACK_NUM"; fi
6663
get_stack_info() {
67-
local current_branch
68-
current_branch=$(git rev-parse --abbrev-ref HEAD)
69-
70-
if [[ $current_branch =~ ^([^0-9]+)-([0-9]+)$ ]]; then
71-
STACK_BASE="${BASH_REMATCH[1]}"
72-
STACK_NUM="${BASH_REMATCH[2]}"
73-
return 0 # Success
74-
else
75-
STACK_BASE=""
76-
STACK_NUM=""
77-
return 1 # Not a stack branch
78-
fi
64+
local current_branch
65+
current_branch=$(git rev-parse --abbrev-ref HEAD)
66+
67+
if [[ $current_branch =~ ^([^0-9]+)-([0-9]+)$ ]]; then
68+
STACK_BASE="${BASH_REMATCH[1]}"
69+
STACK_NUM="${BASH_REMATCH[2]}"
70+
return 0 # Success
71+
else
72+
STACK_BASE=""
73+
STACK_NUM=""
74+
return 1 # Not a stack branch
75+
fi
7976
}
8077

8178
# Gets all branches belonging to a stack
8279
# Usage: get_stack_branches "base-name"
8380
get_stack_branches() {
84-
local base_name="$1"
85-
git branch --list "${base_name}-*" | sed 's/^[* ]*//'
81+
local base_name="$1"
82+
git branch --list "${base_name}-*" | sed 's/^[* ]*//'
8683
}
8784

8885
# Modify increment_stack to use the new function
8986
increment_stack() {
90-
if get_stack_info; then
91-
local new_num=$((STACK_NUM + 1))
92-
local new_branch="${STACK_BASE}-${new_num}"
93-
git checkout -b "$new_branch"
94-
echo "Branch '$new_branch' successfully created and checked out."
95-
else
96-
echo "Error: Current branch is not part of a stack (should match '<base>-<number>' pattern)."
97-
exit 1
98-
fi
87+
if get_stack_info; then
88+
local new_num=$((STACK_NUM + 1))
89+
local new_branch="${STACK_BASE}-${new_num}"
90+
git checkout -b "$new_branch"
91+
echo "Branch '$new_branch' successfully created and checked out."
92+
else
93+
echo "Error: Current branch is not part of a stack (should match '<base>-<number>' pattern)."
94+
exit 1
95+
fi
9996
}
10097

10198
# Force-delete all local branches in the stack "<base_name>-*"
@@ -203,23 +200,29 @@ function delete_shorthand() {
203200
esac
204201
}
205202

206-
# Subcommand dispatch
207-
case "$subcommand" in
208-
create)
209-
create_branch "$@"
210-
;;
211-
increment)
212-
increment_stack
213-
;;
214-
delete)
215-
# If no args, do interactive shorthand; otherwise do standard logic
216-
if [ $# -eq 0 ]; then
217-
delete_shorthand
218-
else
219-
delete_stack "$@"
220-
fi
221-
;;
222-
*)
223-
usage
224-
;;
225-
esac
203+
# Only process arguments if script is run directly (not sourced)
204+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
205+
subcommand="$1"
206+
shift
207+
208+
# Subcommand dispatch
209+
case "$subcommand" in
210+
create)
211+
create_branch "$@"
212+
;;
213+
increment)
214+
increment_stack
215+
;;
216+
delete)
217+
# If no args, do interactive shorthand; otherwise do standard logic
218+
if [ $# -eq 0 ]; then
219+
delete_shorthand
220+
else
221+
delete_stack "$@"
222+
fi
223+
;;
224+
*)
225+
usage
226+
;;
227+
esac
228+
fi

‎bin/gitstack_test.sh‎

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
32130
echo "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
35140
git 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
39144
if [ "$(current_branch)" != "foo-0" ]; then
40145
fail "Expected current branch to be 'foo-0' after create, got '$(current_branch)'"
41146
fi
42147
echo "✅ Successfully created and checked out 'foo-0'"
43148

44149
# 2. Increment -> foo-1
45-
git stack increment
150+
"$SCRIPT_DIR/gitstack.sh" increment
46151
if [ "$(current_branch)" != "foo-1" ]; then
47152
fail "Expected current branch to be 'foo-1' after increment, got '$(current_branch)'"
48153
fi
49154
echo "✅ Successfully incremented to 'foo-1'"
50155

51156
# 3. Increment -> foo-2
52-
git stack increment
157+
"$SCRIPT_DIR/gitstack.sh" increment
53158
if [ "$(current_branch)" != "foo-2" ]; then
54159
fail "Expected current branch to be 'foo-2' after increment, got '$(current_branch)'"
55160
fi
56161
echo "✅ 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
62167
if git rev-parse --verify foo-0 &>/dev/null || \
@@ -69,7 +174,7 @@ echo "✅ Successfully deleted all foo-* branches"
69174
echo
70175
echo "🎉 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

Comments
 (0)