@@ -9,6 +9,7 @@ package clipboard_test
99import (
1010 "bytes"
1111 "context"
12+ "errors"
1213 "image/png"
1314 "os"
1415 "reflect"
@@ -23,6 +24,38 @@ func init() {
2324 clipboard .Debug = true
2425}
2526
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+
2659func TestClipboard (t * testing.T ) {
2760 if runtime .GOOS != "windows" {
2861 if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
@@ -255,7 +288,7 @@ func BenchmarkClipboard(b *testing.B) {
255288}
256289
257290func 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 " {
259292 t .Skip ("CGO_ENABLED is set to 1" )
260293 }
261294 if runtime .GOOS == "windows" {
0 commit comments