Files
gitea-tea/internal/git/clean_test.go
T
bircni 4895aa089d feat(pr): merge with the repository's defaults, ready, gh flags and ✓ lines for pr mutations
pr merge takes the method from the repository's default merge style
instead of always merging (D22), accepts gh's --merge, --squash and
--rebase next to -s/--method, refuses methods the repository disables,
and adds -d/--delete-branch (defaulting to the repository setting),
--auto, -t, -b, -F and -e. On a terminal it asks for the method and
branch deletion with the defaults preselected, without a picker when
the current branch has a pull request. It reports "✓ Squashed and
merged pull request alice/x#3 (title)", the deleted remote branch, and
switches off and deletes the local head branch; --json returns the
merged pull request. Merged, closed and draft pull requests are
reported before anything is asked. -s without a value points at
--squash.

pr checkout names the local branch after the head branch when that
name is free or already at the head, falling back to OWNER/BRANCH for
forks and pr/NUMBER (D13), fetches and switches quietly, reports
"✓ Checked out #1 into feat/one", and has the alias co. pr clean
reports what it deleted and that a remote branch was already gone.

pr create asks for the title and body, then "What's next?": submit,
submit as a draft, add metadata or change the branches, without the
AGit question up front or a final confirmation. It pushes an unpushed
head with --set-upstream --quiet ("✓ Pushed feat/one to origin"),
prints "✓ Created pull request alice/x#9 (title)" and the URL, and
adds --fill, --web, the alias new, the gh shorthands, and milestones
by title.

New tea pr ready [--undo]. pr close and reopen take -c/--comment,
close takes -d/--delete-branch, and both say when the pull request
already is in that state. pr edit reports "✓ Edited", takes the
singular gh reviewer flags (the plurals still work), milestones by
title and --remove-milestone. pr diff is colored and paged on a
terminal and byte-exact when piped, with --name-only, --color and
--web. Every pull request argument accepts #N, URLs and branch names
and defaults to the current branch's pull request.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-11 23:55:11 +02:00

140 lines
5.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
type cleanRunner struct {
calls []string
dirty, missingLocal, missingDefault, onFeature bool
branches string
remoteTracking map[string]string
}
func (r *cleanRunner) Output(_ context.Context, dir string, prompt bool, args ...string) (string, error) {
if dir != "/work" {
return "", &CommandError{ExitCode: 1}
}
command := strings.Join(args, " ")
r.calls = append(r.calls, command)
switch args[0] {
case "rev-parse":
if len(args) > 1 && args[1] == "--is-inside-work-tree" {
return "true", nil
}
if len(args) > 2 && strings.HasPrefix(args[2], "refs/heads/") {
if r.missingLocal && strings.Contains(args[2], "feature") {
return "", &CommandError{ExitCode: 1}
}
if r.missingDefault && strings.Contains(args[2], "main") {
return "", &CommandError{ExitCode: 1}
}
return "abc123", nil
}
if len(args) > 2 && strings.HasPrefix(args[2], "refs/remotes/") {
if sha, ok := r.remoteTracking[args[2]]; ok {
return sha, nil
}
return "", &CommandError{ExitCode: 1}
}
case "status":
if r.dirty {
return " M file", nil
}
case "symbolic-ref":
if r.onFeature {
return "feature", nil
}
return "main", nil
case "for-each-ref":
return r.branches, nil
case "check-ref-format", "switch", "branch", "push":
return "", nil
}
return "", nil
}
func TestFindCleanBranchBySHAAndPreferredName(t *testing.T) {
runner := &cleanRunner{branches: "abc123\tpr/7\torigin/feature\ndef456\tother\t\n"}
branch, remote, err := FindCleanBranch(context.Background(), runner, FindCleanBranchOptions{
Directory: "/work", HeadSHA: "abc123", HeadBranch: "feature", PreferredNames: []string{"pr/7"}, MatchingRemotes: []string{"origin"},
})
require.NoError(t, err)
require.Equal(t, "pr/7", branch)
require.Equal(t, "origin", remote)
}
func TestFindCleanBranchIgnoreSHAUsesName(t *testing.T) {
runner := &cleanRunner{branches: "deadbeef\tfeature\torigin/feature\n"}
branch, remote, err := FindCleanBranch(context.Background(), runner, FindCleanBranchOptions{
Directory: "/work", HeadSHA: "abc123", HeadBranch: "feature", IgnoreSHA: true, MatchingRemotes: []string{"origin"},
})
require.NoError(t, err)
require.Equal(t, "feature", branch)
require.Equal(t, "origin", remote)
}
func TestFindCleanBranchRejectsAmbiguity(t *testing.T) {
runner := &cleanRunner{branches: "abc123\tone\t\nabc123\ttwo\t\n"}
_, _, err := FindCleanBranch(context.Background(), runner, FindCleanBranchOptions{
Directory: "/work", HeadSHA: "abc123", HeadBranch: "feature", MatchingRemotes: []string{"origin"},
})
require.ErrorContains(t, err, "multiple local branches")
}
func TestFindCleanBranchSkipsDefaultBranch(t *testing.T) {
runner := &cleanRunner{branches: "abc123\tmain\torigin/main\nabc123\tpr/7\t\n"}
branch, _, err := FindCleanBranch(context.Background(), runner, FindCleanBranchOptions{
Directory: "/work", HeadSHA: "abc123", HeadBranch: "feature", DefaultBranch: "main", PreferredNames: []string{"pr/7"}, MatchingRemotes: []string{"origin"},
})
require.NoError(t, err)
require.Equal(t, "pr/7", branch)
}
func TestCleanPullRejectsDefaultBranch(t *testing.T) {
_, err := CleanPull(context.Background(), &cleanRunner{}, CleanOptions{Directory: "/work", DefaultBranch: "main", LocalBranch: "main"})
require.ErrorContains(t, err, "default branch")
}
func TestCleanPullValidatesBeforeMutation(t *testing.T) {
runner := &cleanRunner{dirty: true}
_, err := CleanPull(context.Background(), runner, CleanOptions{Directory: "/work", DefaultBranch: "main", LocalBranch: "feature"})
require.ErrorContains(t, err, "working tree has changes")
for _, call := range runner.calls {
require.False(t, strings.HasPrefix(call, "switch "))
require.False(t, strings.HasPrefix(call, "branch "))
require.False(t, strings.HasPrefix(call, "push "))
}
}
func TestCleanPullDeletesLocalAndRemoteBranches(t *testing.T) {
runner := &cleanRunner{onFeature: true}
result, err := CleanPull(context.Background(), runner, CleanOptions{
Directory: "/work", DefaultBranch: "main", LocalBranch: "feature", Remote: "origin", RemoteBranch: "feature",
DeleteRemote: true, CredentialArgs: []string{"-c", "credential.helper=tea"},
})
require.NoError(t, err)
require.Equal(t, CleanResult{LocalBranch: "feature", Remote: "origin", RemoteBranch: "feature", DefaultBranch: "main", RemoteDeleted: true}, result)
require.Contains(t, runner.calls, "switch --quiet -- main")
require.Contains(t, runner.calls, "branch -D -- feature")
require.Contains(t, runner.calls, "-c credential.helper=tea push --delete -- origin feature")
}
func TestCleanPullSkipsRemoteWhenUnavailable(t *testing.T) {
runner := &cleanRunner{}
result, err := CleanPull(context.Background(), runner, CleanOptions{Directory: "/work", DefaultBranch: "main", LocalBranch: "feature"})
require.NoError(t, err)
require.True(t, result.RemoteSkipped)
require.Equal(t, "feature", result.LocalBranch)
for _, call := range runner.calls {
require.False(t, strings.HasPrefix(call, "push "))
}
}