-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhelpers_windows_test.go
More file actions
165 lines (162 loc) · 4.03 KB
/
helpers_windows_test.go
File metadata and controls
165 lines (162 loc) · 4.03 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 termtest
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_cleanPtySequences(t *testing.T) {
tests := []struct {
name string
b []byte
cursorPos int
want []byte
wantCursorPos int
wantCleanUpto int
}{
{
"Window title, cursor after",
[]byte("\u001B]0;C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\2642502767\\cache\\94dd3fa4\\exec\\python3.exe\u0007Hello"),
86, // First two characters of Hello
[]byte("Hello"),
2,
5,
},
{
"Window title, cursor preceding",
[]byte("Hello\u001B]0;C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\2642502767\\cache\\94dd3fa4\\exec\\python3.exe\u0007World"),
1, // First two characters of Hello
[]byte("HelloWorld"),
1,
10,
},
{
"Window title, cursor on top",
[]byte("Hello\u001B]0;C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\2642502767\\cache\\94dd3fa4\\exec\\python3.exe\u0007World"),
10, // Inside title escape sequence
[]byte("HelloWorld"),
4,
10,
},
{
"Backspace character",
[]byte("Foo \u0008Bar"),
7, // End of string
[]byte("FooBar"),
5,
6,
},
{
"Backspace character, cursor on top of backspace",
[]byte("Foo \u0008Bar"),
5, // End of string
[]byte("FooBar"),
3,
6,
},
{
"Cursor position preceding cleaned sequence",
[]byte("Foo\u001B[1mBar"), // \u001B[1m = bold
2, // End of "Foo"
[]byte("FooBar"),
2,
6,
},
{
"Cursor position succeeding cleaned sequence",
[]byte("Foo\u001B[1mBar"), // \u001B[1m = bold
9, // End of "Bar"
[]byte("FooBar"),
5,
6,
},
{
"Cursor position on top of cleaned sequence",
[]byte("Foo\u001B[1mBar"), // \u001B[1m = bold
4, // Unicode code point
[]byte("FooBar"),
2,
6,
},
{
"Negative cursor position",
[]byte("Foo\u001B[1mBar"), // \u001B[1m = bold
-10, // End of "Foo"
[]byte("FooBar"),
-10,
6,
},
{
// Running on ANSI escape codes obviously is not the intent, but without being able to easily identify
// which is which this can be error-prone so we need to ensure this doesn't cause panics
"Doesnt break if running on ANSI escape codes",
[]byte("25h 25l █ Installing Runtime (Unconfigured) 25h 25l █25h 25l █ Installing Runtime Environment 25h 25l Setting Up Runtime \n Resolving Dependencies | 25h"),
165,
[]byte("25h 25l █ Installing Runtime (Unconfigured)25h 25l █25h 25l █ Installing Runtime Environment25h 25l Setting Up Runtime \n Resolving Dependencies |25h"),
159,
158,
},
{
"Escape at first character",
[]byte("\u001B[1mfoo"),
0,
[]byte("foo"),
// Since the cleaner handles an absolute cursor position against relative output, we can't determine start
// of output and so we return a negative
-1,
3,
},
{
"Cursor character (NOT position)",
[]byte("foo\u001B[?25hbar"),
0,
[]byte("foobar"),
0,
6,
},
{
"Home key",
[]byte("\x1b[Hfoo"),
0,
[]byte("foo"),
-1,
3,
},
{
"Home key with Window title following",
[]byte("\x1b[H\x1b]0;C:\\Windows\\System32\\cmd.exe\afoo"),
0,
[]byte("foo"),
-1,
3,
},
{
"Alert / bell character",
[]byte("\aP\x1b[?25lython 3.9.5"),
0,
[]byte("Python 3.9.5"),
-1,
12,
},
{
"Unterminated escape sequence",
[]byte("foo\x1b[?25"),
0,
[]byte("foo\x1b[?25"),
0,
3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.cursorPos > len(tt.b) {
t.Fatal("cursor position cannot be larger than input")
}
if tt.wantCursorPos > len(tt.want) {
t.Fatal("Wanted cursor position cannot be larger than wanted output")
}
cleaned, cursorPos, cleanUptoPos := cleanPtySnapshot(tt.b, tt.cursorPos, false)
assert.Equal(t, string(tt.want), string(cleaned), "wanted output")
assert.Equal(t, tt.wantCursorPos, cursorPos, "wanted cursor position")
assert.Equal(t, tt.wantCleanUpto, cleanUptoPos, "wanted clean upto position")
})
}
}