completion_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package main
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "github.com/spf13/cobra"
  9. intdevice "netdome.biz/paul/qmk-rgb/internal/device"
  10. )
  11. // The tool knows how to resolve --zone, but the shell only offers what the binary
  12. // tells it about, and it told it nothing: the flag was offered, its values were
  13. // not. These cases fix that, and they are worth fixing because --zone and the
  14. // profile names are the two places a user types something the tool already knows.
  15. func TestZoneCompletionOffersSubsystemsAndTheBoardsOwnNames(t *testing.T) {
  16. dir := t.TempDir()
  17. writeDefinition(t, dir, "impact80.json", `{
  18. "name": "Impact 80", "vendorId": "0x36B0", "productId": "0x309F",
  19. "menus": [{"label":"Lighting","content":[
  20. {"label":"logo","content":[{"label":"Effect","type":"dropdown",
  21. "content":["id_qmk_rgblight_effect",2,2],"options":["none"]}]},
  22. {"label":"Backlight","content":[{"label":"Effect","type":"dropdown",
  23. "content":["id_qmk_rgb_matrix_effect",3,2],"options":["none"]}]}]}]}`)
  24. t.Cleanup(forceDefinitionsDir(t, dir))
  25. got, directive := completeZoneNames(nil, nil, "")
  26. if directive != cobra.ShellCompDirectiveNoFileComp {
  27. t.Errorf("directive = %v, want NoFileComp so the shell does not fall back to paths", directive)
  28. }
  29. for _, want := range []string{"backlight", "rgblight", "rgb_matrix", "audio", "led_matrix", "logo", "Backlight"} {
  30. if !contains(got, want) {
  31. t.Errorf("completeZoneNames() = %v, want it to offer %q", got, want)
  32. }
  33. }
  34. // Nothing is offered twice: Backlight is both a label and close to
  35. // backlight, and a list with repeats reads as a bug.
  36. if len(got) != countDistinct(got) {
  37. t.Errorf("completeZoneNames() = %v, want no duplicates", got)
  38. }
  39. }
  40. // The shell is completing as the user types, so a prefix has to narrow the list.
  41. func TestZoneCompletionNarrowsToThePrefix(t *testing.T) {
  42. t.Cleanup(forceDefinitionsDir(t, t.TempDir()))
  43. got, directive := completeZoneNames(nil, nil, "rgb")
  44. if directive != cobra.ShellCompDirectiveNoFileComp {
  45. t.Errorf("directive = %v, want NoFileComp so the shell does not fall back to paths", directive)
  46. }
  47. for _, g := range got {
  48. if !strings.HasPrefix(g, "rgb") {
  49. t.Errorf("completeZoneNames(%q) = %v, want only names with that prefix", "rgb", got)
  50. }
  51. }
  52. if !contains(got, "rgb_matrix") {
  53. t.Errorf("completeZoneNames(\"rgb\") = %v, want rgb_matrix", got)
  54. }
  55. }
  56. // A definition that is not there must not break completion: a shell prints the
  57. // error text, which is worse than offering nothing.
  58. func TestZoneCompletionSurvivesAMissingDataDirectory(t *testing.T) {
  59. t.Cleanup(forceDefinitionsDir(t, filepath.Join(t.TempDir(), "absent")))
  60. got, directive := completeZoneNames(nil, nil, "")
  61. if directive != cobra.ShellCompDirectiveNoFileComp {
  62. t.Errorf("directive = %v, want NoFileComp so the shell does not fall back to paths", directive)
  63. }
  64. if !contains(got, "rgb_matrix") {
  65. t.Errorf("completeZoneNames() = %v, want the QMK subsystem names at least", got)
  66. }
  67. }
  68. // --device takes the numbers keyboard info prints, which come from enumeration
  69. // alone and need no open keyboard.
  70. func TestDeviceCompletionOffersTheConnectedKeyboards(t *testing.T) {
  71. original := discoverAll
  72. discoverAll = func() ([]intdevice.Device, error) {
  73. return []intdevice.Device{{Index: 1}, {Index: 2}}, nil
  74. }
  75. t.Cleanup(func() { discoverAll = original })
  76. got, directive := completeDeviceNumbers(nil, nil, "")
  77. if directive != cobra.ShellCompDirectiveNoFileComp {
  78. t.Errorf("directive = %v, want NoFileComp so the shell does not fall back to paths", directive)
  79. }
  80. if len(got) != 2 || got[0] != "1" || got[1] != "2" {
  81. t.Errorf("completeDeviceNumbers() = %v, want [1 2]", got)
  82. }
  83. }
  84. func TestDeviceCompletionSurvivesADiscoveryFailure(t *testing.T) {
  85. original := discoverAll
  86. discoverAll = func() ([]intdevice.Device, error) { return nil, errors.New("no keyboard") }
  87. t.Cleanup(func() { discoverAll = original })
  88. got, directive := completeDeviceNumbers(nil, nil, "")
  89. if directive != cobra.ShellCompDirectiveNoFileComp {
  90. t.Errorf("directive = %v, want NoFileComp so the shell does not fall back to paths", directive)
  91. }
  92. if len(got) != 0 {
  93. t.Errorf("completeDeviceNumbers() = %v, want nothing when discovery fails", got)
  94. }
  95. }
  96. // load, save and delete take a profile name, and the names in profiles/ are
  97. // readable without a keyboard — which is the point, because the shell is asking
  98. // while nothing is plugged in.
  99. func TestProfileNameCompletionOffersTheFilesInTheDirectory(t *testing.T) {
  100. dir := t.TempDir()
  101. original := profilesDirOverride
  102. profilesDirOverride = dir
  103. t.Cleanup(func() { profilesDirOverride = original })
  104. for _, name := range []string{"paul.json", "default.json", "notes.txt"} {
  105. if err := os.WriteFile(filepath.Join(dir, name), []byte("{}"), 0o600); err != nil {
  106. t.Fatal(err)
  107. }
  108. }
  109. for _, cmd := range []*cobra.Command{NewProfileLoadCmd(), NewProfileDeleteCmd()} {
  110. got, _ := completeProfileNames(cmd, nil, "")
  111. if len(got) != 2 || got[0] != "default" || got[1] != "paul" {
  112. t.Errorf("completeProfileNames() = %v, want [default paul]", got)
  113. }
  114. }
  115. }
  116. func TestProfileNameCompletionSurvivesAMissingDirectory(t *testing.T) {
  117. original := profilesDirOverride
  118. profilesDirOverride = filepath.Join(t.TempDir(), "absent")
  119. t.Cleanup(func() { profilesDirOverride = original })
  120. got, directive := completeProfileNames(nil, nil, "")
  121. if directive != cobra.ShellCompDirectiveNoFileComp {
  122. t.Errorf("directive = %v, want NoFileComp so the shell does not fall back to paths", directive)
  123. }
  124. if len(got) != 0 {
  125. t.Errorf("completeProfileNames() = %v, want nothing", got)
  126. }
  127. }
  128. func contains(list []string, want string) bool {
  129. for _, s := range list {
  130. if s == want {
  131. return true
  132. }
  133. }
  134. return false
  135. }
  136. func countDistinct(list []string) int {
  137. seen := make(map[string]bool, len(list))
  138. for _, s := range list {
  139. seen[s] = true
  140. }
  141. return len(seen)
  142. }