Repository navigation
Expand file tree
/
Copy pathpane_test.go
More file actions
165 lines (155 loc) · 5.3 KB
/
Copy pathpane_test.go
File metadata and controls
165 lines (155 loc) · 5.3 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/xeind/rex-fingers/internal/screen"
)
// A run left on a tty is taken over when the pane still shows what it
// painted: its capture replaces the new one under the new paths, with the
// modes the pane had. A changed pane drops the run and its files.
func TestRecoverLeftoverRun(t *testing.T) {
t.Setenv("TMPDIR", t.TempDir())
dir := t.TempDir()
tty := "/dev/ttys-test"
write := func(name, content string) string {
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
return path
}
begin := func() (paneSpec, paneSpec) {
old := paneSpec{tty: tty, vtFile: write("old.vt", "see /tmp/a\r\nend"), textFile: write("old.txt", "see /tmp/a\nend"),
rows: 2, cols: 20, cursor: true, wrap: true}
if err := old.saveRun([]string{"see stmp/a", "end"}, ""); err != nil {
t.Fatal(err)
}
// The new capture is the stale frame, read with the modes Begin left.
fresh := paneSpec{tty: tty, vtFile: write("new.vt", "see stmp/a\r\nend"), textFile: write("new.txt", "see stmp/a\nend"),
rows: 2, cols: 20, cursor: false, wrap: false}
if err := fresh.load(); err != nil {
t.Fatal(err)
}
return old, fresh
}
old, fresh := begin()
if err := fresh.recover(); err != nil {
t.Fatal(err)
}
if !fresh.recovered || !fresh.cursor || !fresh.wrap {
t.Fatalf("recovered %v cursor %v wrap %v", fresh.recovered, fresh.cursor, fresh.wrap)
}
if got := fresh.scr.Lines[0].Text; got != "see /tmp/a" {
t.Fatalf("screen after recovery: %q", got)
}
for _, gone := range []string{old.vtFile, old.textFile, runFile(tty)} {
if _, err := os.Stat(gone); err == nil {
t.Errorf("%s still exists", gone)
}
}
old, fresh = begin()
fresh.scr = screen.Parse("see stmp/a\r\n$ ls", 2)
if err := fresh.recover(); err != nil {
t.Fatal(err)
}
if fresh.recovered || fresh.cursor {
t.Fatalf("a changed pane must drop the run: %+v", fresh)
}
if _, err := os.Stat(old.vtFile); err == nil {
t.Error("dropped run's capture still exists")
}
if _, err := os.Stat(fresh.vtFile); err != nil {
t.Error("new capture was removed")
}
}
// A pane that scrolled two rows while the hints were up: the rows that
// still show the frame are found at the offset, the new output is not.
func TestAlignRowsFindsTheScroll(t *testing.T) {
painted := []string{"$ ls", "atmp/a", "stmp/b", "dtmp/c", "$ ", ""}
cur := []string{"stmp/b", "dtmp/c", "$ echo hi", "hi", "$ ", ""}
rows := alignRows(cur, painted)
if len(rows) != 2 || rows[0] != 2 || rows[1] != 3 {
t.Fatalf("rows %v, want {0:2 1:3}", rows)
}
if rows := alignRows(painted, painted); len(rows) != 5 || rows[1] != 1 {
t.Fatalf("unchanged screen: %v", rows)
}
if rows := alignRows([]string{"x", "y"}, painted); len(rows) != 0 {
t.Fatalf("replaced screen: %v", rows)
}
}
// The pane's capture runs from the top of the scrollback to the last
// screen row, blank rows included. A screen with scrollback above and
// blank rows below, unchanged since the paint, has not moved, so restore
// repaints all of it.
func TestCheckMovedReadsTheScreenRows(t *testing.T) {
t.Setenv("TMPDIR", t.TempDir())
tty := "/dev/ttys-test"
p := paneSpec{tty: tty, rows: 5}
if err := p.saveRun([]string{"atmp/a", "$", "", "", ""}, ""); err != nil {
t.Fatal(err)
}
p.currentFile = filepath.Join(t.TempDir(), "current.vt")
vt := "1\r\n2\r\n3\r\n\x1b[2m\x1b[1;38;5;0;48;5;9ma\x1b[0;2mtmp/a\x1b[0m\r\n$ \r\n\r\n\r\n"
if err := os.WriteFile(p.currentFile, []byte(vt), 0o600); err != nil {
t.Fatal(err)
}
p.checkMoved()
if p.moved {
t.Fatalf("unchanged screen read as moved; rows %v", p.softRows)
}
}
func TestShowsPainted(t *testing.T) {
scr := screen.Parse("see stmp/a \r\n\r\n", 3)
if !showsPainted(scr, []string{"see stmp/a", "", ""}) {
t.Error("trailing blanks must not matter")
}
if !showsPainted(scr, []string{"see stmp/a"}) {
t.Error("missing trailing rows must count as blank")
}
if showsPainted(scr, []string{"see /tmp/a", "", ""}) || showsPainted(scr, nil) {
t.Error("a different frame, or none, must not match")
}
}
func TestParsePaneSpec(t *testing.T) {
p, err := parsePaneSpec("pid=42,vt=/tmp/x.vt,rows=24,cols=80,cursor=false,wrap=true")
if err != nil {
t.Fatal(err)
}
if p.pid != 42 || p.vtFile != "/tmp/x.vt" || p.rows != 24 || p.cols != 80 || p.cursor || !p.wrap {
t.Fatalf("got %+v", p)
}
p, err = parsePaneSpec("tty=/dev/ttys003,vt=/tmp/y.vt,rows=10")
if err != nil || p.tty != "/dev/ttys003" || !p.cursor || !p.wrap {
t.Fatalf("defaults: %+v, %v", p, err)
}
for _, bad := range []string{"rows", "pid=x,vt=a,rows=1", "size=3,vt=a,rows=1"} {
if _, err := parsePaneSpec(bad); err == nil {
t.Errorf("parsePaneSpec(%q) accepted", bad)
}
}
}
func TestKeyBytes(t *testing.T) {
cases := []struct {
key, mod string
want []byte
}{
{"a", "main", []byte("a")},
{"a", "shift", []byte("A")},
{"a", "ctrl", []byte{0x01}},
{"z", "alt", []byte{0x1b, 'z'}},
}
for _, c := range cases {
got, err := keyBytes(c.key, c.mod)
if err != nil || !bytes.Equal(got, c.want) {
t.Errorf("keyBytes(%q, %q) = %q, %v; want %q", c.key, c.mod, got, err, c.want)
}
}
for _, bad := range [][2]string{{"A", "main"}, {"ab", "main"}, {"a", "super"}} {
if _, err := keyBytes(bad[0], bad[1]); err == nil {
t.Errorf("keyBytes(%q, %q) accepted", bad[0], bad[1])
}
}
}