fork/gdu

git clone https://git.julia.blue/fork/gdu

addielaruefeat: copy path of selected item to clipboard (#645)011b7e8

master
3.4 KiB115 linesraw
1package tui
2
3import (
4	"bytes"
5	"errors"
6	"os/exec"
7	"runtime"
8	"testing"
9
10	"github.com/stretchr/testify/assert"
11
12	"github.com/dundee/gdu/v5/internal/testapp"
13)
14
15func lookPathFor(names ...string) func(string) (string, error) {
16	available := make(map[string]bool, len(names))
17	for _, name := range names {
18		available[name] = true
19	}
20	return func(name string) (string, error) {
21		if available[name] {
22			return "/usr/bin/" + name, nil
23		}
24		return "", errors.New("executable not found")
25	}
26}
27
28func TestClipboardCommand(t *testing.T) {
29	for name, tt := range map[string]struct {
30		env    clipboardEnv
31		want   []string
32		wantOK bool
33	}{
34		"darwin uses pbcopy": {
35			env:  clipboardEnv{goos: osDarwin, lookPath: lookPathFor("pbcopy")},
36			want: []string{"/usr/bin/pbcopy"}, wantOK: true,
37		},
38		"windows uses clip": {
39			env:  clipboardEnv{goos: osWindows, lookPath: lookPathFor("clip")},
40			want: []string{"/usr/bin/clip"}, wantOK: true,
41		},
42		"linux uses xclip": {
43			env:  clipboardEnv{goos: "linux", lookPath: lookPathFor("xclip", "xsel")},
44			want: []string{"/usr/bin/xclip", "-selection", "clipboard"}, wantOK: true,
45		},
46		"linux falls back to xsel": {
47			env:  clipboardEnv{goos: "linux", lookPath: lookPathFor("xsel")},
48			want: []string{"/usr/bin/xsel", "--clipboard", "--input"}, wantOK: true,
49		},
50		"freebsd uses the same tools as linux": {
51			env:  clipboardEnv{goos: "freebsd", lookPath: lookPathFor("xclip")},
52			want: []string{"/usr/bin/xclip", "-selection", "clipboard"}, wantOK: true,
53		},
54		"wayland session prefers wl-copy": {
55			env:  clipboardEnv{goos: "linux", wayland: true, lookPath: lookPathFor("wl-copy", "xclip")},
56			want: []string{"/usr/bin/wl-copy"}, wantOK: true,
57		},
58		"without wayland session prefers xclip over wl-copy": {
59			env:  clipboardEnv{goos: "linux", wayland: false, lookPath: lookPathFor("wl-copy", "xclip")},
60			want: []string{"/usr/bin/xclip", "-selection", "clipboard"}, wantOK: true,
61		},
62		"falls back to wl-copy when only tool": {
63			env:  clipboardEnv{goos: "linux", wayland: false, lookPath: lookPathFor("wl-copy")},
64			want: []string{"/usr/bin/wl-copy"}, wantOK: true,
65		},
66		"no tool available": {
67			env:  clipboardEnv{goos: "linux", lookPath: lookPathFor()},
68			want: nil, wantOK: false,
69		},
70	} {
71		t.Run(name, func(t *testing.T) {
72			got, ok := tt.env.command()
73			assert.Equal(t, tt.wantOK, ok)
74			assert.Equal(t, tt.want, got)
75		})
76	}
77}
78
79func TestCopyToClipboardRoundTrip(t *testing.T) {
80	if runtime.GOOS != osDarwin {
81		t.Skip("clipboard round-trip is only verified with pbcopy/pbpaste on darwin")
82	}
83	if _, err := exec.LookPath("pbcopy"); err != nil {
84		t.Skip("pbcopy not available")
85	}
86
87	// Preserve and restore whatever the user already had on the clipboard.
88	before, _ := exec.Command("pbpaste").Output()
89	t.Cleanup(func() {
90		restore := exec.Command("pbcopy")
91		restore.Stdin = bytes.NewReader(before)
92		_ = restore.Run()
93	})
94
95	const path = "/tmp/gdu clipboard test/some file"
96	if err := copyToClipboard(path); err != nil {
97		t.Fatal(err)
98	}
99
100	out, err := exec.Command("pbpaste").Output()
101	if err != nil {
102		t.Fatal(err)
103	}
104	assert.Equal(t, path, string(out))
105}
106
107func TestCopySelectedPathWithoutCurrentDir(t *testing.T) {
108	app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
109	defer simScreen.Fini()
110
111	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false)
112
113	// No directory analyzed yet: copying must be a safe no-op.
114	assert.NotPanics(t, ui.copySelectedPath)
115}