Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,57 @@ func TestShortHelpFlag(t *testing.T) {
}
}

// TestHelpSubcommandFlags verifies that `fastly help <subcommand>` displays the command's
// specific flags (Required / Optional flags) consistently with `<subcommand> --help`.
func TestHelpSubcommandFlags(t *testing.T) {
cases := []struct {
name string
args string
wantFlags []string
}{
{
name: "help service create includes optional flags",
args: "help service create",
wantFlags: []string{"OPTIONAL FLAGS", "--comment", "--name", "--type", "GLOBAL FLAGS"},
},
{
name: "help service alert create includes required and optional flags",
args: "help service alert create",
wantFlags: []string{"REQUIRED FLAGS", "--description", "--metric", "--name", "OPTIONAL FLAGS", "GLOBAL FLAGS"},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var stdout bytes.Buffer
args := testutil.SplitArgs(tc.args)
app.Init = func(_ []string, _ io.Reader) (*global.Data, error) {
return testutil.MockGlobalData(args, &stdout), nil
}
err := app.Run(args, nil)
var output string
if err != nil {
var re errors.RemediationError
var se errors.SkipExitError
if ok := stderrors.As(err, &re); ok {
output = re.Prefix
} else if ok := stderrors.As(err, &se); ok {
if re, ok := se.Err.(errors.RemediationError); ok {
output = re.Prefix
}
}
}
output += stdout.String()

for _, want := range tc.wantFlags {
if !strings.Contains(output, want) {
t.Errorf("expected %q in output for %q, got:\n%s", want, tc.args, output)
}
}
})
}
}

// stripTrailingSpace removes any trailing spaces from the multiline str.
func stripTrailingSpace(str string) string {
buf := bytes.NewBuffer(nil)
Expand Down
13 changes: 12 additions & 1 deletion pkg/app/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,22 @@ const VerboseUsageTemplate = `{{define "FormatCommands" -}}
{{if .Context.SelectedCommand -}}
{{T "USAGE"|Bold}}
{{.App.Name}} {{.App.FlagSummary}} {{.Context.SelectedCommand.CmdSummary}}
{{if .Context.SelectedCommand.Help}}
{{.Context.SelectedCommand.Help|Wrap 0}}
{{end -}}
{{else}}
{{- T "USAGE"|Bold}}
{{template "FormatUsage" .App -}}
{{end -}}
{{if .Context.Flags|GlobalFlags }}
{{if .Context.Flags|RequiredFlags -}}
{{T "REQUIRED FLAGS"|Bold}}
{{.Context.Flags|RequiredFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{if .Context.Flags|OptionalFlags -}}
{{T "OPTIONAL FLAGS"|Bold}}
{{.Context.Flags|OptionalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{if .Context.Flags|GlobalFlags -}}
{{T "GLOBAL FLAGS"|Bold}}
{{.Context.Flags|GlobalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
Expand Down
Loading