Skip to content

Commit 9cf98f7

Browse files
committed
all: improve error message when CGO_ENABLED=0
1 parent d70377e commit 9cf98f7

File tree

14 files changed

+402
-9
lines changed

14 files changed

+402
-9
lines changed

.github/workflows/hotkey.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ jobs:
4848
stable: 'false'
4949
go-version: '1.17.x'
5050

51-
- name: Run Tests
51+
- name: Run Tests with CGO_ENABLED=1
52+
if: ${{ runner.os == 'Linux' || runner.os == 'macOS'}}
5253
run: |
53-
go test -v -covermode=atomic ./...
54+
CGO_ENABLED=1 go test -v -covermode=atomic .
55+
56+
- name: Run Tests with CGO_ENABLED=0
57+
if: ${{ runner.os == 'Linux' || runner.os == 'macOS'}}
58+
run: |
59+
CGO_ENABLED=0 go test -v -covermode=atomic .
60+
61+
- name: Run Tests on Windows
62+
if: ${{ runner.os == 'Windows'}}
63+
run: |
64+
go test -v -covermode=atomic .

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module golang.design/x/hotkey
33
go 1.17
44

55
require (
6-
golang.design/x/mainthread v0.2.1
6+
golang.design/x/mainthread v0.3.0
77
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8 // indirect
88
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
golang.design/x/mainthread v0.2.1 h1:IUGVW1acDfKoQtFeeS/RD/YYiKK8jxwkJXIQuKuL+ig=
2-
golang.design/x/mainthread v0.2.1/go.mod h1:vYX7cF2b3pTJMGM/hc13NmN6kblKnf4/IyvHeu259L0=
1+
golang.design/x/mainthread v0.3.0 h1:UwFus0lcPodNpMOGoQMe87jSFwbSsEY//CA7yVmu4j8=
2+
golang.design/x/mainthread v0.3.0/go.mod h1:vYX7cF2b3pTJMGM/hc13NmN6kblKnf4/IyvHeu259L0=
33
golang.org/x/sys v0.0.0-20201022201747-fb209a7c41cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
44
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8 h1:de2yTH1xuxjmGB7i6Z5o2z3RCHVa0XlpSZzjd8Fe6bE=
55
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

hotkey.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func newEventChan() (chan<- Event, <-chan Event) {
111111
for len(q) > 0 {
112112
select {
113113
case out <- q[0]:
114+
q[0] = Event{}
114115
q = q[1:]
115116
case e, ok := <-in:
116117
if ok {

hotkey_darwin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// Written by Changkun Ou <changkun.de>
66

7-
//go:build darwin
7+
//go:build darwin && cgo
88

99
package hotkey_test
1010

hotkey_linux.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
#include <X11/Xutil.h>
1212

1313
int displayTest() {
14-
Display* d = XOpenDisplay(0);
14+
Display* d = NULL;
15+
for (int i = 0; i < 42; i++) {
16+
d = XOpenDisplay(0);
17+
if (d == NULL) continue;
18+
break;
19+
}
1520
if (d == NULL) {
1621
return -1;
1722
}
18-
XCloseDisplay(d);
1923
return 0;
2024
}
2125

hotkey_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// Written by Changkun Ou <changkun.de>
66

7-
//go:build linux
7+
//go:build linux && cgo
88

99
package hotkey_test
1010

hotkey_nocgo.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2021 The golang.design Initiative Authors.
2+
// All rights reserved. Use of this source code is governed
3+
// by a MIT license that can be found in the LICENSE file.
4+
//
5+
// Written by Changkun Ou <changkun.de>
6+
7+
//go:build !windows && !cgo
8+
9+
package hotkey
10+
11+
import "context"
12+
13+
// Modifier represents a modifier
14+
type Modifier uint32
15+
16+
// Key represents a key.
17+
type Key uint8
18+
19+
func (hk *Hotkey) register() error {
20+
panic("hotkey: cannot use when CGO_ENABLED=0")
21+
}
22+
23+
// unregister deregisteres a system hotkey.
24+
func (hk *Hotkey) unregister() {
25+
panic("hotkey: cannot use when CGO_ENABLED=0")
26+
}
27+
28+
// handle handles the hotkey event loop.
29+
func (hk *Hotkey) handle(ctx context.Context) {
30+
panic("hotkey: cannot use when CGO_ENABLED=0")
31+
}

hotkey_nocgo_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2021 The golang.design Initiative Authors.
2+
// All rights reserved. Use of this source code is governed
3+
// by a MIT license that can be found in the LICENSE file.
4+
//
5+
// Written by Changkun Ou <changkun.de>
6+
7+
//go:build (linux || darwin) && !cgo
8+
9+
package hotkey_test
10+
11+
import (
12+
"testing"
13+
14+
"golang.design/x/hotkey"
15+
)
16+
17+
// TestHotkey should always run success.
18+
// This is a test to run and for manually testing, registered combination:
19+
// Ctrl+Alt+A (Ctrl+Mod2+Mod4+A on Linux)
20+
func TestHotkey(t *testing.T) {
21+
defer func() {
22+
if r := recover(); r != nil {
23+
return
24+
}
25+
t.Fatalf("expect to fail when CGO_ENABLED=0")
26+
}()
27+
28+
hotkey.Register([]hotkey.Modifier{}, hotkey.Key(0))
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/

0 commit comments

Comments
 (0)