File tree Expand file tree Collapse file tree 4 files changed +12
-15
lines changed Expand file tree Collapse file tree 4 files changed +12
-15
lines changed Original file line number Diff line number Diff line change 68
68
debug = false
69
69
errUnavailable = errors .New ("clipboard unavailable" )
70
70
errUnsupported = errors .New ("unsupported format" )
71
+ errNoCgo = errors .New ("clipboard: cannot use when CGO_ENABLED=0" )
71
72
)
72
73
73
74
// Format represents the format of clipboard data.
85
86
// Due to the limitation on operating systems (such as darwin),
86
87
// concurrent read can even cause panic, use a global lock to
87
88
// guarantee one read at a time.
88
- lock = sync.Mutex {}
89
- initOnce sync.Once
89
+ lock = sync.Mutex {}
90
+ initOnce sync.Once
90
91
initError error
91
92
)
92
93
@@ -95,10 +96,10 @@ var (
95
96
// target system lacks required dependency, such as libx11-dev in X11
96
97
// environment. For example,
97
98
//
98
- // err := clipboard.Init()
99
- // if err != nil {
100
- // panic(err)
101
- // }
99
+ // err := clipboard.Init()
100
+ // if err != nil {
101
+ // panic(err)
102
+ // }
102
103
//
103
104
// If Init returns an error, any subsequent Read/Write/Watch call
104
105
// may result in an unrecoverable panic.
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ package clipboard
5
5
import "context"
6
6
7
7
func initialize () error {
8
- panic ( "clipboard: cannot use when CGO_ENABLED=0" )
8
+ return errNoCgo
9
9
}
10
10
11
11
func read (t Format ) (buf []byte , err error ) {
Original file line number Diff line number Diff line change @@ -34,14 +34,9 @@ func TestClipboardInit(t *testing.T) {
34
34
t .Skip ("Windows does not need to check for cgo" )
35
35
}
36
36
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
+ }
45
40
})
46
41
t .Run ("with-cgo" , func (t * testing.T ) {
47
42
if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
Original file line number Diff line number Diff line change @@ -10,4 +10,5 @@ package clipboard
10
10
var (
11
11
Debug = debug
12
12
ErrUnavailable = errUnavailable
13
+ ErrCgoDisabled = errNoCgo
13
14
)
You can’t perform that action at this time.
0 commit comments