1package clipboard
2
3import (
4	"bytes"
5	"context"
6	"os"
7	"path/filepath"
8	"strconv"
9	"strings"
10	"testing"
11	"time"
12
13	"github.com/gopasspw/gopass/internal/out"
14	ps "github.com/mitchellh/go-ps"
15
16	"github.com/atotto/clipboard"
17	"github.com/stretchr/testify/assert"
18)
19
20func TestCopyToClipboard(t *testing.T) {
21	_ = os.Setenv("GOPASS_NO_NOTIFY", "true")
22	ctx, cancel := context.WithCancel(context.Background())
23	defer cancel()
24	clipboard.Unsupported = true
25
26	buf := &bytes.Buffer{}
27	out.Stdout = buf
28	assert.NoError(t, CopyTo(ctx, "foo", []byte("bar"), 1))
29	assert.Contains(t, buf.String(), "WARNING")
30}
31
32func TestClearClipboard(t *testing.T) {
33	ctx, cancel := context.WithCancel(context.Background())
34	assert.NoError(t, clear(ctx, []byte("bar"), 0))
35	cancel()
36	time.Sleep(50 * time.Millisecond)
37}
38
39func BenchmarkWalkProc(b *testing.B) {
40	for i := 0; i < b.N; i++ {
41		_ = filepath.Walk("/proc", func(path string, info os.FileInfo, err error) error {
42			if err != nil {
43				return nil
44			}
45			if strings.Count(path, "/") != 3 {
46				return nil
47			}
48			if !strings.HasSuffix(path, "/status") {
49				return nil
50			}
51			pid, err := strconv.Atoi(path[6:strings.LastIndex(path, "/")])
52			if err != nil {
53				return nil
54			}
55			walkFn(pid, func(int) {})
56			return nil
57		})
58	}
59}
60
61func BenchmarkListProc(b *testing.B) {
62	for i := 0; i < b.N; i++ {
63		procs, err := ps.Processes()
64		if err != nil {
65			b.Fatalf("err: %s", err)
66		}
67		for _, proc := range procs {
68			walkFn(proc.Pid(), func(int) {})
69		}
70	}
71}
72