@@ -9,6 +9,7 @@ package clipboard_test
9
9
import (
10
10
"bytes"
11
11
"context"
12
+ "errors"
12
13
"image/png"
13
14
"os"
14
15
"reflect"
@@ -23,6 +24,38 @@ func init() {
23
24
clipboard .Debug = true
24
25
}
25
26
27
+ func TestClipboardInit (t * testing.T ) {
28
+ t .Run ("no-cgo" , func (t * testing.T ) {
29
+ if val , ok := os .LookupEnv ("CGO_ENABLED" ); ! ok || val != "0" {
30
+ t .Skip ("CGO_ENABLED is set to 1" )
31
+ }
32
+ if runtime .GOOS == "windows" {
33
+ t .Skip ("Windows does not need to check for cgo" )
34
+ }
35
+
36
+ defer func () {
37
+ if r := recover (); r != nil {
38
+ return
39
+ }
40
+ t .Fatalf ("expect to fail when CGO_ENABLED=0" )
41
+ }()
42
+
43
+ clipboard .Init ()
44
+ })
45
+ t .Run ("with-cgo" , func (t * testing.T ) {
46
+ if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
47
+ t .Skip ("CGO_ENABLED is set to 0" )
48
+ }
49
+ if runtime .GOOS != "linux" {
50
+ t .Skip ("Only Linux may return error at the moment." )
51
+ }
52
+
53
+ if err := clipboard .Init (); err != nil && ! errors .Is (err , clipboard .ErrUnavailable ) {
54
+ t .Fatalf ("expect ErrUnavailable, but got: %v" , err )
55
+ }
56
+ })
57
+ }
58
+
26
59
func TestClipboard (t * testing.T ) {
27
60
if runtime .GOOS != "windows" {
28
61
if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
@@ -255,7 +288,7 @@ func BenchmarkClipboard(b *testing.B) {
255
288
}
256
289
257
290
func TestClipboardNoCgo (t * testing.T ) {
258
- if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "1 " {
291
+ if val , ok := os .LookupEnv ("CGO_ENABLED" ); ! ok || val != "0 " {
259
292
t .Skip ("CGO_ENABLED is set to 1" )
260
293
}
261
294
if runtime .GOOS == "windows" {
0 commit comments