feat(help): group root help, show relevant flags and add help topics

The root help lists commands in gh-style groups (core, Actions,
additional and alias commands) followed by help topics, examples and
pointers. Every help page uses USAGE, ALIASES, FLAGS, INHERITED FLAGS,
ARGUMENTS, JSON FIELDS, EXAMPLES and LEARN MORE sections. Inherited
flags show --repo, --remote and --cwd only where a repository is
selected, --json and --jq where JSON is printed, --yes where the
command confirms, and one line for the remaining global flags.

tea help environment, exit-codes and formatting document global flags,
environment variables, exit codes and JSON output. tea help with an
unknown topic is a usage error with suggestions instead of printing the
root help. A test keeps Shorts, examples and Long texts complete, with
allowlists for the commands that still lack them.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-11 22:55:27 +02:00
1 parent f6a0bcfa6a
commit 48c1e5b781
3 files changed
+781 -9

No files matched your search

+403
View File
@@ -10,7 +10,10 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"unicode"
"gitea.dev/tea/internal/clierr"
"github.com/spf13/cobra"
@@ -193,6 +196,114 @@ func TestRawJSONCommandsKeepJQToThemselves(t *testing.T) {
require.ErrorContains(t, root.Execute(), "raw prints text; --json is not supported")
}
func TestRootHelpGroupsCommandsAndTopics(t *testing.T) {
deps, out, _ := testDeps(t)
require.NoError(t, Execute(context.Background(), deps, []string{"alias", "set", "co", "pr checkout"}))
out.Reset()
require.NoError(t, Execute(context.Background(), deps, []string{"--help"}))
help := out.String()
for _, want := range []string{
"Work seamlessly with Gitea from the command line.\n\nUSAGE\n tea <command> <subcommand> [flags]\n",
"\nCORE COMMANDS\n auth: ",
"\n repo: ",
"\nACTIONS COMMANDS\n run: ",
"\nADDITIONAL COMMANDS\n admin: ",
"\nALIAS COMMANDS\n co: Alias for \"pr checkout\"\n",
"\nHELP TOPICS\n environment: ",
"\nFLAGS\n --help Show help for command\n --version Show tea version\n",
"\nEXAMPLES\n $ tea issue create\n",
"\nLEARN MORE\n Use `tea <command> <subcommand> --help` for more information about a command.\n",
} {
require.Contains(t, help, want)
}
require.NotContains(t, help, " help:")
require.NotContains(t, help, " man:")
// Every visible top-level command belongs to one root help group.
for _, cmd := range NewRoot(deps).Commands() {
if cmd.IsAvailableCommand() {
require.Contains(t, []string{"core", "actions", "additional"}, cmd.GroupID, cmd.Name())
}
}
}
func TestCommandHelpShowsRelevantFlagsOnly(t *testing.T) {
deps, out, _ := testDeps(t)
require.NoError(t, Execute(context.Background(), deps, []string{"issue", "list", "--help"}))
help := out.String()
for _, want := range []string{
"\nUSAGE\n tea issue list [flags]\n",
"\nALIASES\n tea issue ls\n",
"\nFLAGS\n --assignee string ",
"\nINHERITED FLAGS\n -R, --repo OWNER/REPO ",
"\n --json fields ",
"\n -q, --jq expression ",
"\n -h, --help Show help for command\n\n " + globalFlagsLine + "\n",
"\nJSON FIELDS\n assignees, author, body, ",
} {
require.Contains(t, help, want)
}
for _, unwanted := range []string{"--sudo string", "--yes", `[="*"]`, "Global Flags:"} {
require.NotContains(t, help, unwanted)
}
out.Reset()
require.NoError(t, Execute(context.Background(), deps, []string{"label", "list", "--help"}))
require.Contains(t, out.String(), "\nFLAGS\n --limit int Maximum results (1..100) (default 30)\n --org string ")
for _, args := range [][]string{{"version", "--help"}, {"alias", "list", "--help"}, {"auth", "status", "--help"}} {
out.Reset()
require.NoError(t, Execute(context.Background(), deps, args))
require.NotContains(t, out.String(), "--repo", args)
require.Contains(t, out.String(), globalFlagsLine, args)
}
out.Reset()
require.NoError(t, Execute(context.Background(), deps, []string{"version", "--help"}))
require.NotContains(t, out.String(), "--json fields")
out.Reset()
require.NoError(t, Execute(context.Background(), deps, []string{"issue", "--help"}))
require.Contains(t, out.String(), "\nUSAGE\n tea issue <command> [flags]\n\nAVAILABLE COMMANDS\n attachment: ")
require.NotContains(t, out.String(), "JSON FIELDS")
}
func TestHelpConfirmAndArgumentsAnnotations(t *testing.T) {
deps, out, _ := testDeps(t)
root := NewRoot(deps)
issue, _, err := root.Find([]string{"issue"})
require.NoError(t, err)
issue.AddCommand(&cobra.Command{Use: "drop <number>", Short: "Drop an issue", Example: "# Drop issue 7\n$ tea issue drop 7", Annotations: map[string]string{"tea.confirm": "true", "tea.arguments": "An issue is a number or a URL."}, RunE: func(*cobra.Command, []string) error { return nil }})
root.SetArgs([]string{"issue", "drop", "--help"})
require.NoError(t, root.Execute())
require.Contains(t, out.String(), "\n -y, --yes ")
require.Contains(t, out.String(), "\nARGUMENTS\n An issue is a number or a URL.\n\nJSON FIELDS\n")
require.Contains(t, out.String(), "\nEXAMPLES\n # Drop issue 7\n $ tea issue drop 7\n")
}
func TestHelpTopicsAndUnknownTopics(t *testing.T) {
deps, out, _ := testDeps(t)
for args, want := range map[string]string{
"help environment": " TEA_CONFIG_DIR ",
"help exit-codes": " 4 authentication",
"help formatting": "--jq runs inside tea",
"help issue list": "\nUSAGE\n tea issue list [flags]\n",
"environment": " -R, --repo OWNER/REPO ",
} {
out.Reset()
require.NoError(t, Execute(context.Background(), deps, strings.Fields(args)), args)
require.Contains(t, out.String(), want, args)
}
for args, want := range map[string]string{
"help nope": `unknown help topic "nope"; run tea help to list commands and topics`,
"help isue": `unknown help topic "isue"; did you mean issue?`,
"help isue list": `unknown help topic "isue list"; did you mean issue?`,
"help env": `unknown help topic "env"; did you mean environment?`,
"help issue nope": `unknown help topic "issue nope"; run tea help to list commands and topics`,
} {
out.Reset()
err := Execute(context.Background(), deps, strings.Fields(args))
require.Equal(t, clierr.Usage, clierr.ExitCode(err), args)
require.EqualError(t, err, want, args)
require.Empty(t, out.String(), args)
}
}
func TestVerbAndFlagAliases(t *testing.T) {
deps, _, _ := testDeps(t)
root := NewRoot(deps)
@@ -221,6 +332,56 @@ func TestVerbAndFlagAliases(t *testing.T) {
require.NotContains(t, edit.UsageString(), "add-labels")
}
// TestHelpTextsAreComplete checks the help texts of every command: a Short line without a final period,
// a capitalized hyphenated verb ("Mark-read …") or a sibling's wording, an Example on every command and a
// Long text on every command group. Area streams remove the commands they complete from the allowlists
// below; S8 deletes the allowlists.
func TestHelpTextsAreComplete(t *testing.T) {
deps, _, _ := testDeps(t)
pending := func(list string) map[string]bool {
set := map[string]bool{}
for path := range strings.Lines(list) {
if path = strings.TrimSpace(path); path != "" {
set[path] = true
}
}
return set
}
shorts, examples, longs := pending(shortsToFix), pending(examplesToWrite), pending(longsToWrite)
walkCommands(NewRoot(deps), func(cmd *cobra.Command) {
if !cmd.HasParent() || !cmd.IsAvailableCommand() {
return
}
path := strings.TrimPrefix(cmd.CommandPath(), "tea ")
generated := strings.Contains(cmd.Name(), "-") && strings.HasPrefix(cmd.Short, titleCase(cmd.Name())+" ")
siblings := slices.ContainsFunc(cmd.Parent().Commands(), func(other *cobra.Command) bool { return other != cmd && other.Short == cmd.Short })
badShort := cmd.Short == "" || strings.HasSuffix(cmd.Short, ".") || unicode.IsLower(rune(cmd.Short[0])) || generated || siblings
checks := []struct {
name string
missing bool
pending map[string]bool
}{
{"a clear Short", badShort, shorts},
{"an Example", cmd.Example == "" && cmd.Annotations["tea.group"] == "", examples},
{"a Long text", cmd.Long == "" && cmd.Annotations["tea.group"] != "", longs},
}
for _, check := range checks {
switch {
case check.missing && !check.pending[path]:
t.Errorf("%s needs %s", path, check.name)
case !check.missing && check.pending[path]:
t.Errorf("%s has %s now; remove it from the allowlist", path, check.name)
}
delete(check.pending, path)
}
})
for _, check := range []map[string]bool{shorts, examples, longs} {
for path := range check {
t.Errorf("%s is allowlisted but is not a command", path)
}
}
}
// walkCommands calls visit for cmd and each of its descendants.
func walkCommands(cmd *cobra.Command, visit func(*cobra.Command)) {
visit(cmd)
@@ -228,3 +389,245 @@ func walkCommands(cmd *cobra.Command, visit func(*cobra.Command)) {
walkCommands(child, visit)
}
}
// shortsToFix, examplesToWrite and longsToWrite list the commands whose help texts area streams
// have yet to complete; see TestHelpTextsAreComplete.
const (
shortsToFix = `
config get
config list
notification mark-read
notification mark-unread
pr review cancel-request
`
examplesToWrite = `
admin user create
admin user delete
admin user edit
admin user list
admin user view
alias delete
alias list
alias set
api
auth login
auth logout
auth refresh
auth setup-git
auth status
auth switch
branch create
branch delete
branch list
branch protect
branch rename
branch unprotect
branch view
completion bash
completion fish
completion powershell
completion zsh
config get
config list
config path
config set
doctor
issue attachment delete
issue attachment download
issue attachment edit
issue attachment list
issue attachment upload
issue attachment view
issue close
issue comment create
issue comment delete
issue comment edit
issue comment list
issue comment view
issue create
issue edit
issue list
issue reopen
issue view
label create
label delete
label edit
label export
label list
milestone close
milestone create
milestone delete
milestone edit
milestone issue add
milestone issue list
milestone issue remove
milestone list
milestone reopen
milestone view
notification list
notification mark-read
notification mark-unread
notification pin
notification unpin
notification view
notification watch
org create
org delete
org edit
org list
org member list
org member private
org member public
org member remove
org view
pr attachment delete
pr attachment download
pr attachment edit
pr attachment list
pr attachment upload
pr attachment view
pr checkout
pr checks
pr clean
pr close
pr comment create
pr comment delete
pr comment edit
pr comment list
pr comment view
pr create
pr diff
pr edit
pr list
pr merge
pr reopen
pr review
pr review cancel-request
pr review comments
pr review delete
pr review dismiss
pr review list
pr review reply
pr review request
pr review resolve
pr review submit
pr review undismiss
pr review unresolve
pr review view
pr status
pr view
release asset delete
release asset download
release asset edit
release asset list
release asset upload
release create
release delete
release edit
release list
release publish
release view
repo archive
repo clone
repo create
repo delete
repo edit
repo fork
repo list
repo migrate
repo set-default
repo sync
repo view
run artifact download
run artifact list
run cancel
run delete
run jobs
run list
run logs
run rerun
run view
run watch
search issue
search pr
search repo
secret delete
secret list
secret set
ssh-key create
ssh-key delete
ssh-key list
ssh-key view
status
team create
team delete
team edit
team list
team member add
team member list
team member remove
team repo add
team repo list
team repo remove
team view
time create
time delete
time list
time reset
variable delete
variable list
variable set
variable view
webhook create
webhook delete
webhook edit
webhook list
webhook view
wiki create
wiki delete
wiki edit
wiki list
wiki revisions
wiki view
workflow disable
workflow enable
workflow list
workflow run
workflow view
`
longsToWrite = `
admin
admin user
alias
auth
branch
issue
issue attachment
issue comment
label
milestone
milestone issue
notification
org
org member
pr
pr attachment
pr comment
release
release asset
repo
run
run artifact
search
secret
ssh-key
team
team member
team repo
time
variable
webhook
wiki
workflow
`
)