|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cli/go-gh/v2/pkg/api" |
| 10 | + "github.com/github/gh-stack/internal/config" |
| 11 | + "github.com/github/gh-stack/internal/git" |
| 12 | + "github.com/github/gh-stack/internal/github" |
| 13 | + "github.com/github/gh-stack/internal/stack" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func writeTwoStacks(t *testing.T, dir string, s1, s2 stack.Stack) { |
| 19 | + t.Helper() |
| 20 | + sf := &stack.StackFile{ |
| 21 | + SchemaVersion: 1, |
| 22 | + Stacks: []stack.Stack{s1, s2}, |
| 23 | + } |
| 24 | + data, err := json.MarshalIndent(sf, "", " ") |
| 25 | + require.NoError(t, err) |
| 26 | + require.NoError(t, os.WriteFile(filepath.Join(dir, "gh-stack"), data, 0644)) |
| 27 | +} |
| 28 | + |
| 29 | +func TestUnstack_RemovesStack(t *testing.T) { |
| 30 | + gitDir := t.TempDir() |
| 31 | + restore := git.SetOps(&git.MockOps{ |
| 32 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 33 | + CurrentBranchFn: func() (string, error) { return "b1", nil }, |
| 34 | + }) |
| 35 | + defer restore() |
| 36 | + |
| 37 | + s1 := stack.Stack{ |
| 38 | + ID: "42", |
| 39 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 40 | + Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, |
| 41 | + } |
| 42 | + s2 := stack.Stack{ |
| 43 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 44 | + Branches: []stack.BranchRef{{Branch: "b3"}, {Branch: "b4"}}, |
| 45 | + } |
| 46 | + writeTwoStacks(t, gitDir, s1, s2) |
| 47 | + |
| 48 | + var deletedStackID string |
| 49 | + cfg, outR, errR := config.NewTestConfig() |
| 50 | + cfg.GitHubClientOverride = &github.MockClient{ |
| 51 | + DeleteStackFn: func(stackID string) error { |
| 52 | + deletedStackID = stackID |
| 53 | + return nil |
| 54 | + }, |
| 55 | + } |
| 56 | + err := runUnstack(cfg, &unstackOptions{}) |
| 57 | + output := collectOutput(cfg, outR, errR) |
| 58 | + |
| 59 | + require.NoError(t, err) |
| 60 | + assert.Contains(t, output, "Stack removed from local tracking") |
| 61 | + assert.Contains(t, output, "Stack deleted on GitHub") |
| 62 | + assert.Equal(t, "42", deletedStackID) |
| 63 | + |
| 64 | + sf, err := stack.Load(gitDir) |
| 65 | + require.NoError(t, err) |
| 66 | + require.Len(t, sf.Stacks, 1) |
| 67 | + assert.Equal(t, []string{"b3", "b4"}, sf.Stacks[0].BranchNames()) |
| 68 | +} |
| 69 | + |
| 70 | +func TestUnstack_Local(t *testing.T) { |
| 71 | + gitDir := t.TempDir() |
| 72 | + restore := git.SetOps(&git.MockOps{ |
| 73 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 74 | + CurrentBranchFn: func() (string, error) { return "b1", nil }, |
| 75 | + }) |
| 76 | + defer restore() |
| 77 | + |
| 78 | + writeStackFile(t, gitDir, stack.Stack{ |
| 79 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 80 | + Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, |
| 81 | + }) |
| 82 | + |
| 83 | + cfg, outR, errR := config.NewTestConfig() |
| 84 | + err := runUnstack(cfg, &unstackOptions{local: true}) |
| 85 | + output := collectOutput(cfg, outR, errR) |
| 86 | + |
| 87 | + require.NoError(t, err) |
| 88 | + assert.Contains(t, output, "Stack removed") |
| 89 | + // With --local, the GitHub API should NOT be called. |
| 90 | + assert.NotContains(t, output, "Stack deleted on GitHub") |
| 91 | + |
| 92 | + sf, err := stack.Load(gitDir) |
| 93 | + require.NoError(t, err) |
| 94 | + assert.Empty(t, sf.Stacks) |
| 95 | +} |
| 96 | + |
| 97 | +func TestUnstack_WithTarget(t *testing.T) { |
| 98 | + gitDir := t.TempDir() |
| 99 | + restore := git.SetOps(&git.MockOps{ |
| 100 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 101 | + CurrentBranchFn: func() (string, error) { return "unrelated", nil }, |
| 102 | + }) |
| 103 | + defer restore() |
| 104 | + |
| 105 | + s1 := stack.Stack{ |
| 106 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 107 | + Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, |
| 108 | + } |
| 109 | + s2 := stack.Stack{ |
| 110 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 111 | + Branches: []stack.BranchRef{{Branch: "b3"}, {Branch: "b4"}}, |
| 112 | + } |
| 113 | + writeTwoStacks(t, gitDir, s1, s2) |
| 114 | + |
| 115 | + cfg, outR, errR := config.NewTestConfig() |
| 116 | + err := runUnstack(cfg, &unstackOptions{target: "b3", local: true}) |
| 117 | + output := collectOutput(cfg, outR, errR) |
| 118 | + |
| 119 | + require.NoError(t, err) |
| 120 | + assert.Contains(t, output, "Stack removed") |
| 121 | + |
| 122 | + sf, err := stack.Load(gitDir) |
| 123 | + require.NoError(t, err) |
| 124 | + require.Len(t, sf.Stacks, 1) |
| 125 | + assert.Equal(t, []string{"b1", "b2"}, sf.Stacks[0].BranchNames()) |
| 126 | +} |
| 127 | + |
| 128 | +func TestUnstack_NoStackID_WarnsAndSkipsAPI(t *testing.T) { |
| 129 | + gitDir := t.TempDir() |
| 130 | + restore := git.SetOps(&git.MockOps{ |
| 131 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 132 | + CurrentBranchFn: func() (string, error) { return "b1", nil }, |
| 133 | + }) |
| 134 | + defer restore() |
| 135 | + |
| 136 | + // Stack with no ID (never synced to GitHub) |
| 137 | + writeStackFile(t, gitDir, stack.Stack{ |
| 138 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 139 | + Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, |
| 140 | + }) |
| 141 | + |
| 142 | + apiCalled := false |
| 143 | + cfg, outR, errR := config.NewTestConfig() |
| 144 | + cfg.GitHubClientOverride = &github.MockClient{ |
| 145 | + DeleteStackFn: func(stackID string) error { |
| 146 | + apiCalled = true |
| 147 | + return nil |
| 148 | + }, |
| 149 | + } |
| 150 | + err := runUnstack(cfg, &unstackOptions{}) |
| 151 | + output := collectOutput(cfg, outR, errR) |
| 152 | + |
| 153 | + require.NoError(t, err) |
| 154 | + assert.False(t, apiCalled, "API should not be called when stack has no ID") |
| 155 | + assert.Contains(t, output, "no remote ID") |
| 156 | + assert.Contains(t, output, "Stack removed from local tracking") |
| 157 | + assert.NotContains(t, output, "Stack deleted on GitHub") |
| 158 | +} |
| 159 | + |
| 160 | +func TestUnstack_API404_ShowsErrorAndStopsLocalDeletion(t *testing.T) { |
| 161 | + gitDir := t.TempDir() |
| 162 | + restore := git.SetOps(&git.MockOps{ |
| 163 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 164 | + CurrentBranchFn: func() (string, error) { return "b1", nil }, |
| 165 | + }) |
| 166 | + defer restore() |
| 167 | + |
| 168 | + writeStackFile(t, gitDir, stack.Stack{ |
| 169 | + ID: "99", |
| 170 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 171 | + Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, |
| 172 | + }) |
| 173 | + |
| 174 | + cfg, outR, errR := config.NewTestConfig() |
| 175 | + cfg.GitHubClientOverride = &github.MockClient{ |
| 176 | + DeleteStackFn: func(stackID string) error { |
| 177 | + return &api.HTTPError{StatusCode: 404, Message: "Not Found"} |
| 178 | + }, |
| 179 | + } |
| 180 | + err := runUnstack(cfg, &unstackOptions{}) |
| 181 | + output := collectOutput(cfg, outR, errR) |
| 182 | + |
| 183 | + assert.ErrorIs(t, err, ErrAPIFailure) |
| 184 | + assert.Contains(t, output, "Failed to delete stack on GitHub (HTTP 404)") |
| 185 | + // Should NOT remove locally when remote fails |
| 186 | + assert.NotContains(t, output, "Stack removed from local tracking") |
| 187 | + |
| 188 | + // Stack should still exist locally |
| 189 | + sf, err := stack.Load(gitDir) |
| 190 | + require.NoError(t, err) |
| 191 | + require.Len(t, sf.Stacks, 1) |
| 192 | +} |
| 193 | + |
| 194 | +func TestUnstack_API409_ShowsErrorAndStopsLocalDeletion(t *testing.T) { |
| 195 | + gitDir := t.TempDir() |
| 196 | + restore := git.SetOps(&git.MockOps{ |
| 197 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 198 | + CurrentBranchFn: func() (string, error) { return "b1", nil }, |
| 199 | + }) |
| 200 | + defer restore() |
| 201 | + |
| 202 | + writeStackFile(t, gitDir, stack.Stack{ |
| 203 | + ID: "99", |
| 204 | + Trunk: stack.BranchRef{Branch: "main"}, |
| 205 | + Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}}, |
| 206 | + }) |
| 207 | + |
| 208 | + cfg, outR, errR := config.NewTestConfig() |
| 209 | + cfg.GitHubClientOverride = &github.MockClient{ |
| 210 | + DeleteStackFn: func(stackID string) error { |
| 211 | + return &api.HTTPError{StatusCode: 409, Message: "Stack is currently being modified"} |
| 212 | + }, |
| 213 | + } |
| 214 | + err := runUnstack(cfg, &unstackOptions{}) |
| 215 | + output := collectOutput(cfg, outR, errR) |
| 216 | + |
| 217 | + assert.ErrorIs(t, err, ErrAPIFailure) |
| 218 | + assert.Contains(t, output, "Failed to delete stack on GitHub (HTTP 409)") |
| 219 | + // Should NOT remove locally when remote fails |
| 220 | + assert.NotContains(t, output, "Stack removed from local tracking") |
| 221 | + |
| 222 | + // Stack should still exist locally |
| 223 | + sf, err := stack.Load(gitDir) |
| 224 | + require.NoError(t, err) |
| 225 | + require.Len(t, sf.Stacks, 1) |
| 226 | +} |
0 commit comments