Repository navigation
Expand file tree
/
Copy pathcond_test.go
More file actions
516 lines (495 loc) · 16.9 KB
/
Copy pathcond_test.go
File metadata and controls
516 lines (495 loc) · 16.9 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package inputrc
import (
"errors"
"os"
"os/user"
"path/filepath"
"runtime"
"slices"
"strconv"
"testing"
)
func TestIfTests(t *testing.T) {
tests := []struct {
test string
opts []Option
exp bool
}{
{"version >= 8.0", nil, true},
{"version >= 8.3", nil, true},
{"version > 8.3", nil, false},
{"version == 8", nil, false},
{"version != 7.0", nil, true},
{"version<9", nil, true},
{"version <= 8.2", []Option{WithVersion("8.2")}, true},
{"version >= 8.0", []Option{WithVersion("7.1")}, false},
{"editing-mode == emacs", nil, true},
{"editing-mode == vi", nil, false},
{"editing-mode != vi", nil, true},
{"editing-mode=emacs", nil, true},
{"enable-bracketed-paste == on", nil, true},
{"no-such-variable == on", nil, false},
{"term=xterm", []Option{WithTerm("xterm-256color")}, true},
{"term=xterm-256color", []Option{WithTerm("xterm-256color")}, true},
{"term=screen", []Option{WithTerm("xterm-256color")}, false},
{"bash", []Option{WithApp("bash")}, true},
{"bash # comment", []Option{WithApp("bash")}, true},
{"Bash", []Option{WithApp("bash")}, true},
{"bash", []Option{WithApp("Bash")}, true},
{"Bash", []Option{WithApp("Bash")}, true},
{"Bas", []Option{WithApp("Bash")}, false},
}
for _, test := range tests {
cfg := NewDefaultConfig()
src := "$if " + test.test + "\nset blink-matching-paren on\n$endif\n"
if err := ParseBytes([]byte(src), cfg, test.opts...); err != nil {
t.Fatalf("%q: expected no error, got: %v", test.test, err)
}
if got := cfg.GetBool("blink-matching-paren"); got != test.exp {
t.Errorf("%q: expected %t, got %t", test.test, test.exp, got)
}
}
}
func TestMetaIsEscPrefix(t *testing.T) {
tests := map[string]string{
`\M-a`: "\x1ba",
`\M-\C-x`: "\x1b\x18",
`\C-\M-x`: "\x1b\x18",
`\M-\C-?`: "\x1b\x1f",
}
for s, exp := range tests {
if got := Unescape(s); got != exp {
t.Errorf("Unescape(%q): expected %q, got %q", s, exp, got)
}
}
cfg := NewConfig()
if err := ParseBytes([]byte("Meta-a: foo\n"), cfg); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if _, ok := cfg.Binds["emacs"]["\x1ba"]; !ok {
t.Errorf("expected Meta-a to bind Esc a, got: %v", cfg.Binds)
}
}
func TestUnescapeBytes(t *testing.T) {
tests := map[string]string{
`\C-\\`: "\x1c",
`\C-\`: "\x1c",
`\C-\\a`: "\x1ca",
`\xff`: "\xff",
`\xe2`: "\xe2",
`\x4`: "\x04",
`\x4z`: "\x04z",
`\377`: "\xff",
`\341`: "\xe1",
`\341b`: "\xe1b",
`\101`: "A",
"\u00e1": "\u00e1",
}
for s, exp := range tests {
if got := Unescape(s); got != exp {
t.Errorf("Unescape(%q): expected %q, got %q", s, exp, got)
}
}
// a key above 0x7f is the byte itself in a bind, as in readline
cfg := NewConfig()
if err := ParseBytes([]byte("\"\\341\": foo\n\"\\C-\\\\\": bar\n"), cfg, WithHaltOnErr(true)); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
for _, seq := range []string{"\xe1", "\x1c"} {
if _, ok := cfg.Binds["emacs"][seq]; !ok {
t.Errorf("expected a bind for %q, got: %v", seq, cfg.Binds["emacs"])
}
}
// Escape and Unescape are inverses, for bytes that are not UTF-8 too
for _, s := range []string{"\xe1", "\xff\x1c", "a\x00b", "\u00e1", "\x1b[A", "\x80"} {
if got := Unescape(Escape(s)); got != s {
t.Errorf("Unescape(Escape(%q)): got %q (escaped %q)", s, got, Escape(s))
}
}
}
type locConfig struct {
*Config
name string
line int
seen []string
}
func (c *locConfig) Locate(name string, line int) { c.name, c.line = name, line }
func (c *locConfig) Set(name string, value any) error {
c.seen = append(c.seen, name+"@"+c.name+":"+strconv.Itoa(c.line))
return c.Config.Set(name, value)
}
func TestLocator(t *testing.T) {
cfg := &locConfig{Config: NewDefaultConfig()}
src := "# comment\nset mark-directories off\n\nset convert-meta on\n"
if err := ParseBytes([]byte(src), cfg, WithName("x.inputrc")); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
exp := []string{"mark-directories@x.inputrc:2", "convert-meta@x.inputrc:4"}
if !slices.Equal(cfg.seen, exp) {
t.Errorf("expected %v, got %v", exp, cfg.seen)
}
}
func TestCommands(t *testing.T) {
cmds := Commands()
if len(cmds) != 144 {
t.Errorf("expected 144 commands, got %d", len(cmds))
}
if !slices.IsSorted(cmds) {
t.Error("expected commands to be sorted")
}
for _, binds := range DefaultBinds() {
for seq, b := range binds {
if b.Macro {
continue
}
if _, ok := slices.BinarySearch(cmds, b.Action); !ok {
t.Errorf("default bind %q names unknown command %q", Escape(seq), b.Action)
}
}
}
}
func TestSetValues(t *testing.T) {
tests := []struct {
src string
name string
exp any
}{
{"set completion-query-items 3\n", "completion-query-items", 3},
{"set isearch-terminators x\n", "isearch-terminators", "x"},
{"set isearch-terminators \"\\C-a\"\n", "isearch-terminators", `\C-a`},
{"set isearch-terminators '\\C-a'\n", "isearch-terminators", `\C-a`},
{"set comment-begin \"# \"\n", "comment-begin", "# "},
{"set bell-style none # comment\n", "bell-style", "none"},
}
for _, test := range tests {
cfg := NewDefaultConfig()
if err := ParseBytes([]byte(test.src), cfg, WithHaltOnErr(true)); err != nil {
t.Fatalf("%q: expected no error, got: %v", test.src, err)
}
if got := cfg.Vars[test.name]; got != test.exp {
t.Errorf("%q: expected %#v, got %#v", test.src, test.exp, got)
}
}
}
func TestBellStyle(t *testing.T) {
tests := []struct {
src string
exp string
}{
{"set bell-style none\nset prefer-visible-bell on\n", "visible"},
{"set prefer-visible-bell on\nset bell-style none\n", "none"},
{"set bell-style none\nset prefer-visible-bell off\n", "audible"},
{"set bell-style on\n", "audible"},
{"set bell-style off\n", "none"},
{"set bell-style VISIBLE\n", "visible"},
{"set bell-style None\n", "none"},
{"set bell-style none\nset bell-style foo\n", "none"},
{"set bell-style\n", "audible"},
}
for _, test := range tests {
cfg := NewDefaultConfig()
if err := ParseBytes([]byte(test.src), cfg, WithHaltOnErr(true)); err != nil {
t.Fatalf("%q: expected no error, got: %v", test.src, err)
}
if got := cfg.GetString("bell-style"); got != test.exp {
t.Errorf("%q: expected %q, got %q", test.src, test.exp, got)
}
}
}
// TestFaults checks the faults that readline 8.3 reports, with its wording.
func TestFaults(t *testing.T) {
tests := []struct {
src string
exp string // the readline message, or empty when readline says nothing
}{
{"set keymap nothing\n", "keymap: could not set value to `nothing'"},
{"set editing-mode foo\n", "editing-mode: could not set value to `foo'"},
{"set editing-mode\n", "editing-mode: could not set value to `'"},
{"set comment-begin\n", "comment-begin: could not set value to `'"},
{"set nosuch on\n", "nosuch: unknown variable name"},
{"$foo\n", "foo: unknown parser directive"},
{"$endif\n", "$endif without matching $if"},
{"$else\n", "$else found without matching $if"},
{"foo\n", "foo: no key sequence terminator"},
{"\"a\"\n", "\"a\": no key sequence terminator"},
{"\"abc\n", "\"abc: no closing `\"' in key binding"},
{"Bogus-x: end-of-line\n", "Bogus-x: unknown key modifier"},
// readline says nothing about these
{"\\C-xd end-of-line\n", ""},
{"foo bar\n", ""},
{"\"\\C-xd\": nosuch\n", ""},
{"set bell-style\n", ""},
{"set completion-query-items abc\n", ""},
{"set isearch-terminators x\n", ""},
{"$if\n$endif\n", ""},
{"$include\n", ""},
{"$include /missing\n", ""},
{"$include denied\n", ""},
{"\"a\": \"abc\n", "`\"abc': missing closing quote for macro"},
{"\"a\": 'abc\n", "`'abc': missing closing quote for macro"},
}
for _, test := range tests {
cfg := NewDefaultConfig()
err := ParseBytes([]byte(test.src), cfg, WithName("f"))
if test.exp == "" {
if err != nil {
t.Errorf("%q: expected no error, got: %v", test.src, err)
}
continue
}
var es errs
var pe *ParseError
if !errors.As(err, &es) || len(es) != 1 || !errors.As(err, &pe) {
t.Fatalf("%q: expected one parse error, got: %v", test.src, err)
}
if pe.Message() != test.exp || pe.Name != "f" || pe.Line != 1 {
t.Errorf("%q: expected f line 1 %q, got %s line %d %q", test.src, test.exp, pe.Name, pe.Line, pe.Message())
}
}
}
func TestSeparators(t *testing.T) {
// readline ends a key at a colon or at a space, so a space works too
tests := []struct {
src string
seq string
act string
}{
{"\"\\C-xa\" end-of-line\n", "\x18a", "end-of-line"},
{"\"\\C-xa\": end-of-line\n", "\x18a", "end-of-line"},
{"foo end-of-line\n", "f", "end-of-line"},
{"\\C-xd end-of-line\n", "\x18", "end-of-line"},
{"Control-x end-of-line\n", "\x18", "end-of-line"},
{"\\C-xd: end-of-line\n", "\x18", "end-of-line"},
}
for _, test := range tests {
cfg := NewDefaultConfig()
if err := ParseBytes([]byte(test.src), cfg); err != nil {
t.Fatalf("%q: expected no error, got: %v", test.src, err)
}
if b, ok := cfg.Binds["emacs"][test.seq]; !ok || b.Action != test.act {
t.Errorf("%q: expected %q to be %q, got %v", test.src, test.seq, test.act, cfg.Binds["emacs"][test.seq])
}
}
}
func TestAtoi(t *testing.T) {
tests := []struct {
s string
exp int
}{
{"12", 12}, {"12abc", 12}, {"abc", 0}, {"", 0}, {"\t7", 7}, {"-3x", -3}, {"+4", 4}, {"-", 0},
}
for _, test := range tests {
if got := atoi(test.s); got != test.exp {
t.Errorf("atoi(%q): expected %d, got %d", test.s, test.exp, got)
}
}
cfg := NewDefaultConfig()
if err := ParseBytes([]byte("set completion-query-items 12abc\n"), cfg); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if got := cfg.Vars["completion-query-items"]; got != 12 {
t.Errorf("expected 12, got %v", got)
}
}
func TestNumbersAndBools(t *testing.T) {
tests := []struct {
src string
name string
exp any
}{
{"set mark-directories\n", "mark-directories", true},
{"set blink-matching-paren\n", "blink-matching-paren", true},
{"set blink-matching-paren ON\n", "blink-matching-paren", true},
{"set blink-matching-paren 1\n", "blink-matching-paren", true},
{"set blink-matching-paren yes\n", "blink-matching-paren", false},
{"set mark-directories off\n", "mark-directories", false},
{"set history-size 99999999999999999999\n", "history-size", -1},
{"set history-size 2147483647\n", "history-size", 2147483647},
{"set history-size 2147483648\n", "history-size", -1},
{"set history-size -5\n", "history-size", -1},
{"set history-size 4294967297\n", "history-size", 1},
{"set history-size -2147483649\n", "history-size", 2147483647},
{"set keyseq-timeout 2147483648\n", "keyseq-timeout", 0},
{"set keyseq-timeout -5\n", "keyseq-timeout", 0},
{"set keyseq-timeout 4294967297\n", "keyseq-timeout", 1},
{"set completion-query-items -5\n", "completion-query-items", 0},
{"set completion-query-items 4294967297\n", "completion-query-items", 1},
{"set completion-prefix-display-length -5\n", "completion-prefix-display-length", 0},
{"set completion-display-width -5\n", "completion-display-width", -5},
{"set completion-display-width 2147483648\n", "completion-display-width", -2147483648},
{"set completion-display-width abc\n", "completion-display-width", 0},
}
for _, test := range tests {
cfg := NewDefaultConfig()
if err := ParseBytes([]byte(test.src), cfg); err != nil {
t.Fatalf("%q: expected no error, got: %v", test.src, err)
}
if got := cfg.Vars[test.name]; got != test.exp {
t.Errorf("%q: expected %#v, got %#v", test.src, test.exp, got)
}
}
}
func TestIncludeLoop(t *testing.T) {
read := func(name string) ([]byte, error) {
return []byte("$include self\nset blink-matching-paren on\n"), nil
}
cfg := NewDefaultConfig(WithConfigReadFileFunc(read))
err := ParseBytes([]byte("$include self\n"), cfg, WithName("top"))
if !errors.Is(err, ErrIncludeTooDeep) {
t.Fatalf("expected the include to be too deep, got: %v", err)
}
var pe *ParseError
if !errors.As(err, &pe) || pe.Name != "self" || pe.Line != 1 {
t.Errorf("expected a fault at self line 1, got: %v", err)
}
if !cfg.GetBool("blink-matching-paren") {
t.Error("expected the lines after the include to be read")
}
}
func TestIncludeFaults(t *testing.T) {
files := map[string]string{
"inc": "set bell-style none\n\n$foo\n",
"inner": "set keymap nothing\n",
"outer": "$include inner\n$bar\n",
}
read := func(name string) ([]byte, error) {
if s, ok := files[name]; ok {
return []byte(s), nil
}
if name == "denied" {
return nil, os.ErrPermission
}
return nil, os.ErrNotExist
}
cfg := NewDefaultConfig(WithConfigReadFileFunc(read))
src := "$include inc\n$include outer\n$include denied\n$baz\n"
err := ParseBytes([]byte(src), cfg, WithName("top"))
var es errs
if !errors.As(err, &es) {
t.Fatalf("expected a list of errors, got: %v", err)
}
exp := []string{
"inc:3:foo: unknown parser directive",
"inner:1:keymap: could not set value to `nothing'",
"outer:2:bar: unknown parser directive",
"top:4:baz: unknown parser directive",
}
msgs := make([]string, 0, len(es))
for _, err := range es {
var pe *ParseError
if !errors.As(err, &pe) {
t.Fatalf("expected a *ParseError, got %T: %v", err, err)
}
msgs = append(msgs, pe.Name+":"+strconv.Itoa(pe.Line)+":"+pe.Message())
}
if !slices.Equal(msgs, exp) {
t.Errorf("expected %q, got %q", exp, msgs)
}
// errors.Is looks through the list, and the good lines still reached the handler
if !errors.Is(err, ErrUnknownDirective) || !errors.Is(err, ErrInvalidKeymap) {
t.Errorf("expected errors.Is to find the faults in %v", err)
}
if got := cfg.GetString("bell-style"); got != "none" {
t.Errorf("expected bell-style none, got %q", got)
}
// halting stops at the first fault, even in an included file
err = ParseBytes([]byte(src), NewDefaultConfig(WithConfigReadFileFunc(read)), WithName("top"), WithHaltOnErr(true))
if !errors.As(err, &es) || len(es) != 1 {
t.Errorf("expected one error when halting, got: %v", err)
}
// no faults is a nil error, not an empty list
if err := ParseBytes([]byte("set bell-style none\n"), NewDefaultConfig()); err != nil {
t.Errorf("expected nil, got: %#v", err)
}
}
func TestKeymapAfterSet(t *testing.T) {
tests := []struct {
src string
keymap string
seq string
}{
{"set keymap emacs-meta\n\"a\": foo\n", "emacs-meta", "a"},
{"set editing-mode vi\n\"\\C-xq\": kill-line\n", "vi-insert", "\x18q"},
{"set editing-mode vi\nset editing-mode emacs\n\"\\C-xq\": kill-line\n", "emacs", "\x18q"},
{"d: backward-char\n", "emacs", "d"},
{"\"d\": x\n", "emacs", "d"},
}
for _, test := range tests {
cfg := NewDefaultConfig()
if err := ParseBytes([]byte(test.src), cfg, WithHaltOnErr(true)); err != nil {
t.Fatalf("%q: expected no error, got: %v", test.src, err)
}
if _, ok := cfg.Binds[test.keymap][test.seq]; !ok {
t.Errorf("%q: expected a bind in %s for %q", test.src, test.keymap, test.seq)
}
}
}
func TestModeAfterSet(t *testing.T) {
const vi = "set editing-mode vi\n$if mode=vi\nset keymap vi-command\n\"d\": probe\n$endif\n"
tests := []struct {
name string
src string
exp bool
}{
{"mode", vi, true},
{"mode operator", "set editing-mode vi\n$if mode == vi\nset blink-matching-paren on\n$endif\n", true},
{"back to emacs", "set editing-mode vi\nset editing-mode emacs\n$if mode=vi\nset blink-matching-paren on\n$endif\n", false},
{"include", "set editing-mode vi\n$include inc\n", true},
}
read := func(name string) ([]byte, error) {
if name == "inc" {
return []byte(vi), nil
}
return nil, os.ErrNotExist
}
for _, test := range tests {
cfg := NewDefaultConfig(WithConfigReadFileFunc(read))
if err := ParseBytes([]byte(test.src), cfg, WithMode("emacs")); err != nil {
t.Fatalf("%s: expected no error, got: %v", test.name, err)
}
bound := cfg.Binds["vi-command"]["d"].Action == "probe"
if matched := cfg.GetBool("blink-matching-paren"); !test.exp && matched {
t.Errorf("%s: expected the $if to be false", test.name)
} else if test.exp && !bound && !matched {
t.Errorf("%s: expected the $if to be true", test.name)
}
}
}
func TestUserDefaultInputrc(t *testing.T) {
home := ".inputrc"
if runtime.GOOS == "windows" {
home = "_inputrc"
}
files := map[string]string{
"/custom/inputrc": "set bell-style none\n",
filepath.Join("/home/ken", home): "set bell-style visible\n",
}
read := func(name string) ([]byte, error) {
if s, ok := files[name]; ok {
return []byte(s), nil
}
return nil, os.ErrNotExist
}
u := &user.User{HomeDir: "/home/ken"}
tests := []struct {
env string
exp string
}{
// INPUTRC names a file that exists, so only it is read
{"/custom/inputrc", "none"},
// INPUTRC names a missing file, so nothing is read and there is no fallback
{"/missing/inputrc", "audible"},
// INPUTRC is empty, so the home file is read
{"", "visible"},
}
for _, test := range tests {
t.Setenv("INPUTRC", test.env)
cfg := NewDefaultConfig(WithConfigReadFileFunc(read))
if err := UserDefault(u, cfg); err != nil {
t.Fatalf("INPUTRC=%q: expected no error, got: %v", test.env, err)
}
if got := cfg.GetString("bell-style"); got != test.exp {
t.Errorf("INPUTRC=%q: expected bell-style %q, got %q", test.env, test.exp, got)
}
}
}