Skip to content

Commit 8a4379c

Browse files
committed
test: add USB host support mocks
1 parent 3c58f59 commit 8a4379c

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Taskfile.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ tasks:
4242
desc: Run tests
4343
cmds:
4444
- echo "Running tests..."
45-
- go test ./... -v
45+
- go test ./...
4646

4747
run:
4848
desc: Run the application locally

cmd/bt-hid-relay/main_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func init() {
3030
return "", fmt.Errorf("device not found")
3131
}
3232
}
33+
34+
// Mock USB host support check
35+
device.CheckUSBHostSupport = func() (bool, bool, error) {
36+
return true, true, nil
37+
}
3338
}
3439

3540
func TestParseFlags(t *testing.T) {
@@ -110,11 +115,13 @@ func TestMain(t *testing.T) {
110115
// Save original values
111116
originalExit := osExit
112117
originalFindInputDevice := device.FindInputDeviceFunc
118+
originalCheckUSBHostSupport := device.CheckUSBHostSupport
113119

114120
// Restore after test
115121
defer func() {
116122
osExit = originalExit
117123
device.FindInputDeviceFunc = originalFindInputDevice
124+
device.CheckUSBHostSupport = originalCheckUSBHostSupport
118125
log.SetOutput(os.Stderr)
119126
}()
120127

@@ -130,6 +137,11 @@ func TestMain(t *testing.T) {
130137
}
131138
}
132139

140+
// Mock USB host support check
141+
device.CheckUSBHostSupport = func() (bool, bool, error) {
142+
return true, true, nil
143+
}
144+
133145
// Mock os.Exit
134146
osExit = func(code int) {
135147
exitCalled = true

internal/device/host_check.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import (
55
"strings"
66
)
77

8+
// CheckUSBHostSupportFunc is a type for the host support check function
9+
type CheckUSBHostSupportFunc func() (bool, bool, error)
10+
11+
// CheckUSBHostSupport is the function variable that can be replaced in tests
12+
var CheckUSBHostSupport CheckUSBHostSupportFunc = checkUSBHostSupport
13+
814
// CheckUSBHostSupport verifies both USB host hardware capability and if it's enabled
9-
func CheckUSBHostSupport() (hasCapability bool, isEnabled bool, err error) {
15+
func checkUSBHostSupport() (hasCapability bool, isEnabled bool, err error) {
1016
// Check hardware capability first
1117
hasCapability = false
1218

0 commit comments

Comments
 (0)