diff --git a/.github/workflows/clipboard.yml b/.github/workflows/clipboard.yml index ee24ea7..bc27c83 100644 --- a/.github/workflows/clipboard.yml +++ b/.github/workflows/clipboard.yml @@ -51,8 +51,20 @@ jobs: - name: Build run: | - go build + go build -o gclip cmd/gclip/main.go + go build -o gclip-gui cmd/gclip-gui/main.go - - name: Run Tests + - name: Run Tests with CGO_ENABLED=1 + if: ${{ runner.os == 'Linux' || runner.os == 'macOS'}} run: | - go test -v -covermode=atomic ./... \ No newline at end of file + CGO_ENABLED=1 go test -v -covermode=atomic . + + - name: Run Tests with CGO_ENABLED=0 + if: ${{ runner.os == 'Linux' || runner.os == 'macOS'}} + run: | + CGO_ENABLED=0 go test -v -covermode=atomic . + + - name: Run Tests on Windows + if: ${{ runner.os == 'Windows'}} + run: | + go test -v -covermode=atomic . \ No newline at end of file diff --git a/clipboard_linux.go b/clipboard_linux.go index f7fcf02..67bb287 100644 --- a/clipboard_linux.go +++ b/clipboard_linux.go @@ -41,7 +41,7 @@ import ( const errmsg = `Failed to initialize the X11 display, and the clipboard package will not work properly. Install the following dependency may help: - + apt install -y libx11-dev If the clipboard package is in an environment without a frame buffer, @@ -85,8 +85,6 @@ func readc(t string) ([]byte, error) { } defer C.free(unsafe.Pointer(data)) switch { - case n < 0: - return nil, errUnavailable case n == 0: return nil, nil default: @@ -153,7 +151,7 @@ func watch(ctx context.Context, t Format) <-chan []byte { if b == nil { continue } - if bytes.Compare(last, b) != 0 { + if !bytes.Equal(last, b) { recv <- b last = b } diff --git a/clipboard_nocgo.go b/clipboard_nocgo.go new file mode 100644 index 0000000..e4a39ce --- /dev/null +++ b/clipboard_nocgo.go @@ -0,0 +1,21 @@ +//go:build !windows && !cgo + +package clipboard + +import "context" + +func read(t Format) (buf []byte, err error) { + panic("clipboard: cannot use when CGO_ENABLED=0") +} + +func readc(t string) ([]byte, error) { + panic("clipboard: cannot use when CGO_ENABLED=0") +} + +func write(t Format, buf []byte) (<-chan struct{}, error) { + panic("clipboard: cannot use when CGO_ENABLED=0") +} + +func watch(ctx context.Context, t Format) <-chan []byte { + panic("clipboard: cannot use when CGO_ENABLED=0") +} diff --git a/clipboard_test.go b/clipboard_test.go index 0f11ee4..fcc74a2 100644 --- a/clipboard_test.go +++ b/clipboard_test.go @@ -12,6 +12,7 @@ import ( "image/png" "os" "reflect" + "runtime" "testing" "time" @@ -23,6 +24,12 @@ func init() { } func TestClipboard(t *testing.T) { + if runtime.GOOS != "windows" { + if val, ok := os.LookupEnv("CGO_ENABLED"); ok && val == "0" { + t.Skip("CGO_ENABLED is set to 0") + } + } + t.Run("image", func(t *testing.T) { data, err := os.ReadFile("tests/testdata/clipboard.png") if err != nil { @@ -92,6 +99,12 @@ func TestClipboard(t *testing.T) { } func TestClipboardMultipleWrites(t *testing.T) { + if runtime.GOOS != "windows" { + if val, ok := os.LookupEnv("CGO_ENABLED"); ok && val == "0" { + t.Skip("CGO_ENABLED is set to 0") + } + } + data, err := os.ReadFile("tests/testdata/clipboard.png") if err != nil { t.Fatalf("failed to read gold file: %v", err) @@ -133,6 +146,12 @@ func TestClipboardMultipleWrites(t *testing.T) { } func TestClipboardConcurrentRead(t *testing.T) { + if runtime.GOOS != "windows" { + if val, ok := os.LookupEnv("CGO_ENABLED"); ok && val == "0" { + t.Skip("CGO_ENABLED is set to 0") + } + } + // This test check that concurrent read/write to the clipboard does // not cause crashes on some specific platform, such as macOS. done := make(chan bool, 2) @@ -153,6 +172,12 @@ func TestClipboardConcurrentRead(t *testing.T) { } func TestClipboardWriteEmpty(t *testing.T) { + if runtime.GOOS != "windows" { + if val, ok := os.LookupEnv("CGO_ENABLED"); ok && val == "0" { + t.Skip("CGO_ENABLED is set to 0") + } + } + chg1 := clipboard.Write(clipboard.FmtText, nil) if got := clipboard.Read(clipboard.FmtText); got != nil { t.Fatalf("write nil to clipboard should read nil, got: %v", string(got)) @@ -166,6 +191,12 @@ func TestClipboardWriteEmpty(t *testing.T) { } func TestClipboardWatch(t *testing.T) { + if runtime.GOOS != "windows" { + if val, ok := os.LookupEnv("CGO_ENABLED"); ok && val == "0" { + t.Skip("CGO_ENABLED is set to 0") + } + } + ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) defer cancel() @@ -202,7 +233,7 @@ func TestClipboardWatch(t *testing.T) { } return } - if bytes.Compare(data, want) != 0 { + if !bytes.Equal(data, want) { t.Fatalf("received data from watch mismatch, want: %v, got %v", string(want), string(data)) } lastRead = data @@ -211,7 +242,6 @@ func TestClipboardWatch(t *testing.T) { } func BenchmarkClipboard(b *testing.B) { - b.Run("text", func(b *testing.B) { data := []byte("golang.design/x/clipboard") @@ -223,3 +253,45 @@ func BenchmarkClipboard(b *testing.B) { } }) } + +func TestClipboardNoCgo(t *testing.T) { + if val, ok := os.LookupEnv("CGO_ENABLED"); ok && val == "1" { + t.Skip("CGO_ENABLED is set to 1") + } + if runtime.GOOS == "windows" { + t.Skip("Windows should always be tested") + } + + t.Run("Read", func(t *testing.T) { + defer func() { + if r := recover(); r != nil { + return + } + t.Fatalf("expect to fail when CGO_ENABLED=0") + }() + + clipboard.Read(clipboard.FmtText) + }) + + t.Run("Write", func(t *testing.T) { + defer func() { + if r := recover(); r != nil { + return + } + t.Fatalf("expect to fail when CGO_ENABLED=0") + }() + + clipboard.Write(clipboard.FmtText, []byte("dummy")) + }) + + t.Run("Watch", func(t *testing.T) { + defer func() { + if r := recover(); r != nil { + return + } + t.Fatalf("expect to fail when CGO_ENABLED=0") + }() + + clipboard.Watch(context.TODO(), clipboard.FmtText) + }) +} diff --git a/example_test.go b/example_test.go index 5a673cb..4da4270 100644 --- a/example_test.go +++ b/example_test.go @@ -4,6 +4,8 @@ // // Written by Changkun Ou +//go:build cgo + package clipboard_test import ( @@ -20,7 +22,7 @@ func ExampleWrite() { } func ExampleRead() { - fmt.Printf(string(clipboard.Read(clipboard.FmtText))) + fmt.Println(string(clipboard.Read(clipboard.FmtText))) // Output: // Hello, 世界 } @@ -33,7 +35,7 @@ func ExampleWatch() { go func(ctx context.Context) { clipboard.Write(clipboard.FmtText, []byte("你好,world")) }(ctx) - fmt.Printf(string(<-changed)) + fmt.Println(string(<-changed)) // Output: // 你好,world }