|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/github/gh-stack/internal/config" |
| 8 | + "github.com/github/gh-stack/internal/git" |
| 9 | + "github.com/github/gh-stack/internal/stack" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSwitch_SwitchesToSelectedBranch(t *testing.T) { |
| 15 | + gitDir := t.TempDir() |
| 16 | + var checkedOut string |
| 17 | + |
| 18 | + restore := git.SetOps(&git.MockOps{ |
| 19 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 20 | + CurrentBranchFn: func() (string, error) { return "b1", nil }, |
| 21 | + CheckoutBranchFn: func(name string) error { |
| 22 | + checkedOut = name |
| 23 | + return nil |
| 24 | + }, |
| 25 | + }) |
| 26 | + defer restore() |
| 27 | + |
| 28 | + writeStackFile(t, gitDir, stack.Stack{ |
| 29 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 30 | + Branches: []stack.BranchRef{ |
| 31 | + {Branch: "b1"}, |
| 32 | + {Branch: "b2"}, |
| 33 | + {Branch: "b3"}, |
| 34 | + }, |
| 35 | + }) |
| 36 | + |
| 37 | + cfg, outR, errR := config.NewTestConfig() |
| 38 | + cfg.ForceInteractive = true |
| 39 | + |
| 40 | + // Simulate selecting the first option (index 0) which is "3. b3" (top of stack) |
| 41 | + cfg.SelectFn = func(prompt, def string, options []string) (int, error) { |
| 42 | + // Verify prompt text |
| 43 | + assert.Equal(t, "Select a branch in the stack to switch to:", prompt) |
| 44 | + // Verify options are in reverse order with numbering |
| 45 | + assert.Equal(t, []string{"3. b3", "2. b2", "1. b1"}, options) |
| 46 | + return 0, nil // select "3. b3" |
| 47 | + } |
| 48 | + |
| 49 | + err := runSwitch(cfg) |
| 50 | + output := collectOutput(cfg, outR, errR) |
| 51 | + |
| 52 | + require.NoError(t, err) |
| 53 | + assert.Equal(t, "b3", checkedOut) |
| 54 | + assert.Contains(t, output, "Switched to b3") |
| 55 | +} |
| 56 | + |
| 57 | +func TestSwitch_SelectMiddleBranch(t *testing.T) { |
| 58 | + gitDir := t.TempDir() |
| 59 | + var checkedOut string |
| 60 | + |
| 61 | + restore := git.SetOps(&git.MockOps{ |
| 62 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 63 | + CurrentBranchFn: func() (string, error) { return "b1", nil }, |
| 64 | + CheckoutBranchFn: func(name string) error { |
| 65 | + checkedOut = name |
| 66 | + return nil |
| 67 | + }, |
| 68 | + }) |
| 69 | + defer restore() |
| 70 | + |
| 71 | + writeStackFile(t, gitDir, stack.Stack{ |
| 72 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 73 | + Branches: []stack.BranchRef{ |
| 74 | + {Branch: "b1"}, |
| 75 | + {Branch: "b2"}, |
| 76 | + {Branch: "b3"}, |
| 77 | + }, |
| 78 | + }) |
| 79 | + |
| 80 | + cfg, outR, errR := config.NewTestConfig() |
| 81 | + cfg.ForceInteractive = true |
| 82 | + |
| 83 | + // Select index 1 which is "2. b2" |
| 84 | + cfg.SelectFn = func(prompt, def string, options []string) (int, error) { |
| 85 | + return 1, nil // select "2. b2" |
| 86 | + } |
| 87 | + |
| 88 | + err := runSwitch(cfg) |
| 89 | + output := collectOutput(cfg, outR, errR) |
| 90 | + |
| 91 | + require.NoError(t, err) |
| 92 | + assert.Equal(t, "b2", checkedOut) |
| 93 | + assert.Contains(t, output, "Switched to b2") |
| 94 | +} |
| 95 | + |
| 96 | +func TestSwitch_AlreadyOnSelectedBranch(t *testing.T) { |
| 97 | + gitDir := t.TempDir() |
| 98 | + checkoutCalled := false |
| 99 | + |
| 100 | + restore := git.SetOps(&git.MockOps{ |
| 101 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 102 | + CurrentBranchFn: func() (string, error) { return "b2", nil }, |
| 103 | + CheckoutBranchFn: func(name string) error { |
| 104 | + checkoutCalled = true |
| 105 | + return nil |
| 106 | + }, |
| 107 | + }) |
| 108 | + defer restore() |
| 109 | + |
| 110 | + writeStackFile(t, gitDir, stack.Stack{ |
| 111 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 112 | + Branches: []stack.BranchRef{ |
| 113 | + {Branch: "b1"}, |
| 114 | + {Branch: "b2"}, |
| 115 | + {Branch: "b3"}, |
| 116 | + }, |
| 117 | + }) |
| 118 | + |
| 119 | + cfg, outR, errR := config.NewTestConfig() |
| 120 | + cfg.ForceInteractive = true |
| 121 | + |
| 122 | + // Select "2. b2" which is option index 1 — the branch we're already on |
| 123 | + cfg.SelectFn = func(prompt, def string, options []string) (int, error) { |
| 124 | + return 1, nil // select "2. b2" |
| 125 | + } |
| 126 | + |
| 127 | + err := runSwitch(cfg) |
| 128 | + output := collectOutput(cfg, outR, errR) |
| 129 | + |
| 130 | + require.NoError(t, err) |
| 131 | + assert.False(t, checkoutCalled, "CheckoutBranch should not be called when already on target") |
| 132 | + assert.Contains(t, output, "Already on b2") |
| 133 | +} |
| 134 | + |
| 135 | +func TestSwitch_NotInStack(t *testing.T) { |
| 136 | + gitDir := t.TempDir() |
| 137 | + restore := git.SetOps(&git.MockOps{ |
| 138 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 139 | + CurrentBranchFn: func() (string, error) { return "orphan", nil }, |
| 140 | + }) |
| 141 | + defer restore() |
| 142 | + |
| 143 | + // Write a stack that doesn't contain "orphan" |
| 144 | + writeStackFile(t, gitDir, stack.Stack{ |
| 145 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 146 | + Branches: []stack.BranchRef{{Branch: "b1"}}, |
| 147 | + }) |
| 148 | + |
| 149 | + cfg, _, _ := config.NewTestConfig() |
| 150 | + cfg.ForceInteractive = true |
| 151 | + |
| 152 | + err := runSwitch(cfg) |
| 153 | + assert.ErrorIs(t, err, ErrNotInStack) |
| 154 | +} |
| 155 | + |
| 156 | +func TestSwitch_NonInteractive(t *testing.T) { |
| 157 | + gitDir := t.TempDir() |
| 158 | + restore := git.SetOps(&git.MockOps{ |
| 159 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 160 | + CurrentBranchFn: func() (string, error) { return "b1", nil }, |
| 161 | + }) |
| 162 | + defer restore() |
| 163 | + |
| 164 | + writeStackFile(t, gitDir, stack.Stack{ |
| 165 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 166 | + Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, |
| 167 | + }) |
| 168 | + |
| 169 | + cfg, outR, errR := config.NewTestConfig() |
| 170 | + // ForceInteractive not set — non-interactive mode |
| 171 | + |
| 172 | + err := runSwitch(cfg) |
| 173 | + output := collectOutput(cfg, outR, errR) |
| 174 | + |
| 175 | + assert.ErrorIs(t, err, ErrSilent) |
| 176 | + assert.Contains(t, output, "switch requires an interactive terminal") |
| 177 | +} |
| 178 | + |
| 179 | +func TestSwitch_DisplayOrder(t *testing.T) { |
| 180 | + gitDir := t.TempDir() |
| 181 | + restore := git.SetOps(&git.MockOps{ |
| 182 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 183 | + CurrentBranchFn: func() (string, error) { return "first", nil }, |
| 184 | + CheckoutBranchFn: func(name string) error { |
| 185 | + return nil |
| 186 | + }, |
| 187 | + }) |
| 188 | + defer restore() |
| 189 | + |
| 190 | + writeStackFile(t, gitDir, stack.Stack{ |
| 191 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 192 | + Branches: []stack.BranchRef{ |
| 193 | + {Branch: "first"}, |
| 194 | + {Branch: "second"}, |
| 195 | + {Branch: "third"}, |
| 196 | + {Branch: "fourth"}, |
| 197 | + {Branch: "fifth"}, |
| 198 | + }, |
| 199 | + }) |
| 200 | + |
| 201 | + cfg, _, _ := config.NewTestConfig() |
| 202 | + cfg.ForceInteractive = true |
| 203 | + |
| 204 | + var capturedOptions []string |
| 205 | + cfg.SelectFn = func(prompt, def string, options []string) (int, error) { |
| 206 | + capturedOptions = options |
| 207 | + return 0, nil // select top |
| 208 | + } |
| 209 | + |
| 210 | + err := runSwitch(cfg) |
| 211 | + require.NoError(t, err) |
| 212 | + |
| 213 | + expected := []string{ |
| 214 | + "5. fifth", |
| 215 | + "4. fourth", |
| 216 | + "3. third", |
| 217 | + "2. second", |
| 218 | + "1. first", |
| 219 | + } |
| 220 | + assert.Equal(t, expected, capturedOptions) |
| 221 | +} |
| 222 | + |
| 223 | +func TestSwitch_NoBranches(t *testing.T) { |
| 224 | + gitDir := t.TempDir() |
| 225 | + restore := git.SetOps(&git.MockOps{ |
| 226 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 227 | + CurrentBranchFn: func() (string, error) { return "main", nil }, |
| 228 | + }) |
| 229 | + defer restore() |
| 230 | + |
| 231 | + writeStackFile(t, gitDir, stack.Stack{ |
| 232 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 233 | + Branches: []stack.BranchRef{}, |
| 234 | + }) |
| 235 | + |
| 236 | + cfg, _, _ := config.NewTestConfig() |
| 237 | + cfg.ForceInteractive = true |
| 238 | + |
| 239 | + err := runSwitch(cfg) |
| 240 | + assert.ErrorIs(t, err, ErrNotInStack) |
| 241 | +} |
| 242 | + |
| 243 | +func TestSwitch_CmdIntegration(t *testing.T) { |
| 244 | + gitDir := t.TempDir() |
| 245 | + restore := git.SetOps(&git.MockOps{ |
| 246 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 247 | + CurrentBranchFn: func() (string, error) { return "b1", nil }, |
| 248 | + CheckoutBranchFn: func(name string) error { |
| 249 | + return nil |
| 250 | + }, |
| 251 | + }) |
| 252 | + defer restore() |
| 253 | + |
| 254 | + writeStackFile(t, gitDir, stack.Stack{ |
| 255 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 256 | + Branches: []stack.BranchRef{ |
| 257 | + {Branch: "b1"}, |
| 258 | + {Branch: "b2"}, |
| 259 | + }, |
| 260 | + }) |
| 261 | + |
| 262 | + cfg, _, _ := config.NewTestConfig() |
| 263 | + cfg.ForceInteractive = true |
| 264 | + cfg.SelectFn = func(prompt, def string, options []string) (int, error) { |
| 265 | + return 0, nil // select top |
| 266 | + } |
| 267 | + |
| 268 | + cmd := SwitchCmd(cfg) |
| 269 | + cmd.SetOut(io.Discard) |
| 270 | + cmd.SetErr(io.Discard) |
| 271 | + err := cmd.Execute() |
| 272 | + assert.NoError(t, err) |
| 273 | +} |
0 commit comments