Skip to content

Commit ff6477e

Browse files
authored
all: return error when cgo disabled (#78)
1 parent 8354ea1 commit ff6477e

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

clipboard.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ var (
6868
debug = false
6969
errUnavailable = errors.New("clipboard unavailable")
7070
errUnsupported = errors.New("unsupported format")
71+
errNoCgo = errors.New("clipboard: cannot use when CGO_ENABLED=0")
7172
)
7273

7374
// Format represents the format of clipboard data.
@@ -85,8 +86,8 @@ var (
8586
// Due to the limitation on operating systems (such as darwin),
8687
// concurrent read can even cause panic, use a global lock to
8788
// guarantee one read at a time.
88-
lock = sync.Mutex{}
89-
initOnce sync.Once
89+
lock = sync.Mutex{}
90+
initOnce sync.Once
9091
initError error
9192
)
9293

@@ -95,10 +96,10 @@ var (
9596
// target system lacks required dependency, such as libx11-dev in X11
9697
// environment. For example,
9798
//
98-
// err := clipboard.Init()
99-
// if err != nil {
100-
// panic(err)
101-
// }
99+
// err := clipboard.Init()
100+
// if err != nil {
101+
// panic(err)
102+
// }
102103
//
103104
// If Init returns an error, any subsequent Read/Write/Watch call
104105
// may result in an unrecoverable panic.

clipboard_nocgo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package clipboard
55
import "context"
66

77
func initialize() error {
8-
panic("clipboard: cannot use when CGO_ENABLED=0")
8+
return errNoCgo
99
}
1010

1111
func read(t Format) (buf []byte, err error) {

clipboard_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ func TestClipboardInit(t *testing.T) {
3434
t.Skip("Windows does not need to check for cgo")
3535
}
3636

37-
defer func() {
38-
if r := recover(); r != nil {
39-
return
40-
}
41-
t.Fatalf("expect to fail when CGO_ENABLED=0")
42-
}()
43-
44-
clipboard.Init()
37+
if err := clipboard.Init(); !errors.Is(err, clipboard.ErrCgoDisabled) {
38+
t.Fatalf("expect ErrCgoDisabled, got: %v", err)
39+
}
4540
})
4641
t.Run("with-cgo", func(t *testing.T) {
4742
if val, ok := os.LookupEnv("CGO_ENABLED"); ok && val == "0" {

export_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ package clipboard
1010
var (
1111
Debug = debug
1212
ErrUnavailable = errUnavailable
13+
ErrCgoDisabled = errNoCgo
1314
)

0 commit comments

Comments
 (0)