flags_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. // The --device flag accepts the 1-based number that `keyboard info` prints.
  7. // Its help text is the only place a user learns that, so it must not
  8. // describe the HID path the flag rejects.
  9. func TestDeviceFlagUsageDescribesANumberNotAPath(t *testing.T) {
  10. flag := newRootCommand().PersistentFlags().Lookup("device")
  11. if flag == nil {
  12. t.Fatal("--device flag not found")
  13. }
  14. if strings.Contains(flag.Usage, "path") {
  15. t.Errorf("--device usage = %q, must not mention a path; the flag rejects HID paths", flag.Usage)
  16. }
  17. if !strings.Contains(flag.Usage, "number") {
  18. t.Errorf("--device usage = %q, want it to say the argument is a number", flag.Usage)
  19. }
  20. }
  21. // Cobra reads a back-quoted word in a flag's usage as the value placeholder
  22. // and renders it where the type name would go, turning `--device into
  23. // `--device keyboard info`. The help must therefore contain no backticks.
  24. func TestFlagUsageHasNoValuePlaceholder(t *testing.T) {
  25. for _, name := range []string{"device", "zone"} {
  26. flag := newRootCommand().PersistentFlags().Lookup(name)
  27. if flag == nil {
  28. t.Fatalf("--%s flag not found", name)
  29. }
  30. if strings.Contains(flag.Usage, "`") {
  31. t.Errorf("--%s usage = %q, backticks make cobra render a bogus value placeholder", name, flag.Usage)
  32. }
  33. }
  34. }
  35. func TestZoneFlagUsageListsEveryZone(t *testing.T) {
  36. flag := newRootCommand().PersistentFlags().Lookup("zone")
  37. if flag == nil {
  38. t.Fatal("--zone flag not found")
  39. }
  40. for _, zone := range []string{"logo", "backlight", "side"} {
  41. if !strings.Contains(flag.Usage, zone) {
  42. t.Errorf("--zone usage = %q, want it to list %q", flag.Usage, zone)
  43. }
  44. }
  45. }