keyboard_info_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. "testing"
  8. intdevice "netdome.biz/paul/qmk-rgb/internal/device"
  9. )
  10. type deviceInfoOutput struct {
  11. Devices []keyboardLine `json:"devices"`
  12. Total int `json:"total"`
  13. }
  14. func stubDiscovery(t *testing.T, devices []intdevice.Device) {
  15. t.Helper()
  16. orig := discoverAll
  17. discoverAll = func() ([]intdevice.Device, error) { return devices, nil }
  18. t.Cleanup(func() { discoverAll = orig })
  19. }
  20. // runRealKeyboardInfo executes the shipped command so a re-added
  21. // human-readable banner is caught, not just changes inside a helper.
  22. func runRealKeyboardInfo(t *testing.T) (stdout, stderr string, err error) {
  23. t.Helper()
  24. var out, errOut bytes.Buffer
  25. keyboardInfoCmd.SetOut(&out)
  26. keyboardInfoCmd.SetErr(&errOut)
  27. // Restored to nil rather than to whatever it was: OutOrStdout() answers
  28. // os.Stdout, and setting that back leaves the command with a writer of its
  29. // own, which a root's SetOut can no longer override. The next test that ran
  30. // this command through a root would then print its help to the terminal.
  31. t.Cleanup(func() {
  32. keyboardInfoCmd.SetOut(nil)
  33. keyboardInfoCmd.SetErr(nil)
  34. })
  35. err = keyboardInfoCmd.RunE(keyboardInfoCmd, nil)
  36. return out.String(), errOut.String(), err
  37. }
  38. func TestKeyboardInfoWritesOnlyJSON(t *testing.T) {
  39. withJSON(t)
  40. stubDiscovery(t, []intdevice.Device{
  41. {Index: 1, Path: "/dev/hidraw7", VendorID: 0x36b0, ProductID: 0x309f, Name: "Impact 80"},
  42. })
  43. stdout, stderr, err := runRealKeyboardInfo(t)
  44. if err != nil {
  45. t.Fatalf("keyboard info returned error: %v", err)
  46. }
  47. if stderr != "" {
  48. t.Errorf("keyboard info wrote %q to stderr, want no human-readable banner next to the JSON", stderr)
  49. }
  50. var got deviceInfoOutput
  51. if err := json.Unmarshal([]byte(stdout), &got); err != nil {
  52. t.Fatalf("stdout is not valid JSON: %v (output %q)", err, stdout)
  53. }
  54. if got.Total != 1 {
  55. t.Errorf("total = %d, want 1", got.Total)
  56. }
  57. if len(got.Devices) != 1 {
  58. t.Fatalf("devices = %d entries, want 1", len(got.Devices))
  59. }
  60. if got.Devices[0].Index != 1 {
  61. t.Errorf("devices[0].index = %d, want 1", got.Devices[0].Index)
  62. }
  63. if got.Devices[0].Path != "/dev/hidraw7" {
  64. t.Errorf("devices[0].path = %q, want %q", got.Devices[0].Path, "/dev/hidraw7")
  65. }
  66. }
  67. func TestKeyboardInfoEmitsIndentedJSON(t *testing.T) {
  68. withJSON(t)
  69. stubDiscovery(t, []intdevice.Device{
  70. {Index: 1, Path: "/dev/hidraw7", Name: "Impact 80"},
  71. })
  72. stdout, _, err := runRealKeyboardInfo(t)
  73. if err != nil {
  74. t.Fatalf("keyboard info returned error: %v", err)
  75. }
  76. if !strings.Contains(stdout, "\n \"devices\": [") {
  77. t.Errorf("stdout = %q, want two-space indented JSON like the other commands emit", stdout)
  78. }
  79. if !strings.HasSuffix(stdout, "\n") {
  80. t.Errorf("stdout = %q, want a trailing newline", stdout)
  81. }
  82. }
  83. func TestKeyboardInfoEmitsEmptyArrayNotNull(t *testing.T) {
  84. withJSON(t)
  85. stubDiscovery(t, nil)
  86. stdout, _, err := runRealKeyboardInfo(t)
  87. if err != nil {
  88. t.Fatalf("keyboard info returned error: %v", err)
  89. }
  90. if !strings.Contains(stdout, `"devices": []`) {
  91. t.Errorf("stdout = %q, want an empty array so consumers can iterate unconditionally", stdout)
  92. }
  93. }
  94. func TestKeyboardInfoPropagatesDiscoveryError(t *testing.T) {
  95. orig := discoverAll
  96. discoverAll = func() ([]intdevice.Device, error) { return nil, errStub }
  97. t.Cleanup(func() { discoverAll = orig })
  98. _, _, err := runRealKeyboardInfo(t)
  99. if err == nil {
  100. t.Fatal("keyboard info expected discovery error, got nil")
  101. }
  102. if !strings.Contains(err.Error(), "enumerate") {
  103. t.Errorf("error = %v, want it to wrap the discovery failure", err)
  104. }
  105. }
  106. var errStub = errors.New("enumerate: stub failure")
  107. // A command that a test gave a writer of its own keeps it, and a writer set that
  108. // way outranks the root's. Restoring os.Stdout rather than nil therefore left the
  109. // shipped `keyboard info` writing to the terminal while a later test believed it
  110. // was capturing its help, and the failure showed up as noise in an unrelated
  111. // test's output rather than as a failing one.
  112. func TestKeyboardInfoHelpGoesWhereTheRootSendsIt(t *testing.T) {
  113. // In a subtest, so the restore has happened by the time the help runs: that
  114. // ordering is the whole defect, and doing both here would not test it.
  115. t.Run("a test gave it a writer", func(t *testing.T) {
  116. runRealKeyboardInfo(t)
  117. })
  118. var out bytes.Buffer
  119. root := newRootCommand()
  120. registerCommands(root)
  121. root.SetOut(&out)
  122. root.SetErr(&out)
  123. root.SetArgs([]string{"keyboard", "info", "--help"})
  124. if err := root.Execute(); err != nil {
  125. t.Fatalf("keyboard info --help returned error: %v", err)
  126. }
  127. if !strings.Contains(out.String(), "Usage:") {
  128. t.Errorf("captured %q, want the help, so the root's writer is in charge", out.String())
  129. }
  130. }