@@ -44,8 +44,13 @@ function test_get_stack_info() {
4444 echo " Testing get_stack_info..."
4545
4646 # Create and checkout a test branch
47- git checkout -b test-123 2> /dev/null
48- echo " Created test-123 branch"
47+ if ! git checkout -b test-123; then
48+ echo " Failed to create test-123 branch. Trying to checkout existing branch..."
49+ if ! git checkout test-123; then
50+ fail " Could not create or checkout test-123 branch"
51+ fi
52+ fi
53+ echo " Created/checked out test-123 branch"
4954
5055 if get_stack_info; then
5156 echo " Stack info: BASE=$STACK_BASE NUM=$STACK_NUM "
@@ -59,8 +64,14 @@ function test_get_stack_info() {
5964 fi
6065
6166 # Test with non-stack branch
62- git checkout -b not-a-stack-branch 2> /dev/null
63- echo " Created not-a-stack-branch"
67+ if ! git checkout -b not-a-stack-branch; then
68+ echo " Failed to create not-a-stack-branch. Trying to checkout existing branch..."
69+ if ! git checkout not-a-stack-branch; then
70+ fail " Could not create or checkout not-a-stack-branch"
71+ fi
72+ fi
73+ echo " Created/checked out not-a-stack-branch"
74+
6475 if get_stack_info; then
6576 fail " get_stack_info incorrectly identified not-a-stack-branch as a stack branch"
6677 else
@@ -77,11 +88,15 @@ function test_get_stack_branches() {
7788 echo " Testing get_stack_branches..."
7889
7990 # Create test branches
80- git checkout -b bar-1 2> /dev/null
81- git checkout -b bar-2 2> /dev/null
82- git checkout -b bar-3 2> /dev/null
83- git checkout -b other-1 2> /dev/null
84- echo " Created test branches"
91+ for branch in bar-1 bar-2 bar-3 other-1; do
92+ if ! git checkout -b " $branch " ; then
93+ echo " Failed to create $branch . Trying to checkout existing branch..."
94+ if ! git checkout " $branch " ; then
95+ fail " Could not create or checkout $branch "
96+ fi
97+ fi
98+ done
99+ echo " Created/checked out test branches"
85100
86101 # Get branches and check count
87102 local branches
@@ -116,12 +131,15 @@ function test_list_stacks() {
116131 echo " Testing list_stacks..."
117132
118133 # Create some test stacks
119- git checkout -b feature-0 2> /dev/null
120- git checkout -b feature-1 2> /dev/null
121- git checkout -b bugfix-0 2> /dev/null
122- git checkout -b bugfix-1 2> /dev/null
123- git checkout -b other-branch 2> /dev/null
124- echo " Created test branches"
134+ for branch in feature-0 feature-1 bugfix-0 bugfix-1 other-branch; do
135+ if ! git checkout -b " $branch " ; then
136+ echo " Failed to create $branch . Trying to checkout existing branch..."
137+ if ! git checkout " $branch " ; then
138+ fail " Could not create or checkout $branch "
139+ fi
140+ fi
141+ done
142+ echo " Created/checked out test branches"
125143
126144 # Get all stack bases
127145 local stack_bases
@@ -505,6 +523,112 @@ function test_push_command() {
505523 rm -f test1.txt test2.txt test3.txt
506524}
507525
526+ # Test MR creation functionality
527+ function test_mr_command() {
528+ echo " Testing MR command..."
529+
530+ # Create a test stack
531+ git checkout main 2> /dev/null || git checkout master 2> /dev/null
532+ " $SCRIPT_DIR /gitstack.sh" create mr-test
533+ echo " test1" > test1.txt
534+ git add test1.txt
535+ git commit -m " test1"
536+
537+ " $SCRIPT_DIR /gitstack.sh" increment
538+ echo " test2" > test2.txt
539+ git add test2.txt
540+ git commit -m " test2"
541+
542+ " $SCRIPT_DIR /gitstack.sh" increment
543+ echo " test3" > test3.txt
544+ git add test3.txt
545+ git commit -m " test3"
546+
547+ # Create a temporary mock script
548+ local mock_script=" /tmp/mock_glab_$$ .sh"
549+ echo ' #!/bin/bash
550+ # Print all arguments for debugging
551+ echo "[MOCK GLAB] args: $@" >&2
552+ if [ "$1" = "mr" ] && [ "$2" = "create" ]; then
553+ from_branch=$(git rev-parse --abbrev-ref HEAD)
554+ to_branch=""
555+ while [[ $# -gt 0 ]]; do
556+ if [[ "$1" == "-b" ]]; then
557+ shift
558+ to_branch="$1"
559+ break
560+ fi
561+ shift
562+ done
563+ echo "Mock: Creating MR from $from_branch to $to_branch"
564+ exit 0
565+ fi
566+ exit 1' > " $mock_script "
567+ chmod +x " $mock_script "
568+
569+ # Temporarily modify PATH to use our mock
570+ local original_path=" $PATH "
571+ export PATH=" /tmp:$PATH "
572+ mv " $mock_script " " /tmp/glab"
573+
574+ # Test MR creation from middle branch
575+ git checkout mr-test-1
576+ local output
577+ output=$( echo y | " $SCRIPT_DIR /gitstack.sh" mr 2>&1 )
578+ echo " $output "
579+ if echo " $output " | grep -q " Mock: Creating MR from mr-test-1 to mr-test-0" ; then
580+ echo " ✅ MR command correctly targets previous branch"
581+ else
582+ fail " MR command failed to target correct branch"
583+ fi
584+
585+ # Test MR creation from first branch
586+ git checkout mr-test-0
587+ output=$( echo y | " $SCRIPT_DIR /gitstack.sh" mr 2>&1 )
588+ echo " $output "
589+ if echo " $output " | grep -q " Mock: Creating MR from mr-test-0 to main" ; then
590+ echo " ✅ MR command correctly targets main for first branch"
591+ else
592+ fail " MR command failed to target main for first branch"
593+ fi
594+
595+ # Test with additional arguments
596+ output=$( echo y | " $SCRIPT_DIR /gitstack.sh" mr --draft --reviewer @user 2>&1 )
597+ echo " $output "
598+ if echo " $output " | grep -q " Mock: Creating MR from mr-test-0 to main" ; then
599+ echo " ✅ MR command correctly passes additional arguments"
600+ else
601+ fail " MR command failed to pass additional arguments"
602+ fi
603+
604+ # Test help flag (no prompt expected)
605+ output=$( " $SCRIPT_DIR /gitstack.sh" mr --help 2>&1 )
606+ echo " $output "
607+ if echo " $output " | grep -q " git stack mr - Create GitLab MR" ; then
608+ echo " ✅ MR command shows help text"
609+ else
610+ fail " MR command failed to show help text"
611+ fi
612+
613+ # Test error when not on stack branch (no prompt expected)
614+ git checkout -b not-a-stack-branch
615+ output=$( " $SCRIPT_DIR /gitstack.sh" mr 2>&1 || true)
616+ echo " $output "
617+ if echo " $output " | grep -q " Error: Current branch is not part of a stack" ; then
618+ echo " ✅ MR command correctly errors on non-stack branch"
619+ else
620+ fail " MR command failed to error on non-stack branch"
621+ fi
622+
623+ # Clean up
624+ git checkout main 2> /dev/null || git checkout master 2> /dev/null
625+ " $SCRIPT_DIR /gitstack.sh" delete -f mr-test
626+ git branch -D not-a-stack-branch 2> /dev/null || true
627+ rm -f test1.txt test2.txt test3.txt
628+ rm -f " /tmp/glab"
629+ export PATH=" $original_path "
630+ }
631+
508632# Run all tests
509633function run_all_tests() {
510634 source_gitstack
@@ -517,6 +641,7 @@ function run_all_tests() {
517641 test_stack_navigation
518642 test_convert_to_stack
519643 test_push_command
644+ test_mr_command
520645}
521646
522647# Create a temporary test directory
@@ -544,45 +669,10 @@ echo "Starting git stack tests..."
544669# Run all tests
545670run_all_tests
546671
547- # Optional: Clean up any existing test branches from previous runs
548- git branch -D foo-0 foo-1 foo-2 2> /dev/null || true
549-
550- # 1. Create stack with base name 'foo'
551- " $SCRIPT_DIR /gitstack.sh" create foo
552- if [ " $( current_branch) " != " foo-0" ]; then
553- fail " Expected current branch to be 'foo-0' after create, got '$( current_branch) '"
554- fi
555- echo " ✅ Successfully created and checked out 'foo-0'"
556-
557- # 2. Increment -> foo-1
558- " $SCRIPT_DIR /gitstack.sh" increment
559- if [ " $( current_branch) " != " foo-1" ]; then
560- fail " Expected current branch to be 'foo-1' after increment, got '$( current_branch) '"
561- fi
562- echo " ✅ Successfully incremented to 'foo-1'"
563-
564- # 3. Increment -> foo-2
565- " $SCRIPT_DIR /gitstack.sh" increment
566- if [ " $( current_branch) " != " foo-2" ]; then
567- fail " Expected current branch to be 'foo-2' after increment, got '$( current_branch) '"
568- fi
569- echo " ✅ Successfully incremented to 'foo-2'"
570-
571- echo " Deleting stack with '$SCRIPT_DIR /gitstack.sh delete -f foo'"
572- " $SCRIPT_DIR /gitstack.sh" delete -f foo
573-
574- # Verify no foo-* branches remain
575- if git rev-parse --verify foo-0 & > /dev/null || \
576- git rev-parse --verify foo-1 & > /dev/null || \
577- git rev-parse --verify foo-2 & > /dev/null; then
578- fail " Expected no foo-* branches to exist after delete -f foo"
579- fi
580- echo " ✅ Successfully deleted all foo-* branches"
581-
582672echo
583673echo " 🎉 All tests passed!"
584674
585675# Clean up
586676echo " Cleaning up test repository..."
587- cd - > /dev/null || exit 1
677+ cd - > /dev/null || true
588678rm -rf " $TEST_DIR "
0 commit comments