forked from soren-achebe/backscroll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_test.go
More file actions
147 lines (137 loc) · 4.01 KB
/
Copy pathexport_test.go
File metadata and controls
147 lines (137 loc) · 4.01 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"flag"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"github.com/soren-achebe/backscroll/internal/store"
)
func TestFenceFor(t *testing.T) {
cases := []struct {
body string
want string
}{
{"plain output", "```"},
{"has `inline` code", "```"},
{"has ``` a fence", "````"},
{"x``````y", "```````"},
{"", "```"},
}
for _, c := range cases {
if got := fenceFor(c.body); got != c.want {
t.Errorf("fenceFor(%q) = %q, want %q", c.body, got, c.want)
}
}
}
func TestHTMLEscape(t *testing.T) {
if got := htmlEscape(`a < b && c > d`); got != "a < b && c > d" {
t.Errorf("htmlEscape = %q", got)
}
}
func TestRound2(t *testing.T) {
if round2(0.6149999) != 0.615 && round2(0.615) != 0.615 {
t.Errorf("round2 broken: %v", round2(0.615))
}
if round2(0.0) != 0.0 {
t.Errorf("round2(0) = %v", round2(0.0))
}
}
func TestFenceForNeverContained(t *testing.T) {
// property: the chosen fence must never appear in the body
bodies := []string{
"a", "`", "``", "```", "````", "`````",
strings.Repeat("`", 20) + "text" + strings.Repeat("`", 7),
}
for _, b := range bodies {
f := fenceFor(b)
if strings.Contains(b, f) {
t.Errorf("fence %q appears in body %q", f, b)
}
}
}
func TestSplitTargets(t *testing.T) {
mk := func() *flag.FlagSet {
fs := flag.NewFlagSet("export", flag.ContinueOnError)
fs.String("format", "md", "")
fs.Bool("details", false, "")
fs.Int("n", 20, "")
filterFlags(fs)
return fs
}
cases := []struct {
args, wantFlags, wantTargets []string
}{
{[]string{"-2", "--exit", "1", "5"}, []string{"--exit", "1"}, []string{"-2", "5"}},
{[]string{"-n", "5", "-1"}, []string{"-n", "5"}, []string{"-1"}},
{[]string{"--format=md", "3"}, []string{"--format=md"}, []string{"3"}},
{[]string{"--details", "-1"}, []string{"--details"}, []string{"-1"}},
{[]string{"--since", "30m", "--exit", "fail"}, []string{"--since", "30m", "--exit", "fail"}, nil},
{[]string{"--session", "3"}, []string{"--session", "3"}, nil},
{[]string{"-1", "-2", "-3"}, nil, []string{"-1", "-2", "-3"}},
{[]string{"--nosuchflag", "7"}, []string{"--nosuchflag"}, []string{"7"}},
}
for _, c := range cases {
gotFlags, gotTargets := splitTargets(mk(), c.args)
if !reflect.DeepEqual(gotFlags, c.wantFlags) || !reflect.DeepEqual(gotTargets, c.wantTargets) {
t.Errorf("splitTargets(%q) = %q, %q; want %q, %q",
c.args, gotFlags, gotTargets, c.wantFlags, c.wantTargets)
}
}
}
func TestExportFilterMode(t *testing.T) {
t.Setenv("BACKSCROLL_DB", filepath.Join(t.TempDir(), "test.db"))
st, err := store.Open()
if err != nil {
t.Fatalf("open: %v", err)
}
sess, err := st.NewSession("/bin/bash", "xterm")
if err != nil {
t.Fatalf("session: %v", err)
}
base := time.Now().Add(-time.Hour)
add := func(cmd string, exit int, out string) {
t.Helper()
at := base
base = base.Add(time.Minute)
if err := st.AddCommand(sess, cmd, "/tmp", exit, true, at, at.Add(time.Second),
[]byte(out), false, out); err != nil {
t.Fatalf("AddCommand(%q): %v", cmd, err)
}
}
add("true", 0, "")
add("fail-one", 1, "first failure\n")
add("true again", 0, "ok\n")
add("fail-two", 2, "second failure\n")
st.Close()
out := filepath.Join(t.TempDir(), "out.md")
if err := cmdExport([]string{"--exit", "fail", "-o", out}); err != nil {
t.Fatalf("cmdExport: %v", err)
}
got, err := os.ReadFile(out)
if err != nil {
t.Fatal(err)
}
i1 := bytes.Index(got, []byte("fail-one"))
i2 := bytes.Index(got, []byte("fail-two"))
if i1 < 0 || i2 < 0 {
t.Fatalf("missing failures in export:\n%s", got)
}
if i1 > i2 {
t.Errorf("want oldest-first order, got:\n%s", got)
}
if bytes.Contains(got, []byte("true again")) {
t.Errorf("non-failing command leaked into filtered export:\n%s", got)
}
// ids + filters is an error
if err := cmdExport([]string{"-1", "--exit", "fail"}); err == nil {
t.Error("want error when mixing ids and filters")
}
// no matches is an error
if err := cmdExport([]string{"--exit", "42"}); err == nil {
t.Error("want error when no commands match")
}
}