Skip to content

Commit

Permalink
all: panic when cgo is disabled (for darwin and linux)
Browse files Browse the repository at this point in the history
Fixes #11
  • Loading branch information
changkun committed Sep 20, 2021
1 parent 87d06ab commit 5f2192a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/clipboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
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 .
6 changes: 2 additions & 4 deletions clipboard_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions clipboard_nocgo.go
Original file line number Diff line number Diff line change
@@ -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")
}
76 changes: 74 additions & 2 deletions clipboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"image/png"
"os"
"reflect"
"runtime"
"testing"
"time"

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -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")

Expand All @@ -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)
})
}
6 changes: 4 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// Written by Changkun Ou <changkun.de>

//go:build cgo

package clipboard_test

import (
Expand All @@ -20,7 +22,7 @@ func ExampleWrite() {
}

func ExampleRead() {
fmt.Printf(string(clipboard.Read(clipboard.FmtText)))
fmt.Println(string(clipboard.Read(clipboard.FmtText)))
// Output:
// Hello, 世界
}
Expand All @@ -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
}

0 comments on commit 5f2192a

Please sign in to comment.