summaryrefslogtreecommitdiff
path: root/tests/functional/pick-roundtrip/remembering.go
blob: e2d54da345c2b758ae8c09619855a41474f515ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package remembering

import (
	"fmt"
	"os"
	"reflect"
	"strings"
)



func showColour() bool {
	return os.Getenv("NO_COLOUR") == ""
}

func testing(message string, body func()) {
	if showColour() {
		fmt.Fprintf(
			os.Stderr,
			"\033[0;33mtesting\033[0m: %s... ",
			message,
		)
		body()
		fmt.Fprintf(os.Stderr, "\033[0;32mOK\033[0m.\n")
	} else {
		fmt.Fprintf(os.Stderr, "testing: %s... ", message)
		body()
		fmt.Fprintf(os.Stderr, "OK.\n")
	}
}

func assertEq(given any, expected any) {
	if !reflect.DeepEqual(given, expected) {
		if showColour() {
			fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n")
		} else {
			fmt.Fprintf(os.Stderr, "ERR.\n")
		}
		fmt.Fprintf(os.Stderr, "given != expected\n")
		fmt.Fprintf(os.Stderr, "given:    %#v\n", given)
		fmt.Fprintf(os.Stderr, "expected: %#v\n", expected)
		os.Exit(1)
	}
}



func pick(menu string, command ...string) (int, string, string) {
	out := strings.Builder{}
	errW := strings.Builder{}
	rc := run(envT{
		allArgs: append(
			[]string{"remembering", "-p", "func", "--"},
			command...,
		),
		in:  strings.NewReader(menu),
		out: &out,
		err: &errW,
	})
	return rc, out.String(), errW.String()
}

func profileBytes() string {
	path, err := profilePath("func")
	if err != nil {
		panic(err)
	}
	data, err := os.ReadFile(path)
	if err != nil {
		panic(err)
	}
	return string(data)
}



func MainTest() {
	testing("a session of picks shapes the ranking", func() {
		tmp, err := os.MkdirTemp(
			"", "remembering-functional-",
		)
		assertEq(err, nil)
		defer os.RemoveAll(tmp)
		saved := os.Getenv("XDG_DATA_HOME")
		os.Setenv("XDG_DATA_HOME", tmp)
		defer os.Setenv("XDG_DATA_HOME", saved)

		menu := "a\nb\nc\nd\ne\n"

		rc, out, errW := pick(menu, "grep", "-F", "c")
		assertEq(rc, 0)
		assertEq(out, "c\n")
		assertEq(errW, "")
		assertEq(
			profileBytes(),
			"0 profile a\n"+
				"0 profile b\n"+
				"1 profile c\n"+
				"0 profile d\n"+
				"0 profile e\n",
		)

		// the learnt pick now ranks first
		rc, out, errW = pick(menu, "head", "-n1")
		assertEq(rc, 0)
		assertEq(out, "c\n")
		assertEq(errW, "")
		assertEq(
			profileBytes(),
			"0 profile a\n"+
				"0 profile b\n"+
				"2 profile c\n"+
				"0 profile d\n"+
				"0 profile e\n",
		)

		// a cancelled menu forwards the status and
		// learns nothing
		rc, out, _ = pick(menu, "sh", "-c", "exit 3")
		assertEq(rc, 3)
		assertEq(out, "")
		assertEq(
			strings.Contains(
				profileBytes(), "2 profile c",
			),
			true,
		)
	})
}