| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package main
- import (
- "os"
- "strings"
- "testing"
- "github.com/spf13/cobra"
- )
- // The --device flag accepts the 1-based number that `keyboard info` prints.
- // Its help text is the only place a user learns that, so it must not
- // describe the HID path the flag rejects.
- func TestDeviceFlagUsageDescribesANumberNotAPath(t *testing.T) {
- flag := newRootCommand().PersistentFlags().Lookup("device")
- if flag == nil {
- t.Fatal("--device flag not found")
- }
- if strings.Contains(flag.Usage, "path") {
- t.Errorf("--device usage = %q, must not mention a path; the flag rejects HID paths", flag.Usage)
- }
- if !strings.Contains(flag.Usage, "number") {
- t.Errorf("--device usage = %q, want it to say the argument is a number", flag.Usage)
- }
- }
- // Cobra reads a back-quoted word in a flag's usage as the value placeholder
- // and renders it where the type name would go, turning `--device into
- // `--device keyboard info`. The help must therefore contain no backticks.
- func TestFlagUsageHasNoValuePlaceholder(t *testing.T) {
- root := newRootCommand()
- for _, name := range []string{"device", "definition", "json"} {
- flag := root.PersistentFlags().Lookup(name)
- if flag == nil {
- t.Errorf("--%s is registered on the root, want it", name)
- continue
- }
- if strings.Contains(flag.Usage, "`") {
- t.Errorf("--%s usage = %q, backticks make cobra render a bogus value placeholder", name, flag.Usage)
- }
- }
- }
- // The flag usage is not the only place a user learns what --device takes. The
- // same promise is repeated in three documentation files, and it was wrong in
- // all of them at once. Pin the docs to the flag's actual contract by looking
- // for a value that looks like a HID path, not for the word "path" — prose
- // about why paths are unstable, and the rule that forbids them, must pass.
- func TestDocsDoNotOfferAPathValueForDevice(t *testing.T) {
- pathLike := []string{"/dev/", "hidraw", `\\?\hid`, "IO/HIDDevice"}
- for _, path := range []string{
- "../../README.md",
- "../../AGENTS.md",
- "../../.claude/skills/qmk-rgb/SKILL.md",
- } {
- data, err := os.ReadFile(path)
- if err != nil {
- t.Errorf("read %s: %v", path, err)
- continue
- }
- for _, line := range strings.Split(string(data), "\n") {
- if !strings.Contains(line, "--device") {
- continue
- }
- for _, needle := range pathLike {
- if strings.Contains(line, needle) {
- t.Errorf("%s offers a path value for --device: %q", path, strings.TrimSpace(line))
- }
- }
- }
- }
- }
- // The docs must not teach a zone name the resolver rejects. The subsystem
- // names are the vocabulary; a board's own channel names come from its definition file.
- func TestDocsDoNotTeachAZoneNameTheResolverRejects(t *testing.T) {
- for _, path := range []string{
- "../../README.md",
- "../../AGENTS.md",
- "../../.claude/skills/qmk-rgb/SKILL.md",
- } {
- data, err := os.ReadFile(path)
- if err != nil {
- t.Errorf("read %s: %v", path, err)
- continue
- }
- body := string(data)
- for _, wrong := range []string{"zone logo|backlight", "zone matrix", "zone=matrix"} {
- if strings.Contains(body, wrong) {
- t.Errorf("%s contains %q, want only the canonical zone names", path, wrong)
- }
- }
- }
- }
- // The zone is a positional argument now, so its help is the Long text the
- // command carries rather than a flag's usage string. Every subsystem name has to
- // be discoverable there, because a user reading `brightness --help` has nowhere
- // else to learn what a zone may be.
- func TestZoneHelpNamesTheChannelVocabulary(t *testing.T) {
- cmds := map[string]*cobra.Command{
- "effect": NewEffectCmd(),
- "brightness": NewBrightnessCmd(),
- "speed": NewSpeedCmd(),
- "color": NewColorCmd(),
- "enable": NewEnableCmd(),
- "disable": NewDisableCmd(),
- }
- for name, cmd := range cmds {
- t.Run(name, func(t *testing.T) {
- for _, zone := range []string{"backlight", "rgblight", "rgb_matrix", "audio", "led_matrix"} {
- if !strings.Contains(cmd.Long, zone) {
- t.Errorf("%s help does not name the subsystem %q", name, zone)
- }
- }
- if !strings.Contains(cmd.Long, "all") {
- t.Errorf("%s help does not say that all is a zone", name)
- }
- if !strings.Contains(cmd.Long, "comma") {
- t.Errorf("%s help does not say that several zones may be written at once", name)
- }
- // The board-supplied names are not a fixed vocabulary, so the help
- // must not claim they are.
- if strings.Contains(cmd.Long, "logo") || strings.Contains(cmd.Long, "side") {
- t.Errorf("%s help names channels a board supplies", name)
- }
- })
- }
- }
|