summaryrefslogtreecommitdiff
path: root/tests/functional
diff options
context:
space:
mode:
Diffstat (limited to 'tests/functional')
-rw-r--r--tests/functional/pick-roundtrip/main.go7
-rw-r--r--tests/functional/pick-roundtrip/remembering.go129
2 files changed, 136 insertions, 0 deletions
diff --git a/tests/functional/pick-roundtrip/main.go b/tests/functional/pick-roundtrip/main.go
new file mode 100644
index 0000000..0b02dc7
--- /dev/null
+++ b/tests/functional/pick-roundtrip/main.go
@@ -0,0 +1,7 @@
+package main
+
+import "remembering"
+
+func main() {
+ remembering.MainTest()
+}
diff --git a/tests/functional/pick-roundtrip/remembering.go b/tests/functional/pick-roundtrip/remembering.go
new file mode 100644
index 0000000..e2d54da
--- /dev/null
+++ b/tests/functional/pick-roundtrip/remembering.go
@@ -0,0 +1,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,
+ )
+ })
+}