| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | "github.com/stretchr/testify/require" |
| 16 | ) |
| 17 | |
| 18 | func Test_ShowRootCommandHelp_NoAuthor(t *testing.T) { |
| 19 | output := new(bytes.Buffer) |
| 20 | cmd := &Command{Writer: output} |
| 21 | _ = ShowRootCommandHelp(cmd) |
| 22 | |
| 23 | if bytes.Contains(output.Bytes(), []byte("AUTHOR(S):")) { |
| 24 | t.Errorf("expected\n%snot to include %s", output.String(), "AUTHOR(S):") |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func Test_ShowRootCommandHelp_NoVersion(t *testing.T) { |
| 29 | output := new(bytes.Buffer) |
| 30 | cmd := &Command{Writer: output} |
| 31 | |
| 32 | cmd.Version = "" |
| 33 | |
| 34 | _ = ShowRootCommandHelp(cmd) |
| 35 | |
| 36 | if bytes.Contains(output.Bytes(), []byte("VERSION:")) { |
| 37 | t.Errorf("expected\n%snot to include %s", output.String(), "VERSION:") |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func Test_ShowRootCommandHelp_HideVersion(t *testing.T) { |
| 42 | output := new(bytes.Buffer) |
| 43 | cmd := &Command{Writer: output} |
| 44 | |
| 45 | cmd.HideVersion = true |
| 46 | |
| 47 | _ = ShowRootCommandHelp(cmd) |
| 48 | |
| 49 | if bytes.Contains(output.Bytes(), []byte("VERSION:")) { |
| 50 | t.Errorf("expected\n%snot to include %s", output.String(), "VERSION:") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func Test_ShowRootCommandHelp_MultiLineDescription(t *testing.T) { |
| 55 | output := new(bytes.Buffer) |
| 56 | cmd := &Command{Writer: output} |
| 57 | |
| 58 | cmd.HideVersion = true |
| 59 | cmd.Description = "multi\n line" |
| 60 | |
| 61 | _ = ShowRootCommandHelp(cmd) |
| 62 | |
| 63 | if !bytes.Contains(output.Bytes(), []byte("DESCRIPTION:\n multi\n line")) { |
| 64 | t.Errorf("expected\n%s\nto include\n%s", output.String(), "DESCRIPTION:\n multi\n line") |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestShowCommandHelpAndExit(t *testing.T) { |
| 69 | output := new(bytes.Buffer) |
| 70 | cmd := &Command{ |
| 71 | Commands: []*Command{ |
| 72 | { |
| 73 | Name: "ok", |
| 74 | Writer: output, |
| 75 | }, |
| 76 | }, |
| 77 | Writer: output, |
| 78 | } |
| 79 | |
| 80 | ShowCommandHelpAndExit(context.Background(), cmd, "ok", 42) |
| 81 | |
| 82 | require.Equal(t, 42, lastExitCode) |
| 83 | } |
| 84 | |
| 85 | func TestShowRootCommandHelpAndExit(t *testing.T) { |
| 86 | output := new(bytes.Buffer) |
| 87 | cmd := &Command{Writer: output} |
| 88 | |
| 89 | ShowRootCommandHelpAndExit(cmd, 42) |
| 90 | |
| 91 | require.Equal(t, 42, lastExitCode) |
| 92 | } |
| 93 | |
| 94 | func TestShowSubcommandHelpAndExit(t *testing.T) { |
| 95 | output := new(bytes.Buffer) |
| 96 | ok := &Command{ |
| 97 | Name: "ok", |
| 98 | Writer: output, |
| 99 | } |
| 100 | cmd := &Command{ |
| 101 | Commands: []*Command{ok}, |
| 102 | Writer: output, |
| 103 | } |
| 104 | _ = cmd |
| 105 | |
| 106 | ShowSubcommandHelpAndExit(ok, 42) |
| 107 | |
| 108 | require.Equal(t, 42, lastExitCode) |
| 109 | } |
| 110 | |
| 111 | func Test_HelpFlag_RequiredFlagsNoDefault(t *testing.T) { |
| 112 | output := new(bytes.Buffer) |
| 113 | |
| 114 | cmd := &Command{ |
| 115 | Flags: []Flag{ |
| 116 | &Int64Flag{Name: "foo", Aliases: []string{"f"}, Required: true}, |
| 117 | }, |
| 118 | Arguments: AnyArguments, |
| 119 | Writer: output, |
| 120 | } |
| 121 | |
| 122 | _ = cmd.Run(buildTestContext(t), []string{"test", "-h"}) |
| 123 | |
| 124 | expected := `NAME: |
| 125 | test - A new cli application |
| 126 | |
| 127 | USAGE: |
| 128 | test [global options] [arguments...] |
| 129 | |
| 130 | GLOBAL OPTIONS: |
| 131 | --foo int, -f int |
| 132 | --help, -h show help |
| 133 | ` |
| 134 | |
| 135 | assert.Contains(t, output.String(), expected, |
| 136 | "expected output to include usage text") |
| 137 | } |
| 138 | |
| 139 | func Test_HelpCommand_RequiredFlagsNoDefault(t *testing.T) { |
| 140 | output := new(bytes.Buffer) |
| 141 | |
| 142 | cmd := &Command{ |
| 143 | Flags: []Flag{ |
| 144 | &Int64Flag{Name: "foo", Aliases: []string{"f"}, Required: true}, |
| 145 | }, |
| 146 | Arguments: AnyArguments, |
| 147 | Writer: output, |
| 148 | } |
| 149 | |
| 150 | _ = cmd.Run(buildTestContext(t), []string{"test", "help"}) |
| 151 | |
| 152 | expected := `NAME: |
| 153 | test - A new cli application |
| 154 | |
| 155 | USAGE: |
| 156 | test [global options] [arguments...] |
| 157 | |
| 158 | GLOBAL OPTIONS: |
| 159 | --foo int, -f int |
| 160 | --help, -h show help |
| 161 | ` |
| 162 | |
| 163 | assert.Contains(t, output.String(), expected, |
| 164 | "expected output to include usage text") |
| 165 | } |
| 166 | |
| 167 | // Test_HelpCommand_ParsesFlags is a regression test for #2271: flags |
| 168 | // declared on a user-supplied command named "help" must still be parsed. |
| 169 | // Previously in v3.6.2 flag pre-parsing was skipped for any command named |
| 170 | // "help", causing such flags to be silently dropped. |
| 171 | func Test_HelpCommand_ParsesFlags(t *testing.T) { |
| 172 | var parsed string |
| 173 | |
| 174 | cmd := &Command{ |
| 175 | Name: "app", |
| 176 | Commands: []*Command{ |
| 177 | { |
| 178 | Name: "help", |
| 179 | Flags: []Flag{ |
| 180 | &StringFlag{Name: "topic", Aliases: []string{"t"}}, |
| 181 | }, |
| 182 | Action: func(_ context.Context, c *Command) error { |
| 183 | parsed = c.String("topic") |
| 184 | return nil |
| 185 | }, |
| 186 | }, |
| 187 | }, |
| 188 | } |
| 189 | |
| 190 | err := cmd.Run(buildTestContext(t), []string{"app", "help", "--topic", "foo"}) |
| 191 | require.NoError(t, err) |
| 192 | assert.Equal(t, "foo", parsed, |
| 193 | "expected --topic flag on help subcommand to be parsed") |
| 194 | } |
| 195 | |
| 196 | func Test_Help_Custom_Flags(t *testing.T) { |
| 197 | oldFlag := HelpFlag |
| 198 | defer func() { |
| 199 | HelpFlag = oldFlag |
| 200 | }() |
| 201 | |
| 202 | HelpFlag = &BoolFlag{ |
| 203 | Name: "help", |
| 204 | Aliases: []string{"x"}, |
| 205 | Usage: "show help", |
| 206 | } |
| 207 | |
| 208 | out := &bytes.Buffer{} |
| 209 | |
| 210 | cmd := &Command{ |
| 211 | Flags: []Flag{ |
| 212 | &BoolFlag{Name: "foo", Aliases: []string{"h"}}, |
| 213 | }, |
| 214 | Action: func(_ context.Context, cmd *Command) error { |
| 215 | assert.True(t, cmd.Bool("h"), "custom help flag not set") |
| 216 | return nil |
| 217 | }, |
| 218 | Writer: out, |
| 219 | } |
| 220 | |
| 221 | _ = cmd.Run(buildTestContext(t), []string{"test", "-h"}) |
| 222 | require.Len(t, out.String(), 0) |
| 223 | } |
| 224 | |
| 225 | func Test_Help_Nil_Flags(t *testing.T) { |
| 226 | oldFlag := HelpFlag |
| 227 | defer func() { |
| 228 | HelpFlag = oldFlag |
| 229 | }() |
| 230 | HelpFlag = nil |
| 231 | |
| 232 | cmd := &Command{ |
| 233 | Action: func(_ context.Context, cmd *Command) error { |
| 234 | return nil |
| 235 | }, |
| 236 | } |
| 237 | out := new(bytes.Buffer) |
| 238 | cmd.Writer = out |
| 239 | _ = cmd.Run(buildTestContext(t), []string{"test"}) |
| 240 | require.Len(t, out.String(), 0) |
| 241 | } |
| 242 | |
| 243 | func Test_Version_Custom_Flags(t *testing.T) { |
| 244 | oldFlag := VersionFlag |
| 245 | defer func() { |
| 246 | VersionFlag = oldFlag |
| 247 | }() |
| 248 | |
| 249 | VersionFlag = &BoolFlag{ |
| 250 | Name: "version", |
| 251 | Aliases: []string{"V"}, |
| 252 | Usage: "show version", |
| 253 | } |
| 254 | |
| 255 | out := &bytes.Buffer{} |
| 256 | cmd := &Command{ |
| 257 | Flags: []Flag{ |
| 258 | &BoolFlag{Name: "foo", Aliases: []string{"v"}}, |
| 259 | }, |
| 260 | Action: func(_ context.Context, cmd *Command) error { |
| 261 | assert.True(t, cmd.Bool("v"), "custom version flag not set") |
| 262 | return nil |
| 263 | }, |
| 264 | Writer: out, |
| 265 | } |
| 266 | |
| 267 | _ = cmd.Run(buildTestContext(t), []string{"test", "-v"}) |
| 268 | require.Len(t, out.String(), 0) |
| 269 | } |
| 270 | |
| 271 | func Test_helpCommand_Action_ErrorIfNoTopic(t *testing.T) { |
| 272 | cmd := &Command{} |
| 273 | |
| 274 | _ = cmd.Run(context.Background(), []string{"foo", "bar"}) |
| 275 | |
| 276 | err := helpCommandAction(context.Background(), cmd) |
| 277 | require.Error(t, err, "expected error from helpCommandAction()") |
| 278 | |
| 279 | exitErr, ok := err.(*exitError) |
| 280 | require.True(t, ok, "expected *exitError from helpCommandAction()") |
| 281 | |
| 282 | require.Contains(t, exitErr.Error(), "No help topic for", "expected an unknown help topic error") |
| 283 | require.Equal(t, 3, exitErr.exitCode, "expected exit value = 3") |
| 284 | } |
| 285 | |
| 286 | func Test_helpCommand_InHelpOutput(t *testing.T) { |
| 287 | cmd := &Command{} |
| 288 | output := &bytes.Buffer{} |
| 289 | cmd.Writer = output |
| 290 | |
| 291 | _ = cmd.Run(buildTestContext(t), []string{"test", "--help"}) |
| 292 | |
| 293 | s := output.String() |
| 294 | |
| 295 | require.NotContains(t, s, "\nCOMMANDS:\nGLOBAL OPTIONS:\n", "empty COMMANDS section detected") |
| 296 | require.Contains(t, s, "--help, -h", "missing \"--help, --h\"") |
| 297 | } |
| 298 | |
| 299 | func TestHelpCommand_FullName(t *testing.T) { |
| 300 | testCases := []struct { |
| 301 | name string |
| 302 | args []string |
| 303 | contains string |
| 304 | skip bool |
| 305 | }{ |
| 306 | { |
| 307 | name: "app help's FullName", |
| 308 | args: []string{"app", "help", "help"}, |
| 309 | contains: "app help -", |
| 310 | }, |
| 311 | { |
| 312 | name: "app help's FullName via flag", |
| 313 | args: []string{"app", "-h", "help"}, |
| 314 | contains: "app help -", |
| 315 | }, |
| 316 | { |
| 317 | name: "cmd help's FullName", |
| 318 | args: []string{"app", "cmd", "help", "help"}, |
| 319 | contains: "app cmd help -", |
| 320 | skip: true, // FIXME: App Command collapse |
| 321 | }, |
| 322 | { |
| 323 | name: "cmd help's FullName via flag", |
| 324 | args: []string{"app", "cmd", "-h", "help"}, |
| 325 | contains: "app cmd help -", |
| 326 | skip: true, // FIXME: App Command collapse |
| 327 | }, |
| 328 | { |
| 329 | name: "subcmd help's FullName", |
| 330 | args: []string{"app", "cmd", "subcmd", "help", "help"}, |
| 331 | contains: "app cmd subcmd help -", |
| 332 | }, |
| 333 | { |
| 334 | name: "subcmd help's FullName via flag", |
| 335 | args: []string{"app", "cmd", "subcmd", "-h", "help"}, |
| 336 | contains: "app cmd subcmd help -", |
| 337 | }, |
| 338 | } |
| 339 | for _, tc := range testCases { |
| 340 | t.Run(tc.name, func(t *testing.T) { |
| 341 | out := &bytes.Buffer{} |
| 342 | |
| 343 | if tc.skip { |
| 344 | t.SkipNow() |
| 345 | } |
| 346 | |
| 347 | cmd := &Command{ |
| 348 | Name: "app", |
| 349 | Commands: []*Command{ |
| 350 | { |
| 351 | Name: "cmd", |
| 352 | Commands: []*Command{ |
| 353 | { |
| 354 | Name: "subcmd", |
| 355 | }, |
| 356 | }, |
| 357 | }, |
| 358 | }, |
| 359 | Writer: out, |
| 360 | ErrWriter: out, |
| 361 | } |
| 362 | |
| 363 | r := require.New(t) |
| 364 | r.NoError(cmd.Run(buildTestContext(t), tc.args)) |
| 365 | r.Contains(out.String(), tc.contains) |
| 366 | }) |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | func Test_helpCommand_HideHelpCommand(t *testing.T) { |
| 371 | buf := &bytes.Buffer{} |
| 372 | cmd := &Command{ |
| 373 | Name: "app", |
| 374 | Writer: buf, |
| 375 | } |
| 376 | |
| 377 | err := cmd.Run(buildTestContext(t), []string{"app", "help", "help"}) |
| 378 | assert.NoError(t, err) |
| 379 | got := buf.String() |
| 380 | notWant := "COMMANDS:" |
| 381 | assert.NotContains(t, got, notWant) |
| 382 | } |
| 383 | |
| 384 | func Test_helpCommand_HideHelpFlag(t *testing.T) { |
| 385 | cmd := buildMinimalTestCommand() |
| 386 | cmd.HideHelp = true |
| 387 | |
| 388 | assert.Error(t, cmd.Run(buildTestContext(t), []string{"app", "help", "-h"}), "Expected flag error - Got nil") |
| 389 | } |
| 390 | |
| 391 | func Test_helpSubcommand_Action_ErrorIfNoTopic(t *testing.T) { |
| 392 | cmd := &Command{} |
| 393 | _ = cmd.Run(context.Background(), []string{"foo", "bar"}) |
| 394 | |
| 395 | err := helpCommandAction(context.Background(), cmd) |
| 396 | require.Error(t, err, "expected error from helpCommandAction(), but got nil") |
| 397 | |
| 398 | exitErr, ok := err.(*exitError) |
| 399 | require.True(t, ok, "expected *exitError from helpCommandAction(), but instead got: %v", err.Error()) |
| 400 | |
| 401 | require.Contains(t, exitErr.Error(), "No help topic for", "expected an unknown help topic error") |
| 402 | require.Equal(t, 3, exitErr.exitCode, "unexpected exit value") |
| 403 | } |
| 404 | |
| 405 | func TestShowRootCommandHelp_CommandAliases(t *testing.T) { |
| 406 | out := &bytes.Buffer{} |
| 407 | |
| 408 | cmd := &Command{ |
| 409 | Commands: []*Command{ |
| 410 | { |
| 411 | Name: "frobbly", |
| 412 | Aliases: []string{"fr", "frob"}, |
| 413 | Action: func(context.Context, *Command) error { |
| 414 | return nil |
| 415 | }, |
| 416 | }, |
| 417 | }, |
| 418 | Writer: out, |
| 419 | } |
| 420 | |
| 421 | _ = cmd.Run(buildTestContext(t), []string{"foo", "--help"}) |
| 422 | require.Contains(t, out.String(), "frobbly, fr, frob") |
| 423 | } |
| 424 | |
| 425 | func TestShowCommandHelp_AppendHelp(t *testing.T) { |
| 426 | testCases := []struct { |
| 427 | name string |
| 428 | hideHelp bool |
| 429 | hideHelpCommand bool |
| 430 | args []string |
| 431 | verify func(*testing.T, string) |
| 432 | }{ |
| 433 | { |
| 434 | name: "with HideHelp", |
| 435 | hideHelp: true, |
| 436 | args: []string{"app", "help"}, |
| 437 | verify: func(t *testing.T, outString string) { |
| 438 | r := require.New(t) |
| 439 | r.NotContains(outString, "help, h Shows a list of commands or help for one command") |
| 440 | r.NotContains(outString, "--help, -h show help") |
| 441 | }, |
| 442 | }, |
| 443 | { |
| 444 | name: "with HideHelpCommand", |
| 445 | hideHelpCommand: true, |
| 446 | args: []string{"app", "--help"}, |
| 447 | verify: func(t *testing.T, outString string) { |
| 448 | r := require.New(t) |
| 449 | r.NotContains(outString, "help, h Shows a list of commands or help for one command") |
| 450 | r.Contains(outString, "--help, -h show help") |
| 451 | }, |
| 452 | }, |
| 453 | { |
| 454 | name: "with Subcommand", |
| 455 | args: []string{"app", "cmd", "help"}, |
| 456 | verify: func(t *testing.T, outString string) { |
| 457 | r := require.New(t) |
| 458 | r.Contains(outString, "--help, -h show help") |
| 459 | }, |
| 460 | }, |
| 461 | { |
| 462 | name: "without Subcommand", |
| 463 | args: []string{"app", "help"}, |
| 464 | verify: func(t *testing.T, outString string) { |
| 465 | r := require.New(t) |
| 466 | r.Contains(outString, "help, h Shows a list of commands or help for one command") |
| 467 | r.Contains(outString, "--help, -h show help") |
| 468 | }, |
| 469 | }, |
| 470 | } |
| 471 | for _, tc := range testCases { |
| 472 | out := &bytes.Buffer{} |
| 473 | |
| 474 | t.Run(tc.name, func(t *testing.T) { |
| 475 | cmd := &Command{ |
| 476 | Name: "app", |
| 477 | HideHelp: tc.hideHelp, |
| 478 | HideHelpCommand: tc.hideHelpCommand, |
| 479 | Commands: []*Command{ |
| 480 | { |
| 481 | Name: "cmd", |
| 482 | HideHelp: tc.hideHelp, |
| 483 | HideHelpCommand: tc.hideHelpCommand, |
| 484 | Commands: []*Command{{Name: "subcmd"}}, |
| 485 | }, |
| 486 | }, |
| 487 | Writer: out, |
| 488 | ErrWriter: out, |
| 489 | } |
| 490 | |
| 491 | _ = cmd.Run(buildTestContext(t), tc.args) |
| 492 | tc.verify(t, out.String()) |
| 493 | }) |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | func TestShowCommandHelp_HelpPrinter(t *testing.T) { |
| 498 | /*doublecho := func(text string) string { |
| 499 | return text + " " + text |
| 500 | }*/ |
| 501 | |
| 502 | tests := []struct { |
| 503 | name string |
| 504 | template string |
| 505 | printer HelpPrinterFunc |
| 506 | command string |
| 507 | wantTemplate string |
| 508 | wantOutput string |
| 509 | }{ |
| 510 | { |
| 511 | name: "no-command", |
| 512 | template: "", |
| 513 | printer: func(w io.Writer, _ string, _ any) { |
| 514 | fmt.Fprint(w, "yo") |
| 515 | }, |
| 516 | command: "", |
| 517 | wantTemplate: RootCommandHelpTemplate, |
| 518 | wantOutput: "yo", |
| 519 | }, |
| 520 | /*{ |
| 521 | name: "standard-command", |
| 522 | template: "", |
| 523 | printer: func(w io.Writer, templ string, data interface{}) { |
| 524 | fmt.Fprint(w, "yo") |
| 525 | }, |
| 526 | command: "my-command", |
| 527 | wantTemplate: CommandHelpTemplate, |
| 528 | wantOutput: "yo", |
| 529 | }, |
| 530 | { |
| 531 | name: "custom-template-command", |
| 532 | template: "{{doublecho .Name}}", |
| 533 | printer: func(w io.Writer, templ string, data interface{}) { |
| 534 | // Pass a custom function to ensure it gets used |
| 535 | fm := map[string]interface{}{"doublecho": doublecho} |
| 536 | HelpPrinterCustom(w, templ, data, fm) |
| 537 | }, |
| 538 | command: "my-command", |
| 539 | wantTemplate: "{{doublecho .Name}}", |
| 540 | wantOutput: "my-command my-command", |
| 541 | },*/ |
| 542 | } |
| 543 | |
| 544 | for _, tt := range tests { |
| 545 | t.Run(tt.name, func(t *testing.T) { |
| 546 | defer func(old HelpPrinterFunc) { |
| 547 | HelpPrinter = old |
| 548 | }(HelpPrinter) |
| 549 | HelpPrinter = func(w io.Writer, templ string, data any) { |
| 550 | assert.Equal(t, tt.wantTemplate, templ, "template mismatch") |
| 551 | tt.printer(w, templ, data) |
| 552 | } |
| 553 | |
| 554 | var buf bytes.Buffer |
| 555 | cmd := &Command{ |
| 556 | Name: "my-app", |
| 557 | Writer: &buf, |
| 558 | Commands: []*Command{ |
| 559 | { |
| 560 | Name: "my-command", |
| 561 | CustomHelpTemplate: tt.template, |
| 562 | }, |
| 563 | }, |
| 564 | } |
| 565 | |
| 566 | err := cmd.Run(buildTestContext(t), []string{"my-app", "help", tt.command}) |
| 567 | require.NoError(t, err) |
| 568 | |
| 569 | got := buf.String() |
| 570 | assert.Equal(t, tt.wantOutput, got) |
| 571 | }) |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | func TestShowCommandHelp_HelpPrinterCustom(t *testing.T) { |
| 576 | doublecho := func(text string) string { |
| 577 | return text + " " + text |
| 578 | } |
| 579 | |
| 580 | testCases := []struct { |
| 581 | name string |
| 582 | template string |
| 583 | printer HelpPrinterCustomFunc |
| 584 | arguments []string |
| 585 | wantTemplate string |
| 586 | wantOutput string |
| 587 | }{ |
| 588 | { |
| 589 | name: "no command", |
| 590 | printer: func(w io.Writer, _ string, _ any, _ map[string]any) { |
| 591 | fmt.Fprint(w, "yo") |
| 592 | }, |
| 593 | arguments: []string{"my-app", "help"}, |
| 594 | wantTemplate: RootCommandHelpTemplate, |
| 595 | wantOutput: "yo", |
| 596 | }, |
| 597 | { |
| 598 | name: "standard command", |
| 599 | printer: func(w io.Writer, _ string, _ any, _ map[string]any) { |
| 600 | fmt.Fprint(w, "yo") |
| 601 | }, |
| 602 | arguments: []string{"my-app", "help", "my-command"}, |
| 603 | wantTemplate: CommandHelpTemplate, |
| 604 | wantOutput: "yo", |
| 605 | }, |
| 606 | { |
| 607 | name: "custom template command", |
| 608 | template: "{{doublecho .Name}}", |
| 609 | printer: func(w io.Writer, templ string, data any, _ map[string]any) { |
| 610 | // Pass a custom function to ensure it gets used |
| 611 | fm := map[string]any{"doublecho": doublecho} |
| 612 | DefaultPrintHelpCustom(w, templ, data, fm) |
| 613 | }, |
| 614 | arguments: []string{"my-app", "help", "my-command"}, |
| 615 | wantTemplate: "{{doublecho .Name}}", |
| 616 | wantOutput: "my-command my-command", |
| 617 | }, |
| 618 | } |
| 619 | |
| 620 | for _, tc := range testCases { |
| 621 | t.Run(tc.name, func(t *testing.T) { |
| 622 | r := require.New(t) |
| 623 | |
| 624 | defer func(old HelpPrinterCustomFunc) { |
| 625 | HelpPrinterCustom = old |
| 626 | }(HelpPrinterCustom) |
| 627 | |
| 628 | HelpPrinterCustom = func(w io.Writer, tmpl string, data any, fm map[string]any) { |
| 629 | r.Nil(fm) |
| 630 | r.Equal(tc.wantTemplate, tmpl) |
| 631 | |
| 632 | tc.printer(w, tmpl, data, fm) |
| 633 | } |
| 634 | |
| 635 | out := &bytes.Buffer{} |
| 636 | cmd := &Command{ |
| 637 | Name: "my-app", |
| 638 | Writer: out, |
| 639 | Commands: []*Command{ |
| 640 | { |
| 641 | Name: "my-command", |
| 642 | CustomHelpTemplate: tc.template, |
| 643 | }, |
| 644 | }, |
| 645 | } |
| 646 | |
| 647 | t.Logf("cmd.Run(ctx, %+[1]v)", tc.arguments) |
| 648 | |
| 649 | r.NoError(cmd.Run(buildTestContext(t), tc.arguments)) |
| 650 | r.Equal(tc.wantOutput, out.String()) |
| 651 | }) |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | func TestShowCommandHelp_CommandAliases(t *testing.T) { |
| 656 | out := &bytes.Buffer{} |
| 657 | |
| 658 | cmd := &Command{ |
| 659 | Commands: []*Command{ |
| 660 | { |
| 661 | Name: "frobbly", |
| 662 | Aliases: []string{"fr", "frob", "bork"}, |
| 663 | Action: func(context.Context, *Command) error { |
| 664 | return nil |
| 665 | }, |
| 666 | }, |
| 667 | }, |
| 668 | Writer: out, |
| 669 | } |
| 670 | |
| 671 | _ = cmd.Run(buildTestContext(t), []string{"foo", "help", "fr"}) |
| 672 | require.Contains(t, out.String(), "frobbly") |
| 673 | } |
| 674 | |
| 675 | func TestShowSubcommandHelp_CommandAliases(t *testing.T) { |
| 676 | cmd := &Command{ |
| 677 | Commands: []*Command{ |
| 678 | { |
| 679 | Name: "frobbly", |
| 680 | Aliases: []string{"fr", "frob", "bork"}, |
| 681 | Action: func(context.Context, *Command) error { |
| 682 | return nil |
| 683 | }, |
| 684 | }, |
| 685 | }, |
| 686 | } |
| 687 | |
| 688 | output := &bytes.Buffer{} |
| 689 | cmd.Writer = output |
| 690 | |
| 691 | _ = cmd.Run(buildTestContext(t), []string{"foo", "help"}) |
| 692 | |
| 693 | assert.Contains(t, output.String(), "frobbly, fr, frob, bork", "expected output to include all command aliases") |
| 694 | } |
| 695 | |
| 696 | func TestShowCommandHelp_Customtemplate(t *testing.T) { |
| 697 | cmd := &Command{ |
| 698 | Name: "foo", |
| 699 | Commands: []*Command{ |
| 700 | { |
| 701 | Name: "frobbly", |
| 702 | Action: func(context.Context, *Command) error { |
| 703 | return nil |
| 704 | }, |
| 705 | CustomHelpTemplate: `NAME: |
| 706 | {{.FullName}} - {{.Usage}} |
| 707 | |
| 708 | USAGE: |
| 709 | {{.FullName}} [FLAGS] TARGET [TARGET ...] |
| 710 | |
| 711 | FLAGS: |
| 712 | {{range .VisibleFlags}}{{.}} |
| 713 | {{end}} |
| 714 | EXAMPLES: |
| 715 | 1. Frobbly runs with this param locally. |
| 716 | $ {{.FullName}} wobbly |
| 717 | `, |
| 718 | }, |
| 719 | }, |
| 720 | } |
| 721 | output := &bytes.Buffer{} |
| 722 | cmd.Writer = output |
| 723 | |
| 724 | _ = cmd.Run(buildTestContext(t), []string{"foo", "help", "frobbly"}) |
| 725 | |
| 726 | assert.NotContains(t, output.String(), "2. Frobbly runs without this param locally.", |
| 727 | "expected output to exclude \"2. Frobbly runs without this param locally.\";") |
| 728 | |
| 729 | assert.Contains(t, output.String(), "1. Frobbly runs with this param locally.", |
| 730 | "expected output to include \"1. Frobbly runs with this param locally.\"") |
| 731 | |
| 732 | assert.Contains(t, output.String(), "$ foo frobbly wobbly", |
| 733 | "expected output to include \"$ foo frobbly wobbly\"") |
| 734 | } |
| 735 | |
| 736 | func TestShowSubcommandHelp_CommandUsageText(t *testing.T) { |
| 737 | cmd := &Command{ |
| 738 | Commands: []*Command{ |
| 739 | { |
| 740 | Name: "frobbly", |
| 741 | UsageText: "this is usage text", |
| 742 | }, |
| 743 | }, |
| 744 | } |
| 745 | |
| 746 | output := &bytes.Buffer{} |
| 747 | cmd.Writer = output |
| 748 | |
| 749 | _ = cmd.Run(buildTestContext(t), []string{"foo", "frobbly", "--help"}) |
| 750 | |
| 751 | assert.Contains(t, output.String(), "this is usage text", |
| 752 | "expected output to include usage text") |
| 753 | } |
| 754 | |
| 755 | func TestShowSubcommandHelp_MultiLine_CommandUsageText(t *testing.T) { |
| 756 | cmd := &Command{ |
| 757 | Commands: []*Command{ |
| 758 | { |
| 759 | Name: "frobbly", |
| 760 | UsageText: `This is a |
| 761 | multi |
| 762 | line |
| 763 | UsageText`, |
| 764 | }, |
| 765 | }, |
| 766 | } |
| 767 | |
| 768 | output := &bytes.Buffer{} |
| 769 | cmd.Writer = output |
| 770 | |
| 771 | _ = cmd.Run(buildTestContext(t), []string{"foo", "frobbly", "--help"}) |
| 772 | |
| 773 | expected := `USAGE: |
| 774 | This is a |
| 775 | multi |
| 776 | line |
| 777 | UsageText |
| 778 | ` |
| 779 | |
| 780 | assert.Contains(t, output.String(), expected, |
| 781 | "expected output to include usage text") |
| 782 | } |
| 783 | |
| 784 | func TestShowSubcommandHelp_GlobalOptions(t *testing.T) { |
| 785 | cmd := &Command{ |
| 786 | Flags: []Flag{ |
| 787 | &StringFlag{ |
| 788 | Name: "foo", |
| 789 | }, |
| 790 | }, |
| 791 | Commands: []*Command{ |
| 792 | { |
| 793 | Name: "frobbly", |
| 794 | Flags: []Flag{ |
| 795 | &StringFlag{ |
| 796 | Name: "bar", |
| 797 | Local: true, |
| 798 | }, |
| 799 | }, |
| 800 | Action: func(context.Context, *Command) error { |
| 801 | return nil |
| 802 | }, |
| 803 | }, |
| 804 | }, |
| 805 | } |
| 806 | |
| 807 | output := &bytes.Buffer{} |
| 808 | cmd.Writer = output |
| 809 | |
| 810 | _ = cmd.Run(buildTestContext(t), []string{"foo", "frobbly", "--help"}) |
| 811 | |
| 812 | expected := `NAME: |
| 813 | foo frobbly |
| 814 | |
| 815 | USAGE: |
| 816 | foo frobbly [options] |
| 817 | |
| 818 | OPTIONS: |
| 819 | --bar string |
| 820 | --help, -h show help |
| 821 | |
| 822 | GLOBAL OPTIONS: |
| 823 | --foo string |
| 824 | ` |
| 825 | |
| 826 | assert.Contains(t, output.String(), expected, "expected output to include global options") |
| 827 | } |
| 828 | |
| 829 | func TestShowSubcommandHelp_GlobalOptions_HideHelpCommand(t *testing.T) { |
| 830 | cmd := &Command{ |
| 831 | Flags: []Flag{ |
| 832 | &StringFlag{ |
| 833 | Name: "foo", |
| 834 | }, |
| 835 | }, |
| 836 | Commands: []*Command{ |
| 837 | { |
| 838 | Name: "frobbly", |
| 839 | HideHelpCommand: true, |
| 840 | Flags: []Flag{ |
| 841 | &StringFlag{ |
| 842 | Name: "bar", |
| 843 | Local: true, |
| 844 | }, |
| 845 | }, |
| 846 | Action: func(context.Context, *Command) error { |
| 847 | return nil |
| 848 | }, |
| 849 | }, |
| 850 | }, |
| 851 | } |
| 852 | |
| 853 | output := &bytes.Buffer{} |
| 854 | cmd.Writer = output |
| 855 | |
| 856 | _ = cmd.Run(buildTestContext(t), []string{"foo", "frobbly", "--help"}) |
| 857 | |
| 858 | expected := `NAME: |
| 859 | foo frobbly |
| 860 | |
| 861 | USAGE: |
| 862 | foo frobbly [options] |
| 863 | |
| 864 | OPTIONS: |
| 865 | --bar string |
| 866 | --help, -h show help |
| 867 | |
| 868 | GLOBAL OPTIONS: |
| 869 | --foo string |
| 870 | ` |
| 871 | |
| 872 | assert.Contains(t, output.String(), expected, "expected output to include global options") |
| 873 | } |
| 874 | |
| 875 | func TestShowSubcommandHelp_SubcommandUsageText(t *testing.T) { |
| 876 | cmd := &Command{ |
| 877 | Commands: []*Command{ |
| 878 | { |
| 879 | Name: "frobbly", |
| 880 | Commands: []*Command{ |
| 881 | { |
| 882 | Name: "bobbly", |
| 883 | UsageText: "this is usage text", |
| 884 | }, |
| 885 | }, |
| 886 | }, |
| 887 | }, |
| 888 | } |
| 889 | |
| 890 | output := &bytes.Buffer{} |
| 891 | cmd.Writer = output |
| 892 | |
| 893 | _ = cmd.Run(buildTestContext(t), []string{"foo", "frobbly", "bobbly", "--help"}) |
| 894 | |
| 895 | assert.Contains(t, output.String(), "this is usage text", |
| 896 | "expected output to include usage text") |
| 897 | } |
| 898 | |
| 899 | func TestShowSubcommandHelp_MultiLine_SubcommandUsageText(t *testing.T) { |
| 900 | cmd := &Command{ |
| 901 | Commands: []*Command{ |
| 902 | { |
| 903 | Name: "frobbly", |
| 904 | Commands: []*Command{ |
| 905 | { |
| 906 | Name: "bobbly", |
| 907 | UsageText: `This is a |
| 908 | multi |
| 909 | line |
| 910 | UsageText`, |
| 911 | }, |
| 912 | }, |
| 913 | }, |
| 914 | }, |
| 915 | } |
| 916 | |
| 917 | output := &bytes.Buffer{} |
| 918 | cmd.Writer = output |
| 919 | |
| 920 | _ = cmd.Run(buildTestContext(t), []string{"foo", "frobbly", "bobbly", "--help"}) |
| 921 | |
| 922 | expected := `USAGE: |
| 923 | This is a |
| 924 | multi |
| 925 | line |
| 926 | UsageText |
| 927 | ` |
| 928 | |
| 929 | assert.Contains(t, output.String(), expected, |
| 930 | "expected output to include usage text") |
| 931 | } |
| 932 | |
| 933 | func TestShowRootCommandHelp_HiddenCommand(t *testing.T) { |
| 934 | cmd := &Command{ |
| 935 | Commands: []*Command{ |
| 936 | { |
| 937 | Name: "frobbly", |
| 938 | Action: func(context.Context, *Command) error { |
| 939 | return nil |
| 940 | }, |
| 941 | }, |
| 942 | { |
| 943 | Name: "secretfrob", |
| 944 | Hidden: true, |
| 945 | Action: func(context.Context, *Command) error { |
| 946 | return nil |
| 947 | }, |
| 948 | }, |
| 949 | }, |
| 950 | } |
| 951 | |
| 952 | output := &bytes.Buffer{} |
| 953 | cmd.Writer = output |
| 954 | |
| 955 | _ = cmd.Run(buildTestContext(t), []string{"app", "--help"}) |
| 956 | |
| 957 | assert.NotContains(t, output.String(), "secretfrob", |
| 958 | "expected output to exclude \"secretfrob\"") |
| 959 | |
| 960 | assert.Contains(t, output.String(), "frobbly", |
| 961 | "expected output to include \"frobbly\"") |
| 962 | } |
| 963 | |
| 964 | func TestShowRootCommandHelp_HelpPrinter(t *testing.T) { |
| 965 | doublecho := func(text string) string { |
| 966 | return text + " " + text |
| 967 | } |
| 968 | |
| 969 | tests := []struct { |
| 970 | name string |
| 971 | template string |
| 972 | printer HelpPrinterFunc |
| 973 | wantTemplate string |
| 974 | wantOutput string |
| 975 | }{ |
| 976 | { |
| 977 | name: "standard-command", |
| 978 | template: "", |
| 979 | printer: func(w io.Writer, _ string, _ any) { |
| 980 | fmt.Fprint(w, "yo") |
| 981 | }, |
| 982 | wantTemplate: RootCommandHelpTemplate, |
| 983 | wantOutput: "yo", |
| 984 | }, |
| 985 | { |
| 986 | name: "custom-template-command", |
| 987 | template: "{{doublecho .Name}}", |
| 988 | printer: func(w io.Writer, templ string, data any) { |
| 989 | // Pass a custom function to ensure it gets used |
| 990 | fm := map[string]any{"doublecho": doublecho} |
| 991 | DefaultPrintHelpCustom(w, templ, data, fm) |
| 992 | }, |
| 993 | wantTemplate: "{{doublecho .Name}}", |
| 994 | wantOutput: "my-app my-app", |
| 995 | }, |
| 996 | } |
| 997 | |
| 998 | for _, tt := range tests { |
| 999 | t.Run(tt.name, func(t *testing.T) { |
| 1000 | defer func(old HelpPrinterFunc) { |
| 1001 | HelpPrinter = old |
| 1002 | }(HelpPrinter) |
| 1003 | HelpPrinter = func(w io.Writer, templ string, data any) { |
| 1004 | assert.Equal(t, tt.wantTemplate, templ, "unexpected template") |
| 1005 | tt.printer(w, templ, data) |
| 1006 | } |
| 1007 | |
| 1008 | var buf bytes.Buffer |
| 1009 | cmd := &Command{ |
| 1010 | Name: "my-app", |
| 1011 | Writer: &buf, |
| 1012 | CustomRootCommandHelpTemplate: tt.template, |
| 1013 | } |
| 1014 | |
| 1015 | err := cmd.Run(buildTestContext(t), []string{"my-app", "help"}) |
| 1016 | require.NoError(t, err) |
| 1017 | |
| 1018 | assert.Equal(t, tt.wantOutput, buf.String()) |
| 1019 | }) |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | func TestShowRootCommandHelp_HelpPrinterCustom(t *testing.T) { |
| 1024 | doublecho := func(text string) string { |
| 1025 | return text + " " + text |
| 1026 | } |
| 1027 | |
| 1028 | tests := []struct { |
| 1029 | name string |
| 1030 | template string |
| 1031 | printer HelpPrinterCustomFunc |
| 1032 | wantTemplate string |
| 1033 | wantOutput string |
| 1034 | }{ |
| 1035 | { |
| 1036 | name: "standard-command", |
| 1037 | template: "", |
| 1038 | printer: func(w io.Writer, _ string, _ any, _ map[string]any) { |
| 1039 | fmt.Fprint(w, "yo") |
| 1040 | }, |
| 1041 | wantTemplate: RootCommandHelpTemplate, |
| 1042 | wantOutput: "yo", |
| 1043 | }, |
| 1044 | { |
| 1045 | name: "custom-template-command", |
| 1046 | template: "{{doublecho .Name}}", |
| 1047 | printer: func(w io.Writer, templ string, data any, _ map[string]any) { |
| 1048 | // Pass a custom function to ensure it gets used |
| 1049 | fm := map[string]any{"doublecho": doublecho} |
| 1050 | DefaultPrintHelpCustom(w, templ, data, fm) |
| 1051 | }, |
| 1052 | wantTemplate: "{{doublecho .Name}}", |
| 1053 | wantOutput: "my-app my-app", |
| 1054 | }, |
| 1055 | } |
| 1056 | |
| 1057 | for _, tt := range tests { |
| 1058 | t.Run(tt.name, func(t *testing.T) { |
| 1059 | defer func(old HelpPrinterCustomFunc) { |
| 1060 | HelpPrinterCustom = old |
| 1061 | }(HelpPrinterCustom) |
| 1062 | HelpPrinterCustom = func(w io.Writer, templ string, data any, fm map[string]any) { |
| 1063 | assert.Nil(t, fm, "unexpected function map passed") |
| 1064 | assert.Equal(t, tt.wantTemplate, templ, "unexpected template") |
| 1065 | tt.printer(w, templ, data, fm) |
| 1066 | } |
| 1067 | |
| 1068 | var buf bytes.Buffer |
| 1069 | cmd := &Command{ |
| 1070 | Name: "my-app", |
| 1071 | Writer: &buf, |
| 1072 | CustomRootCommandHelpTemplate: tt.template, |
| 1073 | } |
| 1074 | |
| 1075 | err := cmd.Run(buildTestContext(t), []string{"my-app", "help"}) |
| 1076 | require.NoError(t, err) |
| 1077 | assert.Equal(t, tt.wantOutput, buf.String()) |
| 1078 | }) |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | func TestShowRootCommandHelp_CustomAppTemplate(t *testing.T) { |
| 1083 | cmd := &Command{ |
| 1084 | Commands: []*Command{ |
| 1085 | { |
| 1086 | Name: "frobbly", |
| 1087 | Action: func(context.Context, *Command) error { |
| 1088 | return nil |
| 1089 | }, |
| 1090 | }, |
| 1091 | { |
| 1092 | Name: "secretfrob", |
| 1093 | Hidden: true, |
| 1094 | Action: func(context.Context, *Command) error { |
| 1095 | return nil |
| 1096 | }, |
| 1097 | }, |
| 1098 | }, |
| 1099 | ExtraInfo: func() map[string]string { |
| 1100 | platform := fmt.Sprintf("OS: %s | Arch: %s", runtime.GOOS, runtime.GOARCH) |
| 1101 | goruntime := fmt.Sprintf("Version: %s | CPUs: %d", runtime.Version(), runtime.NumCPU()) |
| 1102 | return map[string]string{ |
| 1103 | "PLATFORM": platform, |
| 1104 | "RUNTIME": goruntime, |
| 1105 | } |
| 1106 | }, |
| 1107 | CustomRootCommandHelpTemplate: `NAME: |
| 1108 | {{.Name}} - {{.Usage}} |
| 1109 | |
| 1110 | USAGE: |
| 1111 | {{.Name}} {{if .VisibleFlags}}[FLAGS] {{end}}COMMAND{{if .VisibleFlags}} [COMMAND FLAGS | -h]{{end}} [ARGUMENTS...] |
| 1112 | |
| 1113 | COMMANDS: |
| 1114 | {{range .VisibleCommands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} |
| 1115 | {{end}}{{if .VisibleFlags}} |
| 1116 | GLOBAL FLAGS: |
| 1117 | {{range .VisibleFlags}}{{.}} |
| 1118 | {{end}}{{end}} |
| 1119 | VERSION: |
| 1120 | 2.0.0 |
| 1121 | {{"\n"}}{{range $key, $value := ExtraInfo}} |
| 1122 | {{$key}}: |
| 1123 | {{$value}} |
| 1124 | {{end}}`, |
| 1125 | } |
| 1126 | |
| 1127 | output := &bytes.Buffer{} |
| 1128 | cmd.Writer = output |
| 1129 | |
| 1130 | _ = cmd.Run(buildTestContext(t), []string{"app", "--help"}) |
| 1131 | |
| 1132 | assert.NotContains(t, output.String(), "secretfrob", "expected output to exclude \"secretfrob\"") |
| 1133 | assert.Contains(t, output.String(), "frobbly", "expected output to include \"frobbly\"") |
| 1134 | |
| 1135 | if !strings.Contains(output.String(), "PLATFORM:") || |
| 1136 | !strings.Contains(output.String(), "OS:") || |
| 1137 | !strings.Contains(output.String(), "Arch:") { |
| 1138 | t.Errorf("expected output to include \"PLATFORM:, OS: and Arch:\"; got: %q", output.String()) |
| 1139 | } |
| 1140 | |
| 1141 | if !strings.Contains(output.String(), "RUNTIME:") || |
| 1142 | !strings.Contains(output.String(), "Version:") || |
| 1143 | !strings.Contains(output.String(), "CPUs:") { |
| 1144 | t.Errorf("expected output to include \"RUNTIME:, Version: and CPUs:\"; got: %q", output.String()) |
| 1145 | } |
| 1146 | |
| 1147 | if !strings.Contains(output.String(), "VERSION:") || |
| 1148 | !strings.Contains(output.String(), "2.0.0") { |
| 1149 | t.Errorf("expected output to include \"VERSION:, 2.0.0\"; got: %q", output.String()) |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | func TestShowRootCommandHelp_UsageText(t *testing.T) { |
| 1154 | cmd := &Command{ |
| 1155 | UsageText: "This is a single line of UsageText", |
| 1156 | Commands: []*Command{ |
| 1157 | { |
| 1158 | Name: "frobbly", |
| 1159 | }, |
| 1160 | }, |
| 1161 | } |
| 1162 | |
| 1163 | output := &bytes.Buffer{} |
| 1164 | cmd.Writer = output |
| 1165 | |
| 1166 | _ = cmd.Run(buildTestContext(t), []string{"foo"}) |
| 1167 | |
| 1168 | assert.Contains(t, output.String(), "This is a single line of UsageText", "expected output to include usage text") |
| 1169 | } |
| 1170 | |
| 1171 | func TestShowRootCommandHelp_MultiLine_UsageText(t *testing.T) { |
| 1172 | cmd := &Command{ |
| 1173 | UsageText: `This is a |
| 1174 | multi |
| 1175 | line |
| 1176 | App UsageText`, |
| 1177 | Commands: []*Command{ |
| 1178 | { |
| 1179 | Name: "frobbly", |
| 1180 | }, |
| 1181 | }, |
| 1182 | } |
| 1183 | |
| 1184 | output := &bytes.Buffer{} |
| 1185 | cmd.Writer = output |
| 1186 | |
| 1187 | _ = cmd.Run(buildTestContext(t), []string{"foo"}) |
| 1188 | |
| 1189 | expected := `USAGE: |
| 1190 | This is a |
| 1191 | multi |
| 1192 | line |
| 1193 | App UsageText |
| 1194 | ` |
| 1195 | |
| 1196 | assert.Contains(t, output.String(), expected, "expected output to include usage text") |
| 1197 | } |
| 1198 | |
| 1199 | func TestShowRootCommandHelp_CommandMultiLine_UsageText(t *testing.T) { |
| 1200 | cmd := &Command{ |
| 1201 | UsageText: `This is a |
| 1202 | multi |
| 1203 | line |
| 1204 | App UsageText`, |
| 1205 | Commands: []*Command{ |
| 1206 | { |
| 1207 | Name: "frobbly", |
| 1208 | Aliases: []string{"frb1", "frbb2", "frl2"}, |
| 1209 | Usage: "this is a long help output for the run command, long usage \noutput, long usage output, long usage output, long usage output\noutput, long usage output, long usage output", |
| 1210 | }, |
| 1211 | { |
| 1212 | Name: "grobbly", |
| 1213 | Aliases: []string{"grb1", "grbb2"}, |
| 1214 | Usage: "this is another long help output for the run command, long usage \noutput, long usage output", |
| 1215 | }, |
| 1216 | }, |
| 1217 | } |
| 1218 | |
| 1219 | output := &bytes.Buffer{} |
| 1220 | cmd.Writer = output |
| 1221 | |
| 1222 | _ = cmd.Run(buildTestContext(t), []string{"foo"}) |
| 1223 | |
| 1224 | expected := "COMMANDS:\n" + |
| 1225 | " frobbly, frb1, frbb2, frl2 this is a long help output for the run command, long usage \n" + |
| 1226 | " output, long usage output, long usage output, long usage output\n" + |
| 1227 | " output, long usage output, long usage output\n" + |
| 1228 | " grobbly, grb1, grbb2 this is another long help output for the run command, long usage \n" + |
| 1229 | " output, long usage output" |
| 1230 | assert.Contains(t, output.String(), expected, "expected output to include usage text") |
| 1231 | } |
| 1232 | |
| 1233 | func TestHideHelpCommand(t *testing.T) { |
| 1234 | cmd := &Command{ |
| 1235 | HideHelpCommand: true, |
| 1236 | Writer: io.Discard, |
| 1237 | } |
| 1238 | |
| 1239 | err := cmd.Run(buildTestContext(t), []string{"foo", "help"}) |
| 1240 | require.ErrorContains(t, err, "No help topic for 'help'") |
| 1241 | |
| 1242 | err = cmd.Run(buildTestContext(t), []string{"foo", "--help"}) |
| 1243 | assert.NoError(t, err) |
| 1244 | } |
| 1245 | |
| 1246 | func TestHideHelpCommand_False(t *testing.T) { |
| 1247 | cmd := &Command{ |
| 1248 | HideHelpCommand: false, |
| 1249 | Writer: io.Discard, |
| 1250 | } |
| 1251 | |
| 1252 | err := cmd.Run(buildTestContext(t), []string{"foo", "help"}) |
| 1253 | assert.NoError(t, err) |
| 1254 | |
| 1255 | err = cmd.Run(buildTestContext(t), []string{"foo", "--help"}) |
| 1256 | assert.NoError(t, err) |
| 1257 | } |
| 1258 | |
| 1259 | func TestHideHelpCommand_WithHideHelp(t *testing.T) { |
| 1260 | cmd := &Command{ |
| 1261 | HideHelp: true, // effective (hides both command and flag) |
| 1262 | HideHelpCommand: true, // ignored |
| 1263 | Writer: io.Discard, |
| 1264 | } |
| 1265 | |
| 1266 | err := cmd.Run(buildTestContext(t), []string{"foo", "help"}) |
| 1267 | require.ErrorContains(t, err, "No help topic for 'help'") |
| 1268 | |
| 1269 | err = cmd.Run(buildTestContext(t), []string{"foo", "--help"}) |
| 1270 | require.ErrorContains(t, err, providedButNotDefinedErrMsg) |
| 1271 | } |
| 1272 | |
| 1273 | func TestHideHelpCommand_WithSubcommands(t *testing.T) { |
| 1274 | cmd := &Command{ |
| 1275 | HideHelpCommand: true, |
| 1276 | Commands: []*Command{ |
| 1277 | { |
| 1278 | Name: "nully", |
| 1279 | Commands: []*Command{ |
| 1280 | { |
| 1281 | Name: "nully2", |
| 1282 | }, |
| 1283 | }, |
| 1284 | }, |
| 1285 | }, |
| 1286 | } |
| 1287 | |
| 1288 | r := require.New(t) |
| 1289 | |
| 1290 | r.ErrorContains(cmd.Run(buildTestContext(t), []string{"cli.test", "help"}), "No help topic for 'help'") |
| 1291 | r.NoError(cmd.Run(buildTestContext(t), []string{"cli.test", "--help"})) |
| 1292 | } |
| 1293 | |
| 1294 | func TestDefaultCompleteWithFlags(t *testing.T) { |
| 1295 | origArgv := os.Args |
| 1296 | t.Cleanup(func() { os.Args = origArgv }) |
| 1297 | |
| 1298 | for _, tc := range []struct { |
| 1299 | name string |
| 1300 | cmd *Command |
| 1301 | argv []string |
| 1302 | env map[string]string |
| 1303 | expected string |
| 1304 | }{ |
| 1305 | { |
| 1306 | name: "empty", |
| 1307 | cmd: &Command{}, |
| 1308 | argv: []string{"prog", "cmd"}, |
| 1309 | env: map[string]string{"SHELL": "bash"}, |
| 1310 | expected: "", |
| 1311 | }, |
| 1312 | { |
| 1313 | name: "typical-flag-suggestion", |
| 1314 | cmd: &Command{ |
| 1315 | Flags: []Flag{ |
| 1316 | &BoolFlag{Name: "excitement"}, |
| 1317 | &StringFlag{Name: "hat-shape"}, |
| 1318 | }, |
| 1319 | parent: &Command{ |
| 1320 | Name: "cmd", |
| 1321 | Flags: []Flag{ |
| 1322 | &BoolFlag{Name: "happiness"}, |
| 1323 | &Int64Flag{Name: "everybody-jump-on"}, |
| 1324 | }, |
| 1325 | Commands: []*Command{ |
| 1326 | {Name: "putz"}, |
| 1327 | }, |
| 1328 | }, |
| 1329 | }, |
| 1330 | argv: []string{"cmd", "--e", completionFlag}, |
| 1331 | env: map[string]string{"SHELL": "bash"}, |
| 1332 | expected: "--excitement\n", |
| 1333 | }, |
| 1334 | { |
| 1335 | name: "typical-flag-suggestion-hidden-bool", |
| 1336 | cmd: &Command{ |
| 1337 | Flags: []Flag{ |
| 1338 | &BoolFlag{Name: "excitement", Hidden: true}, |
| 1339 | &StringFlag{Name: "hat-shape"}, |
| 1340 | }, |
| 1341 | parent: &Command{ |
| 1342 | Name: "cmd", |
| 1343 | Flags: []Flag{ |
| 1344 | &BoolFlag{Name: "happiness"}, |
| 1345 | &Int64Flag{Name: "everybody-jump-on"}, |
| 1346 | }, |
| 1347 | Commands: []*Command{ |
| 1348 | {Name: "putz"}, |
| 1349 | }, |
| 1350 | }, |
| 1351 | }, |
| 1352 | argv: []string{"cmd", "--e", completionFlag}, |
| 1353 | env: map[string]string{"SHELL": "bash"}, |
| 1354 | expected: "", |
| 1355 | }, |
| 1356 | { |
| 1357 | name: "flag-suggestion-double-dash-shows-all-flags", |
| 1358 | cmd: &Command{ |
| 1359 | Flags: []Flag{ |
| 1360 | &BoolFlag{Name: "excitement"}, |
| 1361 | &StringFlag{Name: "hat-shape"}, |
| 1362 | }, |
| 1363 | parent: &Command{ |
| 1364 | Name: "cmd", |
| 1365 | Flags: []Flag{ |
| 1366 | &BoolFlag{Name: "happiness"}, |
| 1367 | &Int64Flag{Name: "everybody-jump-on"}, |
| 1368 | }, |
| 1369 | Commands: []*Command{ |
| 1370 | {Name: "putz"}, |
| 1371 | }, |
| 1372 | }, |
| 1373 | }, |
| 1374 | argv: []string{"cmd", "--e", "--", completionFlag}, |
| 1375 | env: map[string]string{"SHELL": "bash"}, |
| 1376 | expected: "--excitement\n--hat-shape\n", |
| 1377 | }, |
| 1378 | { |
| 1379 | name: "typical-command-suggestion", |
| 1380 | cmd: &Command{ |
| 1381 | Name: "putz", |
| 1382 | Commands: []*Command{ |
| 1383 | {Name: "futz"}, |
| 1384 | }, |
| 1385 | Flags: []Flag{ |
| 1386 | &BoolFlag{Name: "excitement"}, |
| 1387 | &StringFlag{Name: "hat-shape"}, |
| 1388 | }, |
| 1389 | parent: &Command{ |
| 1390 | Name: "cmd", |
| 1391 | Flags: []Flag{ |
| 1392 | &BoolFlag{Name: "happiness"}, |
| 1393 | &Int64Flag{Name: "everybody-jump-on"}, |
| 1394 | }, |
| 1395 | }, |
| 1396 | }, |
| 1397 | argv: []string{"cmd", completionFlag}, |
| 1398 | env: map[string]string{"SHELL": "bash"}, |
| 1399 | expected: "futz\n", |
| 1400 | }, |
| 1401 | { |
| 1402 | name: "autocomplete-with-spaces", |
| 1403 | cmd: &Command{ |
| 1404 | Name: "putz", |
| 1405 | Commands: []*Command{ |
| 1406 | {Name: "help"}, |
| 1407 | }, |
| 1408 | Flags: []Flag{ |
| 1409 | &BoolFlag{Name: "excitement"}, |
| 1410 | &StringFlag{Name: "hat-shape"}, |
| 1411 | }, |
| 1412 | parent: &Command{ |
| 1413 | Name: "cmd", |
| 1414 | Flags: []Flag{ |
| 1415 | &BoolFlag{Name: "happiness"}, |
| 1416 | &Int64Flag{Name: "everybody-jump-on"}, |
| 1417 | }, |
| 1418 | }, |
| 1419 | }, |
| 1420 | argv: []string{"cmd", "--url", "http://localhost:8000", "h", completionFlag}, |
| 1421 | env: map[string]string{"SHELL": "bash"}, |
| 1422 | expected: "help\n", |
| 1423 | }, |
| 1424 | { |
| 1425 | name: "zsh-autocomplete-with-flag-descriptions", |
| 1426 | cmd: &Command{ |
| 1427 | Name: "putz", |
| 1428 | Flags: []Flag{ |
| 1429 | &BoolFlag{Name: "excitement", Usage: "an exciting flag"}, |
| 1430 | &StringFlag{Name: "hat-shape"}, |
| 1431 | }, |
| 1432 | parent: &Command{ |
| 1433 | Name: "cmd", |
| 1434 | Flags: []Flag{ |
| 1435 | &BoolFlag{Name: "happiness"}, |
| 1436 | &Int64Flag{Name: "everybody-jump-on"}, |
| 1437 | }, |
| 1438 | }, |
| 1439 | }, |
| 1440 | argv: []string{"cmd", "putz", "-e", completionFlag}, |
| 1441 | env: map[string]string{"SHELL": "zsh"}, |
| 1442 | expected: "--excitement:an exciting flag\n", |
| 1443 | }, |
| 1444 | { |
| 1445 | name: "zsh-autocomplete-with-empty-flag-descriptions", |
| 1446 | cmd: &Command{ |
| 1447 | Name: "putz", |
| 1448 | Flags: []Flag{ |
| 1449 | &BoolFlag{Name: "excitement"}, |
| 1450 | &StringFlag{Name: "hat-shape"}, |
| 1451 | }, |
| 1452 | parent: &Command{ |
| 1453 | Name: "cmd", |
| 1454 | Flags: []Flag{ |
| 1455 | &BoolFlag{Name: "happiness"}, |
| 1456 | &Int64Flag{Name: "everybody-jump-on"}, |
| 1457 | }, |
| 1458 | }, |
| 1459 | }, |
| 1460 | argv: []string{"cmd", "putz", "-e", completionFlag}, |
| 1461 | env: map[string]string{"SHELL": "zsh"}, |
| 1462 | expected: "--excitement\n", |
| 1463 | }, |
| 1464 | } { |
| 1465 | t.Run(tc.name, func(ct *testing.T) { |
| 1466 | writer := &bytes.Buffer{} |
| 1467 | rootCmd := tc.cmd.Root() |
| 1468 | rootCmd.Writer = writer |
| 1469 | |
| 1470 | os.Args = tc.argv |
| 1471 | for k, v := range tc.env { |
| 1472 | ct.Setenv(k, v) |
| 1473 | } |
| 1474 | tc.cmd.parsedArgs = &stringSliceArgs{ |
| 1475 | tc.argv[1:], |
| 1476 | } |
| 1477 | f := DefaultCompleteWithFlags |
| 1478 | f(buildTestContext(ct), tc.cmd) |
| 1479 | |
| 1480 | written := writer.String() |
| 1481 | |
| 1482 | assert.Equal(ct, tc.expected, written, "written help does not match") |
| 1483 | }) |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | func TestMutuallyExclusiveFlags(t *testing.T) { |
| 1488 | writer := &bytes.Buffer{} |
| 1489 | cmd := &Command{ |
| 1490 | Name: "cmd", |
| 1491 | Writer: writer, |
| 1492 | MutuallyExclusiveFlags: []MutuallyExclusiveFlags{ |
| 1493 | { |
| 1494 | Flags: [][]Flag{ |
| 1495 | { |
| 1496 | &StringFlag{ |
| 1497 | Name: "s1", |
| 1498 | }, |
| 1499 | }, |
| 1500 | }, |
| 1501 | }, |
| 1502 | }, |
| 1503 | } |
| 1504 | |
| 1505 | _ = ShowRootCommandHelp(cmd) |
| 1506 | |
| 1507 | assert.Contains(t, writer.String(), "--s1", "written help does not include mutex flag") |
| 1508 | } |
| 1509 | |
| 1510 | func TestWrap(t *testing.T) { |
| 1511 | emptywrap := wrap("", 4, 16) |
| 1512 | assert.Empty(t, emptywrap, "Wrapping empty line should return empty line") |
| 1513 | } |
| 1514 | |
| 1515 | func TestWrappedHelp(t *testing.T) { |
| 1516 | // Reset HelpPrinter after this test. |
| 1517 | defer func(old HelpPrinterFunc) { |
| 1518 | HelpPrinter = old |
| 1519 | }(HelpPrinter) |
| 1520 | |
| 1521 | output := new(bytes.Buffer) |
| 1522 | cmd := &Command{ |
| 1523 | Writer: output, |
| 1524 | Flags: []Flag{ |
| 1525 | &BoolFlag{ |
| 1526 | Name: "foo", |
| 1527 | Aliases: []string{"h"}, |
| 1528 | Usage: "here's a really long help text line, let's see where it wraps. blah blah blah and so on.", |
| 1529 | }, |
| 1530 | }, |
| 1531 | Usage: "here's a sample App.Usage string long enough that it should be wrapped in this test", |
| 1532 | UsageText: "i'm not sure how App.UsageText differs from App.Usage, but this should also be wrapped in this test", |
| 1533 | // TODO: figure out how to make ArgsUsage appear in the help text, and test that |
| 1534 | Description: `here's a sample App.Description string long enough that it should be wrapped in this test |
| 1535 | |
| 1536 | with a newline |
| 1537 | and an indented line`, |
| 1538 | Copyright: `Here's a sample copyright text string long enough that it should be wrapped. |
| 1539 | Including newlines. |
| 1540 | And also indented lines. |
| 1541 | |
| 1542 | |
| 1543 | And then another long line. Blah blah blah does anybody ever read these things?`, |
| 1544 | } |
| 1545 | |
| 1546 | HelpPrinter = func(w io.Writer, templ string, data any) { |
| 1547 | funcMap := map[string]any{ |
| 1548 | "wrapAt": func() int { |
| 1549 | return 30 |
| 1550 | }, |
| 1551 | } |
| 1552 | |
| 1553 | HelpPrinterCustom(w, templ, data, funcMap) |
| 1554 | } |
| 1555 | |
| 1556 | _ = ShowRootCommandHelp(cmd) |
| 1557 | |
| 1558 | expected := `NAME: |
| 1559 | - here's a sample |
| 1560 | App.Usage string long |
| 1561 | enough that it should be |
| 1562 | wrapped in this test |
| 1563 | |
| 1564 | USAGE: |
| 1565 | i'm not sure how |
| 1566 | App.UsageText differs from |
| 1567 | App.Usage, but this should |
| 1568 | also be wrapped in this |
| 1569 | test |
| 1570 | |
| 1571 | DESCRIPTION: |
| 1572 | here's a sample |
| 1573 | App.Description string long |
| 1574 | enough that it should be |
| 1575 | wrapped in this test |
| 1576 | |
| 1577 | with a newline |
| 1578 | and an indented line |
| 1579 | |
| 1580 | GLOBAL OPTIONS: |
| 1581 | --foo, -h here's a |
| 1582 | really long help text |
| 1583 | line, let's see where it |
| 1584 | wraps. blah blah blah |
| 1585 | and so on. |
| 1586 | |
| 1587 | COPYRIGHT: |
| 1588 | Here's a sample copyright |
| 1589 | text string long enough |
| 1590 | that it should be wrapped. |
| 1591 | Including newlines. |
| 1592 | And also indented lines. |
| 1593 | |
| 1594 | |
| 1595 | And then another long line. |
| 1596 | Blah blah blah does anybody |
| 1597 | ever read these things? |
| 1598 | ` |
| 1599 | |
| 1600 | assert.Equal(t, expected, output.String(), "Unexpected wrapping") |
| 1601 | } |
| 1602 | |
| 1603 | func TestWrappedCommandHelp(t *testing.T) { |
| 1604 | // Reset HelpPrinter after this test. |
| 1605 | defer func(old HelpPrinterFunc) { |
| 1606 | HelpPrinter = old |
| 1607 | }(HelpPrinter) |
| 1608 | |
| 1609 | output := &bytes.Buffer{} |
| 1610 | cmd := &Command{ |
| 1611 | Writer: output, |
| 1612 | ErrWriter: output, |
| 1613 | Commands: []*Command{ |
| 1614 | { |
| 1615 | Name: "add", |
| 1616 | Aliases: []string{"a"}, |
| 1617 | Usage: "add a task to the list", |
| 1618 | UsageText: "this is an even longer way of describing adding a task to the list", |
| 1619 | Description: "and a description long enough to wrap in this test case", |
| 1620 | Action: func(context.Context, *Command) error { |
| 1621 | return nil |
| 1622 | }, |
| 1623 | }, |
| 1624 | }, |
| 1625 | } |
| 1626 | cmd.setupDefaults([]string{"cli.test"}) |
| 1627 | cmd.setupCommandGraph() |
| 1628 | |
| 1629 | HelpPrinter = func(w io.Writer, templ string, data any) { |
| 1630 | funcMap := map[string]any{ |
| 1631 | "wrapAt": func() int { |
| 1632 | return 30 |
| 1633 | }, |
| 1634 | } |
| 1635 | |
| 1636 | HelpPrinterCustom(w, templ, data, funcMap) |
| 1637 | } |
| 1638 | |
| 1639 | r := require.New(t) |
| 1640 | |
| 1641 | r.NoError(ShowCommandHelp(context.Background(), cmd, "add")) |
| 1642 | r.Equal(`NAME: |
| 1643 | cli.test add - add a task |
| 1644 | to the list |
| 1645 | |
| 1646 | USAGE: |
| 1647 | this is an even longer way |
| 1648 | of describing adding a task |
| 1649 | to the list |
| 1650 | |
| 1651 | DESCRIPTION: |
| 1652 | and a description long |
| 1653 | enough to wrap in this test |
| 1654 | case |
| 1655 | |
| 1656 | OPTIONS: |
| 1657 | --help, -h show help |
| 1658 | `, |
| 1659 | output.String(), |
| 1660 | ) |
| 1661 | } |
| 1662 | |
| 1663 | func TestWrappedSubcommandHelp(t *testing.T) { |
| 1664 | // Reset HelpPrinter after this test. |
| 1665 | defer func(old HelpPrinterFunc) { |
| 1666 | HelpPrinter = old |
| 1667 | }(HelpPrinter) |
| 1668 | |
| 1669 | output := new(bytes.Buffer) |
| 1670 | cmd := &Command{ |
| 1671 | Name: "cli.test", |
| 1672 | Writer: output, |
| 1673 | Commands: []*Command{ |
| 1674 | { |
| 1675 | Name: "bar", |
| 1676 | Aliases: []string{"a"}, |
| 1677 | Usage: "add a task to the list", |
| 1678 | UsageText: "this is an even longer way of describing adding a task to the list", |
| 1679 | Description: "and a description long enough to wrap in this test case", |
| 1680 | Action: func(context.Context, *Command) error { |
| 1681 | return nil |
| 1682 | }, |
| 1683 | Commands: []*Command{ |
| 1684 | { |
| 1685 | Name: "grok", |
| 1686 | Usage: "remove an existing template", |
| 1687 | UsageText: "longer usage text goes here, la la la, hopefully this is long enough to wrap even more", |
| 1688 | Action: func(context.Context, *Command) error { |
| 1689 | return nil |
| 1690 | }, |
| 1691 | }, |
| 1692 | }, |
| 1693 | }, |
| 1694 | }, |
| 1695 | } |
| 1696 | |
| 1697 | HelpPrinter = func(w io.Writer, templ string, data any) { |
| 1698 | funcMap := map[string]any{ |
| 1699 | "wrapAt": func() int { |
| 1700 | return 30 |
| 1701 | }, |
| 1702 | } |
| 1703 | |
| 1704 | HelpPrinterCustom(w, templ, data, funcMap) |
| 1705 | } |
| 1706 | |
| 1707 | _ = cmd.Run(buildTestContext(t), []string{"foo", "bar", "grok", "--help"}) |
| 1708 | |
| 1709 | expected := `NAME: |
| 1710 | cli.test bar grok - remove |
| 1711 | an |
| 1712 | existing |
| 1713 | template |
| 1714 | |
| 1715 | USAGE: |
| 1716 | longer usage text goes |
| 1717 | here, la la la, hopefully |
| 1718 | this is long enough to wrap |
| 1719 | even more |
| 1720 | |
| 1721 | OPTIONS: |
| 1722 | --help, -h show help |
| 1723 | ` |
| 1724 | |
| 1725 | assert.Equal(t, expected, output.String(), "Unexpected wrapping") |
| 1726 | } |
| 1727 | |
| 1728 | func TestWrappedHelpSubcommand(t *testing.T) { |
| 1729 | // Reset HelpPrinter after this test. |
| 1730 | defer func(old HelpPrinterFunc) { |
| 1731 | HelpPrinter = old |
| 1732 | }(HelpPrinter) |
| 1733 | |
| 1734 | output := &bytes.Buffer{} |
| 1735 | cmd := &Command{ |
| 1736 | Name: "cli.test", |
| 1737 | Writer: output, |
| 1738 | ErrWriter: output, |
| 1739 | Commands: []*Command{ |
| 1740 | { |
| 1741 | Name: "bar", |
| 1742 | Aliases: []string{"a"}, |
| 1743 | Usage: "add a task to the list", |
| 1744 | UsageText: "this is an even longer way of describing adding a task to the list", |
| 1745 | Description: "and a description long enough to wrap in this test case", |
| 1746 | Action: func(context.Context, *Command) error { |
| 1747 | return nil |
| 1748 | }, |
| 1749 | Commands: []*Command{ |
| 1750 | { |
| 1751 | Name: "grok", |
| 1752 | Usage: "remove an existing template", |
| 1753 | UsageText: "longer usage text goes here, la la la, hopefully this is long enough to wrap even more", |
| 1754 | Action: func(context.Context, *Command) error { |
| 1755 | return nil |
| 1756 | }, |
| 1757 | Flags: []Flag{ |
| 1758 | &StringFlag{ |
| 1759 | Name: "test-f", |
| 1760 | Usage: "my test usage", |
| 1761 | }, |
| 1762 | }, |
| 1763 | }, |
| 1764 | }, |
| 1765 | }, |
| 1766 | }, |
| 1767 | } |
| 1768 | |
| 1769 | HelpPrinter = func(w io.Writer, templ string, data any) { |
| 1770 | funcMap := map[string]any{ |
| 1771 | "wrapAt": func() int { |
| 1772 | return 30 |
| 1773 | }, |
| 1774 | } |
| 1775 | |
| 1776 | HelpPrinterCustom(w, templ, data, funcMap) |
| 1777 | } |
| 1778 | |
| 1779 | r := require.New(t) |
| 1780 | |
| 1781 | r.NoError(cmd.Run(buildTestContext(t), []string{"cli.test", "bar", "help", "grok"})) |
| 1782 | r.Equal(`NAME: |
| 1783 | cli.test bar grok - remove |
| 1784 | an |
| 1785 | existing |
| 1786 | template |
| 1787 | |
| 1788 | USAGE: |
| 1789 | longer usage text goes |
| 1790 | here, la la la, hopefully |
| 1791 | this is long enough to wrap |
| 1792 | even more |
| 1793 | |
| 1794 | OPTIONS: |
| 1795 | --test-f string my test |
| 1796 | usage |
| 1797 | --help, -h show help |
| 1798 | `, |
| 1799 | output.String(), |
| 1800 | ) |
| 1801 | } |
| 1802 | |
| 1803 | func TestCategorizedHelp(t *testing.T) { |
| 1804 | // Reset HelpPrinter after this test. |
| 1805 | defer func(old HelpPrinterFunc) { |
| 1806 | HelpPrinter = old |
| 1807 | }(HelpPrinter) |
| 1808 | |
| 1809 | output := new(bytes.Buffer) |
| 1810 | cmd := &Command{ |
| 1811 | Name: "cli.test", |
| 1812 | Writer: output, |
| 1813 | Action: func(context.Context, *Command) error { return nil }, |
| 1814 | Flags: []Flag{ |
| 1815 | &StringFlag{ |
| 1816 | Name: "strd", // no category set |
| 1817 | }, |
| 1818 | &Int64Flag{ |
| 1819 | Name: "intd", |
| 1820 | Aliases: []string{"altd1", "altd2"}, |
| 1821 | Category: "cat1", |
| 1822 | }, |
| 1823 | &BoolWithInverseFlag{ |
| 1824 | Name: "bf", |
| 1825 | // Category: "cat1", |
| 1826 | }, |
| 1827 | }, |
| 1828 | MutuallyExclusiveFlags: []MutuallyExclusiveFlags{ |
| 1829 | { |
| 1830 | Category: "cat1", |
| 1831 | Flags: [][]Flag{ |
| 1832 | { |
| 1833 | &StringFlag{ |
| 1834 | Name: "m1", |
| 1835 | Category: "overridden", |
| 1836 | }, |
| 1837 | }, |
| 1838 | }, |
| 1839 | }, |
| 1840 | { |
| 1841 | Flags: [][]Flag{ |
| 1842 | { |
| 1843 | &StringFlag{ |
| 1844 | Name: "m2", |
| 1845 | Category: "ignored", |
| 1846 | }, |
| 1847 | }, |
| 1848 | }, |
| 1849 | }, |
| 1850 | }, |
| 1851 | } |
| 1852 | |
| 1853 | HelpPrinter = func(w io.Writer, templ string, data any) { |
| 1854 | funcMap := map[string]any{ |
| 1855 | "wrapAt": func() int { |
| 1856 | return 30 |
| 1857 | }, |
| 1858 | } |
| 1859 | |
| 1860 | HelpPrinterCustom(w, templ, data, funcMap) |
| 1861 | } |
| 1862 | |
| 1863 | r := require.New(t) |
| 1864 | r.NoError(cmd.Run(buildTestContext(t), []string{"cli.test", "help"})) |
| 1865 | |
| 1866 | r.Equal(`NAME: |
| 1867 | cli.test - A new cli |
| 1868 | application |
| 1869 | |
| 1870 | USAGE: |
| 1871 | cli.test [global options] |
| 1872 | |
| 1873 | GLOBAL OPTIONS: |
| 1874 | --[no-]bf (default: false) |
| 1875 | --help, -h show help |
| 1876 | --m2 string |
| 1877 | --strd string |
| 1878 | |
| 1879 | cat1 |
| 1880 | |
| 1881 | --intd int, --altd1 int, --altd2 int (default: 0) |
| 1882 | --m1 string |
| 1883 | |
| 1884 | `, output.String()) |
| 1885 | } |
| 1886 | |
| 1887 | func Test_checkShellCompleteFlag(t *testing.T) { |
| 1888 | t.Parallel() |
| 1889 | tests := []struct { |
| 1890 | name string |
| 1891 | cmd *Command |
| 1892 | arguments []string |
| 1893 | wantShellCompletion bool |
| 1894 | wantArgs []string |
| 1895 | }{ |
| 1896 | { |
| 1897 | name: "disable-shell-completion", |
| 1898 | arguments: []string{completionFlag}, |
| 1899 | cmd: &Command{}, |
| 1900 | wantShellCompletion: false, |
| 1901 | wantArgs: []string{completionFlag}, |
| 1902 | }, |
| 1903 | { |
| 1904 | name: "child-disable-shell-completion", |
| 1905 | arguments: []string{completionFlag}, |
| 1906 | cmd: &Command{ |
| 1907 | parent: &Command{}, |
| 1908 | }, |
| 1909 | wantShellCompletion: false, |
| 1910 | wantArgs: []string{completionFlag}, |
| 1911 | }, |
| 1912 | { |
| 1913 | name: "last argument isn't --generate-shell-completion", |
| 1914 | arguments: []string{"foo"}, |
| 1915 | cmd: &Command{ |
| 1916 | EnableShellCompletion: true, |
| 1917 | }, |
| 1918 | wantShellCompletion: false, |
| 1919 | wantArgs: []string{"foo"}, |
| 1920 | }, |
| 1921 | { |
| 1922 | name: "arguments include double dash", |
| 1923 | arguments: []string{"--", "foo", completionFlag}, |
| 1924 | cmd: &Command{ |
| 1925 | EnableShellCompletion: true, |
| 1926 | }, |
| 1927 | wantShellCompletion: false, |
| 1928 | wantArgs: []string{"--", "foo"}, |
| 1929 | }, |
| 1930 | { |
| 1931 | name: "shell completion", |
| 1932 | arguments: []string{"foo", completionFlag}, |
| 1933 | cmd: &Command{ |
| 1934 | EnableShellCompletion: true, |
| 1935 | }, |
| 1936 | wantShellCompletion: true, |
| 1937 | wantArgs: []string{"foo"}, |
| 1938 | }, |
| 1939 | { |
| 1940 | name: "double dash is the token being completed", |
| 1941 | arguments: []string{"foo", "--", completionFlag}, |
| 1942 | cmd: &Command{ |
| 1943 | EnableShellCompletion: true, |
| 1944 | }, |
| 1945 | wantShellCompletion: true, |
| 1946 | wantArgs: []string{"foo", "--"}, |
| 1947 | }, |
| 1948 | } |
| 1949 | |
| 1950 | for _, tt := range tests { |
| 1951 | tt := tt |
| 1952 | t.Run(tt.name, func(t *testing.T) { |
| 1953 | t.Parallel() |
| 1954 | shellCompletion, args := checkShellCompleteFlag(tt.cmd, tt.arguments) |
| 1955 | assert.Equal(t, tt.wantShellCompletion, shellCompletion) |
| 1956 | assert.Equal(t, tt.wantArgs, args) |
| 1957 | }) |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | func TestNIndent(t *testing.T) { |
| 1962 | t.Parallel() |
| 1963 | tests := []struct { |
| 1964 | numSpaces int |
| 1965 | str string |
| 1966 | expected string |
| 1967 | }{ |
| 1968 | { |
| 1969 | numSpaces: 0, |
| 1970 | str: "foo", |
| 1971 | expected: "\nfoo", |
| 1972 | }, |
| 1973 | { |
| 1974 | numSpaces: 0, |
| 1975 | str: "foo\n", |
| 1976 | expected: "\nfoo\n", |
| 1977 | }, |
| 1978 | { |
| 1979 | numSpaces: 2, |
| 1980 | str: "foo", |
| 1981 | expected: "\n foo", |
| 1982 | }, |
| 1983 | { |
| 1984 | numSpaces: 3, |
| 1985 | str: "foo\n", |
| 1986 | expected: "\n foo\n ", |
| 1987 | }, |
| 1988 | } |
| 1989 | for _, test := range tests { |
| 1990 | assert.Equal(t, test.expected, nindent(test.numSpaces, test.str)) |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | func TestTemplateError(t *testing.T) { |
| 1995 | oldew := ErrWriter |
| 1996 | defer func() { ErrWriter = oldew }() |
| 1997 | |
| 1998 | var buf bytes.Buffer |
| 1999 | ErrWriter = &buf |
| 2000 | err := errors.New("some error") |
| 2001 | |
| 2002 | handleTemplateError(err) |
| 2003 | assert.Equal(t, []byte(nil), buf.Bytes()) |
| 2004 | |
| 2005 | t.Setenv("CLI_TEMPLATE_ERROR_DEBUG", "true") |
| 2006 | handleTemplateError(err) |
| 2007 | assert.Contains(t, buf.String(), "CLI TEMPLATE ERROR") |
| 2008 | assert.Contains(t, buf.String(), err.Error()) |
| 2009 | } |
| 2010 | |
| 2011 | func TestCliArgContainsFlag(t *testing.T) { |
| 2012 | tests := []struct { |
| 2013 | name string |
| 2014 | args []string |
| 2015 | contains bool |
| 2016 | }{ |
| 2017 | { |
| 2018 | name: "", |
| 2019 | args: []string{}, |
| 2020 | }, |
| 2021 | { |
| 2022 | name: "f", |
| 2023 | args: []string{}, |
| 2024 | }, |
| 2025 | { |
| 2026 | name: "f", |
| 2027 | args: []string{"g", "foo", "f"}, |
| 2028 | }, |
| 2029 | { |
| 2030 | name: "f", |
| 2031 | args: []string{"-f", "foo", "f"}, |
| 2032 | contains: true, |
| 2033 | }, |
| 2034 | { |
| 2035 | name: "f", |
| 2036 | args: []string{"g", "-f", "f"}, |
| 2037 | contains: true, |
| 2038 | }, |
| 2039 | { |
| 2040 | name: "fh", |
| 2041 | args: []string{"g", "f", "--fh"}, |
| 2042 | contains: true, |
| 2043 | }, |
| 2044 | { |
| 2045 | name: "fhg", |
| 2046 | args: []string{"-fhg", "f", "fh"}, |
| 2047 | }, |
| 2048 | { |
| 2049 | name: "fhg", |
| 2050 | args: []string{"--fhg", "f", "fh"}, |
| 2051 | contains: true, |
| 2052 | }, |
| 2053 | } |
| 2054 | |
| 2055 | for _, test := range tests { |
| 2056 | if test.contains { |
| 2057 | assert.True(t, cliArgContains(test.name, test.args)) |
| 2058 | } else { |
| 2059 | assert.False(t, cliArgContains(test.name, test.args)) |
| 2060 | } |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | func TestCommandHelpSuggest(t *testing.T) { |
| 2065 | cmd := &Command{ |
| 2066 | Suggest: true, |
| 2067 | Commands: []*Command{ |
| 2068 | { |
| 2069 | Name: "putz", |
| 2070 | }, |
| 2071 | }, |
| 2072 | } |
| 2073 | |
| 2074 | cmd.setupDefaults([]string{"foo"}) |
| 2075 | |
| 2076 | err := ShowCommandHelp(context.Background(), cmd, "put") |
| 2077 | assert.ErrorContains(t, err, "No help topic for 'put'. putz") |
| 2078 | } |
| 2079 | |
| 2080 | func TestWrapLine(t *testing.T) { |
| 2081 | assert.Equal(t, " ", wrapLine(" ", 0, 3, " ")) |
| 2082 | } |
| 2083 | |
| 2084 | func TestPrintHelpCustomTemplateError(t *testing.T) { |
| 2085 | tmpls := []*string{ |
| 2086 | &helpNameTemplate, |
| 2087 | &argsTemplate, |
| 2088 | &usageTemplate, |
| 2089 | &descriptionTemplate, |
| 2090 | &visibleCommandTemplate, |
| 2091 | ©rightTemplate, |
| 2092 | &versionTemplate, |
| 2093 | &visibleFlagCategoryTemplate, |
| 2094 | &visibleFlagTemplate, |
| 2095 | &visiblePersistentFlagTemplate, |
| 2096 | &visibleFlagCategoryTemplate, |
| 2097 | &authorsTemplate, |
| 2098 | &visibleCommandCategoryTemplate, |
| 2099 | } |
| 2100 | |
| 2101 | oldErrWriter := ErrWriter |
| 2102 | defer func() { ErrWriter = oldErrWriter }() |
| 2103 | |
| 2104 | t.Setenv("CLI_TEMPLATE_ERROR_DEBUG", "true") |
| 2105 | |
| 2106 | for _, tmpl := range tmpls { |
| 2107 | oldtmpl := *tmpl |
| 2108 | // safety mechanism in case something fails |
| 2109 | defer func(stmpl *string) { *stmpl = oldtmpl }(tmpl) |
| 2110 | |
| 2111 | errBuf := &bytes.Buffer{} |
| 2112 | ErrWriter = errBuf |
| 2113 | buf := &bytes.Buffer{} |
| 2114 | |
| 2115 | *tmpl = "{{junk" |
| 2116 | DefaultPrintHelpCustom(buf, "", "", nil) |
| 2117 | |
| 2118 | assert.Contains(t, errBuf.String(), "CLI TEMPLATE ERROR") |
| 2119 | |
| 2120 | // reset template back. |
| 2121 | *tmpl = oldtmpl |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | func TestCustomUsageCommandHelp(t *testing.T) { |
| 2126 | old := UsageCommandHelp |
| 2127 | defer func() { UsageCommandHelp = old }() |
| 2128 | |
| 2129 | UsageCommandHelp = "Custom usage help command" |
| 2130 | |
| 2131 | out := &bytes.Buffer{} |
| 2132 | cmd := &Command{ |
| 2133 | Name: "app", |
| 2134 | Commands: []*Command{ |
| 2135 | { |
| 2136 | Name: "cmd", |
| 2137 | Commands: []*Command{{Name: "subcmd"}}, |
| 2138 | }, |
| 2139 | }, |
| 2140 | Writer: out, |
| 2141 | ErrWriter: out, |
| 2142 | } |
| 2143 | |
| 2144 | _ = cmd.Run(buildTestContext(t), []string{"app", "help"}) |
| 2145 | assert.Contains(t, out.String(), UsageCommandHelp) |
| 2146 | } |