Files
gitea-tea/internal/terminal/pull_form_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

105 lines
3.9 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package terminal
import (
"context"
"errors"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestDefaultPullTitle(t *testing.T) {
for _, tc := range []struct{ in, want string }{
{"", ""},
{"feature", "Feature"},
{"fix-my-bug", "Fix My Bug"},
{"alice:add_login", "Add Login"},
{"über-fix", "Über Fix"},
} {
require.Equal(t, tc.want, DefaultPullTitle(tc.in), tc.in)
}
}
func TestPullFormAsksTitleBodyThenWhatsNext(t *testing.T) {
known := PullFormChoices{Repo: "alice/x", BaseOwner: "alice", Base: "main", HeadOwner: "alice", HeadBranch: "feature", Title: "Seeded", Body: "Seeded body", AllowMaintainerEdits: true}
for _, tc := range []struct {
name, input string
choices PullFormChoices
want PullFormValues
err error
rendered []string
hidden []string
}{
{
name: "submit", input: "Seeded\n\n1\n", choices: known,
want: PullFormValues{IssueFormValues: IssueFormValues{Title: "Seeded", Body: "Seeded body"}, Base: "main", Head: "feature", Topic: "feature", AllowMaintainerEdits: true},
rendered: []string{"Creating pull request for feature into main in alice/x", "Title (required):", BodyTitle, "What's next?", "Change branches"},
hidden: []string{"Base branch", "AGit", "Issue", "Add metadata"},
},
{
name: "submit as draft", input: "Add login\n\n2\n", choices: known,
want: PullFormValues{IssueFormValues: IssueFormValues{Title: "Add login", Body: "Seeded body"}, Base: "main", Head: "feature", Topic: "feature", AllowMaintainerEdits: true, AsDraft: true},
},
{name: "cancel", input: "Seeded\n\n4\n", choices: known, err: ErrCanceled},
{
name: "unknown head asks the branches first", input: "main\nn\nbob\nfix-typo\n\n\nFix Typo\n\n1\n", choices: PullFormChoices{Repo: "alice/x", BaseOwner: "alice", Base: "main"},
want: PullFormValues{IssueFormValues: IssueFormValues{Title: "Fix Typo"}, Base: "main", Head: "bob:fix-typo"},
rendered: []string{"Base branch (required):", "Push through AGit", "Head branch (required):", "Creating pull request for bob:fix-typo into main in alice/x"},
},
{
name: "metadata", input: "Seeded\n\n3\n2\n2026-12-24\n1\n", choices: func() PullFormChoices {
c := known
c.CanPush, c.Milestones = true, []IssueMilestoneChoice{{Title: "v1.0", ID: 4}}
return c
}(),
want: PullFormValues{IssueFormValues: IssueFormValues{Title: "Seeded", Body: "Seeded body", Milestone: 4, Deadline: mustDueDate(t, "2026-12-24")}, Base: "main", Head: "feature", Topic: "feature", AllowMaintainerEdits: true},
rendered: []string{"Add metadata (assignees, labels, milestone, due date)", "Milestone:", "Due date:"},
},
} {
t.Run(tc.name, func(t *testing.T) {
p, rendered := linePrompter(t, tc.input)
values, err := p.CreatePull(context.Background(), tc.choices)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
return
}
require.NoError(t, err)
require.Equal(t, tc.want, values)
for _, want := range tc.rendered {
require.Contains(t, rendered.String(), want)
}
for _, unwanted := range tc.hidden {
require.NotContains(t, rendered.String(), unwanted)
}
})
}
}
func mustDueDate(t *testing.T, raw string) *time.Time {
t.Helper()
due, err := parseDueDate(raw)
require.NoError(t, err)
return due
}
func TestCreatePullPreservesDraftOnEditorFailure(t *testing.T) {
path := filepath.Join(t.TempDir(), "draft.md")
called := false
p, _ := linePrompter(t, "Title\n")
values, err := p.CreatePull(context.Background(), PullFormChoices{
BaseOwner: "alice", Base: "main", HeadOwner: "alice", HeadBranch: "feature",
EditBody: func(context.Context, string) (Draft, error) {
called = true
return Draft{Path: path, Body: "partial"}, errors.New("editor failed")
},
})
require.True(t, called)
require.ErrorContains(t, err, "editor failed")
require.Equal(t, path, values.Draft.Path)
}