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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Bug Fixes:

- fix(help): always show `GLOBAL FLAGS` as the last section in help output, after `COMMANDS` ([#1656](https://github.com/fastly/cli/issues/1656))
- fix(service-version): describe `--version` as draft-only in `stage` and `unstage` help ([#1320](https://github.com/fastly/cli/issues/1320))
- fix(auth): ensure `fastly auth token` output is not corrupted by a version update notice written to stdout ([#1971](https://github.com/fastly/cli/pull/1971))

Expand Down
67 changes: 67 additions & 0 deletions pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,73 @@ func TestHelpSubcommandFlags(t *testing.T) {
}
}

// TestHelpSectionOrder verifies that GLOBAL FLAGS is always the last section
// before SEE ALSO, whether the command has subcommands or only flags.
func TestHelpSectionOrder(t *testing.T) {
cases := []struct {
name string
args string
before []string
}{
{
name: "parent command --help lists commands before global flags",
args: "service --help",
before: []string{"COMMANDS"},
},
{
name: "leaf command --help lists optional flags before global flags",
args: "service create --help",
before: []string{"OPTIONAL FLAGS"},
},
{
name: "verbose help for parent command lists subcommands before global flags",
args: "help service",
before: []string{"SUBCOMMANDS"},
},
}

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()

globalIdx := strings.Index(output, "GLOBAL FLAGS")
if globalIdx == -1 {
t.Fatalf("expected \"GLOBAL FLAGS\" in output for %q, got:\n%s", tc.args, output)
}
for _, section := range tc.before {
idx := strings.Index(output, section)
if idx == -1 {
t.Fatalf("expected %q in output for %q, got:\n%s", section, tc.args, output)
}
if idx > globalIdx {
t.Errorf("expected %q before \"GLOBAL FLAGS\" for %q, got:\n%s", section, tc.args, output)
}
}
if seeAlso := strings.Index(output, "SEE ALSO"); seeAlso != -1 && seeAlso < globalIdx {
t.Errorf("expected \"GLOBAL FLAGS\" before \"SEE ALSO\" for %q, got:\n%s", tc.args, output)
}
})
}
}

// TestTokenExemptCommandWithArgs checks that a token-exempt command still runs without a token when it takes positional arguments.
func TestTokenExemptCommandWithArgs(t *testing.T) {
var stdout bytes.Buffer
Expand Down
16 changes: 8 additions & 8 deletions pkg/app/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ var CompactUsageTemplate = `{{define "FormatCommand" -}}
{{T "OPTIONAL FLAGS"|Bold}}
{{.Context.Flags|OptionalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{if .Context.Flags|GlobalFlags -}}
{{T "GLOBAL FLAGS"|Bold}}
{{.Context.Flags|GlobalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{if .Context.Args -}}
{{T "ARGS"|Bold}}
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
Expand All @@ -115,6 +111,10 @@ var CompactUsageTemplate = `{{define "FormatCommand" -}}
{{T "COMMANDS"|Bold}}
{{.App.Commands|CommandsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{if .Context.Flags|GlobalFlags -}}
{{T "GLOBAL FLAGS"|Bold}}
{{.Context.Flags|GlobalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
` + authGuideTemplate + `
{{T "SEE ALSO"|Bold}}
{{.Context.SelectedCommand|SeeAlso}}
Expand Down Expand Up @@ -225,10 +225,6 @@ const VerboseUsageTemplate = `{{define "FormatCommands" -}}
{{T "OPTIONAL FLAGS"|Bold}}
{{.Context.Flags|OptionalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{if .Context.Flags|GlobalFlags -}}
{{T "GLOBAL FLAGS"|Bold}}
{{.Context.Flags|GlobalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{if .Context.Args -}}
{{T "ARGS"|Bold}}
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
Expand All @@ -242,6 +238,10 @@ const VerboseUsageTemplate = `{{define "FormatCommands" -}}
{{T "COMMANDS"|Bold -}}
{{template "FormatCommands" .App}}
{{end -}}
{{if .Context.Flags|GlobalFlags -}}
{{T "GLOBAL FLAGS"|Bold}}
{{.Context.Flags|GlobalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
` + authGuideTemplate + `
{{T "SEE ALSO"|Bold}}
{{.Context.SelectedCommand|SeeAlso}}
Expand Down