@@ -12,6 +12,7 @@ import (
1212 "image/png"
1313 "os"
1414 "reflect"
15+ "runtime"
1516 "testing"
1617 "time"
1718
@@ -23,6 +24,12 @@ func init() {
2324}
2425
2526func TestClipboard (t * testing.T ) {
27+ if runtime .GOOS != "windows" {
28+ if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
29+ t .Skip ("CGO_ENABLED is set to 0" )
30+ }
31+ }
32+
2633 t .Run ("image" , func (t * testing.T ) {
2734 data , err := os .ReadFile ("tests/testdata/clipboard.png" )
2835 if err != nil {
@@ -92,6 +99,12 @@ func TestClipboard(t *testing.T) {
9299}
93100
94101func TestClipboardMultipleWrites (t * testing.T ) {
102+ if runtime .GOOS != "windows" {
103+ if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
104+ t .Skip ("CGO_ENABLED is set to 0" )
105+ }
106+ }
107+
95108 data , err := os .ReadFile ("tests/testdata/clipboard.png" )
96109 if err != nil {
97110 t .Fatalf ("failed to read gold file: %v" , err )
@@ -133,6 +146,12 @@ func TestClipboardMultipleWrites(t *testing.T) {
133146}
134147
135148func TestClipboardConcurrentRead (t * testing.T ) {
149+ if runtime .GOOS != "windows" {
150+ if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
151+ t .Skip ("CGO_ENABLED is set to 0" )
152+ }
153+ }
154+
136155 // This test check that concurrent read/write to the clipboard does
137156 // not cause crashes on some specific platform, such as macOS.
138157 done := make (chan bool , 2 )
@@ -153,6 +172,12 @@ func TestClipboardConcurrentRead(t *testing.T) {
153172}
154173
155174func TestClipboardWriteEmpty (t * testing.T ) {
175+ if runtime .GOOS != "windows" {
176+ if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
177+ t .Skip ("CGO_ENABLED is set to 0" )
178+ }
179+ }
180+
156181 chg1 := clipboard .Write (clipboard .FmtText , nil )
157182 if got := clipboard .Read (clipboard .FmtText ); got != nil {
158183 t .Fatalf ("write nil to clipboard should read nil, got: %v" , string (got ))
@@ -166,6 +191,12 @@ func TestClipboardWriteEmpty(t *testing.T) {
166191}
167192
168193func TestClipboardWatch (t * testing.T ) {
194+ if runtime .GOOS != "windows" {
195+ if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
196+ t .Skip ("CGO_ENABLED is set to 0" )
197+ }
198+ }
199+
169200 ctx , cancel := context .WithTimeout (context .Background (), time .Second * 2 )
170201 defer cancel ()
171202
@@ -202,7 +233,7 @@ func TestClipboardWatch(t *testing.T) {
202233 }
203234 return
204235 }
205- if bytes .Compare (data , want ) != 0 {
236+ if ! bytes .Equal (data , want ) {
206237 t .Fatalf ("received data from watch mismatch, want: %v, got %v" , string (want ), string (data ))
207238 }
208239 lastRead = data
@@ -211,7 +242,6 @@ func TestClipboardWatch(t *testing.T) {
211242}
212243
213244func BenchmarkClipboard (b * testing.B ) {
214-
215245 b .Run ("text" , func (b * testing.B ) {
216246 data := []byte ("golang.design/x/clipboard" )
217247
@@ -223,3 +253,45 @@ func BenchmarkClipboard(b *testing.B) {
223253 }
224254 })
225255}
256+
257+ func TestClipboardNoCgo (t * testing.T ) {
258+ if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "1" {
259+ t .Skip ("CGO_ENABLED is set to 1" )
260+ }
261+ if runtime .GOOS == "windows" {
262+ t .Skip ("Windows should always be tested" )
263+ }
264+
265+ t .Run ("Read" , func (t * testing.T ) {
266+ defer func () {
267+ if r := recover (); r != nil {
268+ return
269+ }
270+ t .Fatalf ("expect to fail when CGO_ENABLED=0" )
271+ }()
272+
273+ clipboard .Read (clipboard .FmtText )
274+ })
275+
276+ t .Run ("Write" , func (t * testing.T ) {
277+ defer func () {
278+ if r := recover (); r != nil {
279+ return
280+ }
281+ t .Fatalf ("expect to fail when CGO_ENABLED=0" )
282+ }()
283+
284+ clipboard .Write (clipboard .FmtText , []byte ("dummy" ))
285+ })
286+
287+ t .Run ("Watch" , func (t * testing.T ) {
288+ defer func () {
289+ if r := recover (); r != nil {
290+ return
291+ }
292+ t .Fatalf ("expect to fail when CGO_ENABLED=0" )
293+ }()
294+
295+ clipboard .Watch (context .TODO (), clipboard .FmtText )
296+ })
297+ }
0 commit comments