Skip to content

Commit 5f2192a

Browse files
committed
all: panic when cgo is disabled (for darwin and linux)
Fixes #11
1 parent 87d06ab commit 5f2192a

File tree

5 files changed

+116
-11
lines changed

5 files changed

+116
-11
lines changed

.github/workflows/clipboard.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,20 @@ jobs:
5151

5252
- name: Build
5353
run: |
54-
go build
54+
go build -o gclip cmd/gclip/main.go
55+
go build -o gclip-gui cmd/gclip-gui/main.go
5556
56-
- name: Run Tests
57+
- name: Run Tests with CGO_ENABLED=1
58+
if: ${{ runner.os == 'Linux' || runner.os == 'macOS'}}
5759
run: |
58-
go test -v -covermode=atomic ./...
60+
CGO_ENABLED=1 go test -v -covermode=atomic .
61+
62+
- name: Run Tests with CGO_ENABLED=0
63+
if: ${{ runner.os == 'Linux' || runner.os == 'macOS'}}
64+
run: |
65+
CGO_ENABLED=0 go test -v -covermode=atomic .
66+
67+
- name: Run Tests on Windows
68+
if: ${{ runner.os == 'Windows'}}
69+
run: |
70+
go test -v -covermode=atomic .

clipboard_linux.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141

4242
const errmsg = `Failed to initialize the X11 display, and the clipboard package
4343
will not work properly. Install the following dependency may help:
44-
44+
4545
apt install -y libx11-dev
4646
4747
If the clipboard package is in an environment without a frame buffer,
@@ -85,8 +85,6 @@ func readc(t string) ([]byte, error) {
8585
}
8686
defer C.free(unsafe.Pointer(data))
8787
switch {
88-
case n < 0:
89-
return nil, errUnavailable
9088
case n == 0:
9189
return nil, nil
9290
default:
@@ -153,7 +151,7 @@ func watch(ctx context.Context, t Format) <-chan []byte {
153151
if b == nil {
154152
continue
155153
}
156-
if bytes.Compare(last, b) != 0 {
154+
if !bytes.Equal(last, b) {
157155
recv <- b
158156
last = b
159157
}

clipboard_nocgo.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//go:build !windows && !cgo
2+
3+
package clipboard
4+
5+
import "context"
6+
7+
func read(t Format) (buf []byte, err error) {
8+
panic("clipboard: cannot use when CGO_ENABLED=0")
9+
}
10+
11+
func readc(t string) ([]byte, error) {
12+
panic("clipboard: cannot use when CGO_ENABLED=0")
13+
}
14+
15+
func write(t Format, buf []byte) (<-chan struct{}, error) {
16+
panic("clipboard: cannot use when CGO_ENABLED=0")
17+
}
18+
19+
func watch(ctx context.Context, t Format) <-chan []byte {
20+
panic("clipboard: cannot use when CGO_ENABLED=0")
21+
}

clipboard_test.go

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2526
func 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

94101
func 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

135148
func 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

155174
func 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

168193
func 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

213244
func 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+
}

example_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//
55
// Written by Changkun Ou <changkun.de>
66

7+
//go:build cgo
8+
79
package clipboard_test
810

911
import (
@@ -20,7 +22,7 @@ func ExampleWrite() {
2022
}
2123

2224
func ExampleRead() {
23-
fmt.Printf(string(clipboard.Read(clipboard.FmtText)))
25+
fmt.Println(string(clipboard.Read(clipboard.FmtText)))
2426
// Output:
2527
// Hello, 世界
2628
}
@@ -33,7 +35,7 @@ func ExampleWatch() {
3335
go func(ctx context.Context) {
3436
clipboard.Write(clipboard.FmtText, []byte("你好,world"))
3537
}(ctx)
36-
fmt.Printf(string(<-changed))
38+
fmt.Println(string(<-changed))
3739
// Output:
3840
// 你好,world
3941
}

0 commit comments

Comments
 (0)