|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// mergedWorktreeRunner returns a mockRunner that supports MergedBranches and ListWorktrees. |
| 10 | +func mergedWorktreeRunner(mergedOut, worktreeOut string) *mockRunner { |
| 11 | + return &mockRunner{ |
| 12 | + run: func(args ...string) (string, error) { |
| 13 | + if len(args) >= 1 && args[0] == cmdBranch { |
| 14 | + return mergedOut, nil |
| 15 | + } |
| 16 | + if len(args) >= 1 && args[0] == cmdWorktreeTest { |
| 17 | + return worktreeOut, nil |
| 18 | + } |
| 19 | + return "", nil |
| 20 | + }, |
| 21 | + runInDir: noopRunInDir, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestFindMergedCandidatesFound(t *testing.T) { |
| 26 | + worktreeOut := strings.Join([]string{ |
| 27 | + "worktree /repo", |
| 28 | + "HEAD abc123", |
| 29 | + "branch refs/heads/main", |
| 30 | + "", |
| 31 | + "worktree " + pathWtDone, |
| 32 | + "HEAD def456", |
| 33 | + "branch refs/heads/" + branchDone, |
| 34 | + "", |
| 35 | + "worktree /wt/feature-active", |
| 36 | + "HEAD ghi789", |
| 37 | + "branch refs/heads/feature/active", |
| 38 | + "", |
| 39 | + }, "\n") |
| 40 | + |
| 41 | + r := mergedWorktreeRunner(" "+branchDone+"\n bugfix/old", worktreeOut) |
| 42 | + candidates, err := findMergedCandidates(r, branchMain) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("findMergedCandidates: %v", err) |
| 45 | + } |
| 46 | + if len(candidates) != 1 { |
| 47 | + t.Fatalf("got %d candidates, want 1", len(candidates)) |
| 48 | + } |
| 49 | + if candidates[0].branch != branchDone { |
| 50 | + t.Errorf("branch = %q, want %q", candidates[0].branch, branchDone) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestFindMergedCandidatesNone(t *testing.T) { |
| 55 | + r := mergedWorktreeRunner("", "worktree /repo\nHEAD abc\nbranch refs/heads/main\n") |
| 56 | + candidates, err := findMergedCandidates(r, branchMain) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("findMergedCandidates: %v", err) |
| 59 | + } |
| 60 | + if len(candidates) != 0 { |
| 61 | + t.Errorf("expected 0 candidates, got %d", len(candidates)) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestFindMergedCandidatesError(t *testing.T) { |
| 66 | + r := &mockRunner{ |
| 67 | + run: func(args ...string) (string, error) { |
| 68 | + if len(args) >= 1 && args[0] == cmdBranch { |
| 69 | + return "", errGitFailed |
| 70 | + } |
| 71 | + return "", nil |
| 72 | + }, |
| 73 | + runInDir: noopRunInDir, |
| 74 | + } |
| 75 | + |
| 76 | + _, err := findMergedCandidates(r, branchMain) |
| 77 | + if err == nil { |
| 78 | + t.Fatal(errExpected) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestPrintMergedCandidates(t *testing.T) { |
| 83 | + cmd, buf := newTestCmd() |
| 84 | + candidates := []mergedCandidate{ |
| 85 | + {path: pathWtDone, branch: branchDone}, |
| 86 | + {path: "/wt/bugfix-old", branch: "bugfix/old"}, |
| 87 | + } |
| 88 | + printMergedCandidates(cmd, candidates) |
| 89 | + |
| 90 | + out := buf.String() |
| 91 | + if !strings.Contains(out, "Merged worktrees:") { |
| 92 | + t.Errorf("output missing header: %q", out) |
| 93 | + } |
| 94 | + if !strings.Contains(out, "done") { |
| 95 | + t.Errorf("output missing task 'done': %q", out) |
| 96 | + } |
| 97 | + if !strings.Contains(out, "old") { |
| 98 | + t.Errorf("output missing task 'old': %q", out) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func testMergedCandidate() []mergedCandidate { |
| 103 | + return []mergedCandidate{{path: pathWtDone, branch: branchDone}} |
| 104 | +} |
| 105 | + |
| 106 | +func TestRemoveMergedWorktreesAllSucceed(t *testing.T) { |
| 107 | + cmd, _ := newTestCmd() |
| 108 | + r := &mockRunner{ |
| 109 | + run: func(_ ...string) (string, error) { return "", nil }, |
| 110 | + runInDir: noopRunInDir, |
| 111 | + } |
| 112 | + |
| 113 | + removed := removeMergedWorktrees(cmd, r, testMergedCandidate()) |
| 114 | + if removed != 1 { |
| 115 | + t.Errorf("removed = %d, want 1", removed) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestRemoveMergedWorktreesRemoveFails(t *testing.T) { |
| 120 | + cmd, buf := newTestCmd() |
| 121 | + r := &mockRunner{ |
| 122 | + run: func(args ...string) (string, error) { |
| 123 | + if len(args) >= 2 && args[0] == cmdWorktreeTest && args[1] == "remove" { |
| 124 | + return "", errors.New("locked") |
| 125 | + } |
| 126 | + return "", nil |
| 127 | + }, |
| 128 | + runInDir: noopRunInDir, |
| 129 | + } |
| 130 | + |
| 131 | + removed := removeMergedWorktrees(cmd, r, testMergedCandidate()) |
| 132 | + if removed != 0 { |
| 133 | + t.Errorf("removed = %d, want 0", removed) |
| 134 | + } |
| 135 | + if !strings.Contains(buf.String(), "Failed to remove") { |
| 136 | + t.Errorf("output = %q, want failure message", buf.String()) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestRemoveMergedWorktreesDeleteBranchFails(t *testing.T) { |
| 141 | + cmd, buf := newTestCmd() |
| 142 | + r := &mockRunner{ |
| 143 | + run: func(args ...string) (string, error) { |
| 144 | + if len(args) >= 1 && args[0] == cmdBranch { |
| 145 | + return "", errors.New("branch not found") |
| 146 | + } |
| 147 | + return "", nil |
| 148 | + }, |
| 149 | + runInDir: noopRunInDir, |
| 150 | + } |
| 151 | + |
| 152 | + removed := removeMergedWorktrees(cmd, r, testMergedCandidate()) |
| 153 | + if removed != 0 { |
| 154 | + t.Errorf("removed = %d, want 0 (branch delete failed)", removed) |
| 155 | + } |
| 156 | + if !strings.Contains(buf.String(), "failed to delete branch") { |
| 157 | + t.Errorf("output = %q, want branch delete failure message", buf.String()) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestConfirmRemoval(t *testing.T) { |
| 162 | + tests := []struct { |
| 163 | + name string |
| 164 | + input string |
| 165 | + want bool |
| 166 | + }{ |
| 167 | + {"yes_short", "y\n", true}, |
| 168 | + {"yes_full", "yes\n", true}, |
| 169 | + {"no", "n\n", false}, |
| 170 | + {"empty", "\n", false}, |
| 171 | + } |
| 172 | + |
| 173 | + for _, tt := range tests { |
| 174 | + t.Run(tt.name, func(t *testing.T) { |
| 175 | + cmd, _ := newTestCmd() |
| 176 | + cmd.SetIn(strings.NewReader(tt.input)) |
| 177 | + got := confirmRemoval(cmd, 2) |
| 178 | + if got != tt.want { |
| 179 | + t.Errorf("confirmRemoval(%q) = %v, want %v", tt.input, got, tt.want) |
| 180 | + } |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
0 commit comments