flags_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. )
  7. // The --device flag accepts the 1-based number that `keyboard info` prints.
  8. // Its help text is the only place a user learns that, so it must not
  9. // describe the HID path the flag rejects.
  10. func TestDeviceFlagUsageDescribesANumberNotAPath(t *testing.T) {
  11. flag := newRootCommand().PersistentFlags().Lookup("device")
  12. if flag == nil {
  13. t.Fatal("--device flag not found")
  14. }
  15. if strings.Contains(flag.Usage, "path") {
  16. t.Errorf("--device usage = %q, must not mention a path; the flag rejects HID paths", flag.Usage)
  17. }
  18. if !strings.Contains(flag.Usage, "number") {
  19. t.Errorf("--device usage = %q, want it to say the argument is a number", flag.Usage)
  20. }
  21. }
  22. // Cobra reads a back-quoted word in a flag's usage as the value placeholder
  23. // and renders it where the type name would go, turning `--device into
  24. // `--device keyboard info`. The help must therefore contain no backticks.
  25. func TestFlagUsageHasNoValuePlaceholder(t *testing.T) {
  26. for _, name := range []string{"device", "zone"} {
  27. flag := newRootCommand().PersistentFlags().Lookup(name)
  28. if flag == nil {
  29. t.Fatalf("--%s flag not found", name)
  30. }
  31. if strings.Contains(flag.Usage, "`") {
  32. t.Errorf("--%s usage = %q, backticks make cobra render a bogus value placeholder", name, flag.Usage)
  33. }
  34. }
  35. }
  36. // The flag usage is not the only place a user learns what --device takes. The
  37. // same promise is repeated in three documentation files, and it was wrong in
  38. // all of them at once. Pin the docs to the flag's actual contract by looking
  39. // for a value that looks like a HID path, not for the word "path" — prose
  40. // about why paths are unstable, and the rule that forbids them, must pass.
  41. func TestDocsDoNotOfferAPathValueForDevice(t *testing.T) {
  42. pathLike := []string{"/dev/", "hidraw", `\\?\hid`, "IO/HIDDevice"}
  43. for _, path := range []string{
  44. "../../README.md",
  45. "../../AGENTS.md",
  46. "../../.claude/skills/qmk-rgb/SKILL.md",
  47. } {
  48. data, err := os.ReadFile(path)
  49. if err != nil {
  50. t.Errorf("read %s: %v", path, err)
  51. continue
  52. }
  53. for _, line := range strings.Split(string(data), "\n") {
  54. if !strings.Contains(line, "--device") {
  55. continue
  56. }
  57. for _, needle := range pathLike {
  58. if strings.Contains(line, needle) {
  59. t.Errorf("%s offers a path value for --device: %q", path, strings.TrimSpace(line))
  60. }
  61. }
  62. }
  63. }
  64. }
  65. // The docs must not teach a zone name the resolver rejects. The subsystem
  66. // names are the vocabulary; a board's own channel names come from its definition file.
  67. func TestDocsDoNotTeachAZoneNameTheResolverRejects(t *testing.T) {
  68. for _, path := range []string{
  69. "../../README.md",
  70. "../../AGENTS.md",
  71. "../../.claude/skills/qmk-rgb/SKILL.md",
  72. } {
  73. data, err := os.ReadFile(path)
  74. if err != nil {
  75. t.Errorf("read %s: %v", path, err)
  76. continue
  77. }
  78. body := string(data)
  79. for _, wrong := range []string{"--zone logo|backlight", "--zone matrix", "zone=matrix"} {
  80. if strings.Contains(body, wrong) {
  81. t.Errorf("%s contains %q, want only the canonical zone names", path, wrong)
  82. }
  83. }
  84. }
  85. }
  86. func TestZoneFlagUsageNamesTheChannelVocabulary(t *testing.T) {
  87. flag := newRootCommand().PersistentFlags().Lookup("zone")
  88. if flag == nil {
  89. t.Fatal("--zone flag not found")
  90. }
  91. // Every subsystem name must be discoverable from the help text.
  92. for _, zone := range []string{"backlight", "rgblight", "rgb_matrix", "audio", "led_matrix"} {
  93. if !strings.Contains(flag.Usage, zone) {
  94. t.Errorf("--zone usage = %q, want it to list %q", flag.Usage, zone)
  95. }
  96. }
  97. // The board-supplied names are not a fixed vocabulary, so the help must not
  98. // claim they are.
  99. if strings.Contains(flag.Usage, "logo") || strings.Contains(flag.Usage, "side") {
  100. t.Errorf("--zone usage = %q, must not name channels a board supplies", flag.Usage)
  101. }
  102. }