Skip to content

Commit 68907de

Browse files
author
Stanislav (Stas) Katkov
committed
simplify internal/clipboard testing
1 parent 3f8be9b commit 68907de

File tree

2 files changed

+1
-139
lines changed

2 files changed

+1
-139
lines changed

internal/clipboard/clipboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ func enhanceError(originalErr error, operation string) error {
4949
helpMsg = `clipboard manager was not found on this system`
5050
}
5151

52-
return fmt.Errorf("clipboard %s failed: %w%s", operation, originalErr, helpMsg)
52+
return fmt.Errorf("clipboard %s failed: %s", operation, helpMsg)
5353
}

internal/clipboard/clipboard_test.go

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,7 @@ import (
77
"testing"
88
)
99

10-
func TestEnhanceError_NonLinux(t *testing.T) {
11-
// This test verifies that on non-Linux systems, we just return a simple error
12-
originalErr := os.ErrNotExist
13-
enhanced := enhanceError(originalErr, "copy")
14-
15-
if enhanced == nil {
16-
t.Fatal("Expected error, got nil")
17-
}
18-
19-
if !strings.Contains(enhanced.Error(), "clipboard copy failed") {
20-
t.Errorf("Expected error message to contain 'clipboard copy failed', got: %s", enhanced.Error())
21-
}
22-
23-
// Should not contain Linux-specific help messages
24-
if strings.Contains(enhanced.Error(), "wl-clipboard") {
25-
t.Errorf("Expected no Linux-specific help on non-Linux, got: %s", enhanced.Error())
26-
}
27-
}
28-
2910
func TestEnhanceError_WaylandSession(t *testing.T) {
30-
// Skip if not on Linux
31-
if os.Getenv("CI") != "" {
32-
t.Skip("Skipping session-dependent test in CI")
33-
}
34-
35-
// Save and restore original session type
3611
originalSession := os.Getenv("XDG_SESSION_TYPE")
3712
defer func() {
3813
if originalSession == "" {
@@ -42,10 +17,8 @@ func TestEnhanceError_WaylandSession(t *testing.T) {
4217
}
4318
}()
4419

45-
// Set Wayland session
4620
os.Setenv("XDG_SESSION_TYPE", "wayland")
4721

48-
// Create an error that looks like a missing executable
4922
originalErr := &os.PathError{
5023
Op: "exec",
5124
Path: "wl-copy",
@@ -69,12 +42,6 @@ func TestEnhanceError_WaylandSession(t *testing.T) {
6942
}
7043

7144
func TestEnhanceError_X11Session(t *testing.T) {
72-
// Skip if not on Linux
73-
if os.Getenv("CI") != "" {
74-
t.Skip("Skipping session-dependent test in CI")
75-
}
76-
77-
// Save and restore original session type
7845
originalSession := os.Getenv("XDG_SESSION_TYPE")
7946
defer func() {
8047
if originalSession == "" {
@@ -84,10 +51,8 @@ func TestEnhanceError_X11Session(t *testing.T) {
8451
}
8552
}()
8653

87-
// Set X11 session
8854
os.Setenv("XDG_SESSION_TYPE", "x11")
8955

90-
// Create an error that looks like a missing executable
9156
originalErr := &os.PathError{
9257
Op: "exec",
9358
Path: "xclip",
@@ -111,12 +76,6 @@ func TestEnhanceError_X11Session(t *testing.T) {
11176
}
11277

11378
func TestEnhanceError_UnknownSession(t *testing.T) {
114-
// Skip if not on Linux
115-
if os.Getenv("CI") != "" {
116-
t.Skip("Skipping session-dependent test in CI")
117-
}
118-
119-
// Save and restore original session type
12079
originalSession := os.Getenv("XDG_SESSION_TYPE")
12180
defer func() {
12281
if originalSession == "" {
@@ -126,10 +85,8 @@ func TestEnhanceError_UnknownSession(t *testing.T) {
12685
}
12786
}()
12887

129-
// Unset session type
13088
os.Unsetenv("XDG_SESSION_TYPE")
13189

132-
// Create an error that looks like a missing executable
13390
originalErr := &os.PathError{
13491
Op: "exec",
13592
Path: "xclip",
@@ -144,7 +101,6 @@ func TestEnhanceError_UnknownSession(t *testing.T) {
144101

145102
errMsg := enhanced.Error()
146103

147-
// Should mention that clipboard manager was not found
148104
if !strings.Contains(errMsg, "clipboard manager was not found") {
149105
t.Errorf("Expected error message to mention clipboard manager not found, got: %s", errMsg)
150106
}
@@ -153,97 +109,3 @@ func TestEnhanceError_UnknownSession(t *testing.T) {
153109
t.Errorf("Expected error message to contain operation type, got: %s", errMsg)
154110
}
155111
}
156-
157-
func TestEnhanceError_AllOperations(t *testing.T) {
158-
// Skip if not on Linux
159-
if os.Getenv("CI") != "" {
160-
t.Skip("Skipping session-dependent test in CI")
161-
}
162-
163-
// Save and restore original session type
164-
originalSession := os.Getenv("XDG_SESSION_TYPE")
165-
defer func() {
166-
if originalSession == "" {
167-
os.Unsetenv("XDG_SESSION_TYPE")
168-
} else {
169-
os.Setenv("XDG_SESSION_TYPE", originalSession)
170-
}
171-
}()
172-
173-
testCases := []struct {
174-
name string
175-
sessionType string
176-
operation string
177-
wantContain string
178-
}{
179-
{
180-
name: "wayland copy",
181-
sessionType: "wayland",
182-
operation: "copy",
183-
wantContain: "wl-clipboard",
184-
},
185-
{
186-
name: "wayland paste",
187-
sessionType: "wayland",
188-
operation: "paste",
189-
wantContain: "wl-clipboard",
190-
},
191-
{
192-
name: "x11 copy",
193-
sessionType: "x11",
194-
operation: "copy",
195-
wantContain: "xclip",
196-
},
197-
{
198-
name: "x11 paste",
199-
sessionType: "x11",
200-
operation: "paste",
201-
wantContain: "xclip",
202-
},
203-
{
204-
name: "unknown copy",
205-
sessionType: "",
206-
operation: "copy",
207-
wantContain: "clipboard manager was not found",
208-
},
209-
{
210-
name: "unknown paste",
211-
sessionType: "",
212-
operation: "paste",
213-
wantContain: "clipboard manager was not found",
214-
},
215-
}
216-
217-
for _, tc := range testCases {
218-
t.Run(tc.name, func(t *testing.T) {
219-
if tc.sessionType == "" {
220-
os.Unsetenv("XDG_SESSION_TYPE")
221-
} else {
222-
os.Setenv("XDG_SESSION_TYPE", tc.sessionType)
223-
}
224-
225-
originalErr := &os.PathError{
226-
Op: "exec",
227-
Path: "xclip",
228-
Err: os.ErrNotExist,
229-
}
230-
231-
enhanced := enhanceError(originalErr, tc.operation)
232-
233-
if enhanced == nil {
234-
t.Fatal("Expected error, got nil")
235-
}
236-
237-
errMsg := enhanced.Error()
238-
239-
if !strings.Contains(errMsg, tc.wantContain) {
240-
t.Errorf("Expected error to contain %q, got: %s", tc.wantContain, errMsg)
241-
}
242-
243-
expectedOp := "clipboard " + tc.operation + " failed"
244-
if !strings.Contains(errMsg, expectedOp) {
245-
t.Errorf("Expected error to contain %q, got: %s", expectedOp, errMsg)
246-
}
247-
})
248-
}
249-
}

0 commit comments

Comments
 (0)