File: terminfo_test.go

package info (click to toggle)
golang-github-xo-terminfo 0.0~git20210125.ca9a967-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 256 kB
  • sloc: sh: 50; makefile: 6
file content (392 lines) | stat: -rw-r--r-- 10,316 bytes parent folder | download | duplicates (2)
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
package terminfo

import (
	"errors"
	"fmt"
	"os/exec"
	"path/filepath"
	"reflect"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"testing"
)

func TestOpen(t *testing.T) {
	for term, filename := range terms(t) {
		t.Run(strings.TrimPrefix(filename, "/"), func(term, filename string) func(*testing.T) {
			return func(t *testing.T) {
				t.Parallel()

				// open
				ti, err := Open(filepath.Dir(filepath.Dir(filename)), term)
				if err != nil {
					t.Fatalf("term %s expected no error, got: %v", term, err)
				}

				if ti.File != filename {
					t.Errorf("term %s should have file %s, got: %s", term, filename, ti.File)
				}

				// check we have at least one name
				if len(ti.Names) < 1 {
					t.Errorf("term %s expected names to have at least one value", term)
				}
			}
		}(term, filename))
	}
}

var infocmpMap = struct {
	ic map[string]*infocmp
	sync.RWMutex
}{
	ic: make(map[string]*infocmp),
}

func TestValues(t *testing.T) {
	// load infocmp data
	err := loadInfocmpData(t)
	if err != nil {
		t.Fatal(err)
	}

	for term, filename := range terms(t) {
		t.Run(strings.TrimPrefix(filename, "/"), func(t *testing.T) {
			t.Parallel()

			infocmpMap.RLock()
			ic := infocmpMap.ic[term]
			infocmpMap.RUnlock()

			// load
			ti, err := Load(term)
			if err != nil {
				t.Fatalf("term %s expected no error, got: %v", term, err)
			}

			// check names
			if !reflect.DeepEqual(ic.names, ti.Names) {
				t.Errorf("term %s names do not match", term)
			}

			// check bool caps
			for i, v := range ic.boolCaps {
				if v == nil {
					if _, ok := ti.BoolsM[i]; !ok {
						t.Errorf("term %s expected bool cap %d (%s) to be missing", term, i, BoolCapName(i))
					}
				} else if v.(bool) != ti.Bools[i] {
					t.Errorf("term %s bool cap %d (%s) should be %t", term, i, BoolCapName(i), v)
				}
			}

			// check extended bool caps
			if len(ic.extBoolCaps) != len(ti.ExtBools) {
				t.Errorf("term %s should have same number of extended bools (%d, %d)", term, len(ic.extBoolCaps), len(ti.ExtBools))
			}
			for i, v := range ic.extBoolCaps {
				z, ok := ti.ExtBools[i]
				if !ok {
					t.Errorf("term %s should have extended bool %d", term, i)
				}
				if v.(bool) != z {
					t.Errorf("term %s extended bool cap %d (%s) should be %t", term, i, ic.extBoolNames[i], v)
				}

				n, ok := ti.ExtBoolNames[i]
				if !ok {
					t.Errorf("term %s missing extended bool %d name", term, i)
				}
				if string(n) != ic.extBoolNames[i] {
					t.Errorf("term %s extended bool %d name should be '%s', got: '%s'", term, i, ic.extBoolNames[i], string(n))
				}
			}

			// check num caps
			for i, v := range ic.numCaps {
				if v == nil {
					if _, ok := ti.NumsM[i]; !ok {
						//t.Errorf("term %s expected num cap %d (%s) to be missing", term, i, NumCapName(i))
					}
				} else if v.(int) != ti.Nums[i] {
					t.Errorf("term %s num cap %d (%s) should be %d", term, i, NumCapName(i), v)
				}
			}

			// check extended num caps
			if len(ic.extNumCaps) != len(ti.ExtNums) {
				t.Errorf("term %s should have same number of extended nums (%d, %d)", term, len(ic.extNumCaps), len(ti.ExtNums))
			}
			for i, v := range ic.extNumCaps {
				z, ok := ti.ExtNums[i]
				if !ok {
					t.Errorf("term %s should have extended num %d", term, i)
				}
				if v.(int) != z {
					t.Errorf("term %s extended num cap %d (%s) should be %t", term, i, ic.extNumNames[i], v)
				}

				n, ok := ti.ExtNumNames[i]
				if !ok {
					t.Errorf("term %s missing extended num %d name", term, i)
				}
				if string(n) != ic.extNumNames[i] {
					t.Errorf("term %s extended num %d name should be '%s', got: '%s'", term, i, ic.extNumNames[i], string(n))
				}
			}

			// check string caps
			for i, v := range ic.stringCaps {
				if v == nil {
					if _, ok := ti.StringsM[i]; !ok {
						//t.Errorf("term %s expected string cap %d (%s) to be missing", term, i, StringCapName(i))
					}
				} else if v.(string) != string(ti.Strings[i]) {
					t.Errorf("term %s string cap %d (%s) is invalid:", term, i, StringCapName(i))
					t.Errorf("got:  %#v", string(ti.Strings[i]))
					t.Errorf("want: %#v", v)
				}
			}

			// check extended string caps
			if len(ic.extStringCaps) != len(ti.ExtStrings) {
				t.Errorf("term %s should have same number of extended strings (%d, %d)", term, len(ic.extStringCaps), len(ti.ExtStrings))
			}
			for i, v := range ic.extStringCaps {
				z, ok := ti.ExtStrings[i]
				if !ok {
					t.Errorf("term %s should have extended string %d", term, i)
				}
				if v.(string) != string(z) {
					t.Errorf("term %s extended string cap %d (%s) should be %t", term, i, ic.extStringNames[i], v)
				}

				n, ok := ti.ExtStringNames[i]
				if !ok {
					t.Errorf("term %s missing extended string %d name", term, i)
				}
				if string(n) != ic.extStringNames[i] {
					t.Errorf("term %s extended string %d name should be '%s', got: '%s'", term, i, ic.extStringNames[i], string(n))
				}
			}
		})
	}
}

func loadInfocmpData(t *testing.T) error {
	// start loaders
	wg := new(sync.WaitGroup)
	termsCh, errs := make(chan string, 64), make(chan error, 64)
	for i := 0; i < runtime.NumCPU()*2; i++ {
		go infocmpLoader(wg, t, i, termsCh, errs)
	}

	for term := range terms(t) {
		wg.Add(1)
		termsCh <- term
	}
	defer close(termsCh)

	wg.Wait()

	var err error
	select {
	case err = <-errs:
	default:
	}

	return err
}

func infocmpLoader(wg *sync.WaitGroup, t *testing.T, id int, terms <-chan string, res chan<- error) {
	for term := range terms {
		ic, err := getInfocmpData(t, term)
		if err != nil {
			res <- fmt.Errorf("loader %d: %v", id, err)
			return
		}

		infocmpMap.Lock()
		infocmpMap.ic[term] = ic
		infocmpMap.Unlock()

		wg.Done()
	}
}

var (
	shortCapNameMap map[string]int
)

type infocmp struct {
	names          []string
	boolCaps       map[int]interface{}
	numCaps        map[int]interface{}
	stringCaps     map[int]interface{}
	extBoolCaps    map[int]interface{}
	extNumCaps     map[int]interface{}
	extStringCaps  map[int]interface{}
	extBoolNames   map[int]string
	extNumNames    map[int]string
	extStringNames map[int]string
}

func init() {
	shortCapNameMap = make(map[string]int)
	for _, z := range [][]string{boolCapNames[:], numCapNames[:], stringCapNames[:]} {
		for i := 0; i < len(z); i += 2 {
			shortCapNameMap[z[i+1]] = i / 2
		}
	}
}

var (
	staticCharRE = regexp.MustCompile(`(?m)^static\s+char\s+(.*)\s*\[\]\s*=\s*(".*");$`)
)

func getInfocmpData(t *testing.T, term string) (*infocmp, error) {
	c := exec.Command("/usr/bin/infocmp", "-E", "-x")
	c.Env = []string{"TERM=" + term}

	buf, err := c.CombinedOutput()
	if err != nil {
		t.Logf("shell error (TERM=%s):\n%s\n", term, string(buf))
		return nil, err
	}

	// read static strings
	m := staticCharRE.FindAllStringSubmatch(string(buf), -1)
	if !strings.HasSuffix(strings.TrimSpace(m[0][1]), "_alias_data") {
		return nil, errors.New("missing _alias_data")
	}

	// some names have " in them, and infocmp -E doesn't correctly escape them
	names, err := strconv.Unquote(`"` + strings.Replace(m[0][2][1:len(m[0][2])-1], `"`, `\"`, -1) + `"`)
	if err != nil {
		return nil, fmt.Errorf("could not unquote _alias_data: %v", err)
	}

	ic := &infocmp{
		names:          strings.Split(names, "|"),
		boolCaps:       make(map[int]interface{}),
		numCaps:        make(map[int]interface{}),
		stringCaps:     make(map[int]interface{}),
		extBoolCaps:    make(map[int]interface{}),
		extNumCaps:     make(map[int]interface{}),
		extStringCaps:  make(map[int]interface{}),
		extBoolNames:   make(map[int]string),
		extNumNames:    make(map[int]string),
		extStringNames: make(map[int]string),
	}

	// load string cap data
	caps := make(map[string]string, len(m))
	for i, s := range m[1:] {
		k := strings.TrimSpace(s[1])
		idx := strings.LastIndex(k, "_s_")
		if idx == -1 {
			return nil, fmt.Errorf("string cap %d (%s) does not contain _s_", i, k)
		}

		v, err := strconv.Unquote(s[2])
		if err != nil {
			return nil, fmt.Errorf("could not unquote %d: %v", i, err)
		}
		caps[k] = v
	}

	// extract the values
	for _, err := range []error{
		processSect(buf, caps, ic.boolCaps, ic.extBoolCaps, ic.extBoolNames, boolSectRE),
		processSect(buf, caps, ic.numCaps, ic.extNumCaps, ic.extNumNames, numSectRE),
		processSect(buf, caps, ic.stringCaps, ic.extStringCaps, ic.extStringNames, stringSectRE),
	} {
		if err != nil {
			return nil, err
		}
	}

	return ic, nil
}

// regexp's used by processSect.
var (
	boolSectRE   = regexp.MustCompile(`_bool_data\[\]\s*=\s*{`)
	numSectRE    = regexp.MustCompile(`_number_data\[\]\s*=\s*{`)
	stringSectRE = regexp.MustCompile(`_string_data\[\]\s*=\s*{`)
	endSectRE    = regexp.MustCompile(`(?m)^};$`)

	capValuesRE = regexp.MustCompile(`(?m)^\s+/\*\s+[0-9]+:\s+([^\s]+)\s+\*/\s+(.*),$`)
	numRE       = regexp.MustCompile(`^[0-9]+$`)
	absCanRE    = regexp.MustCompile(`^(ABSENT|CANCELLED)_(BOOLEAN|NUMERIC|STRING)$`)
)

// processSect processes a text section in the infocmp C export.
func processSect(buf []byte, caps map[string]string, xx, yy map[int]interface{}, extn map[int]string, sectRE *regexp.Regexp) error {
	var err error

	// extract section
	start := sectRE.FindIndex(buf)
	if start == nil || len(start) != 2 {
		return fmt.Errorf("could not find sect (%s)", sectRE)
	}
	end := endSectRE.FindIndex(buf[start[1]:])
	if end == nil || len(end) != 2 {
		return fmt.Errorf("could not find end of section (%s)", sectRE)
	}
	buf = buf[start[1] : start[1]+end[0]]

	// load caps
	m := capValuesRE.FindAllStringSubmatch(string(buf), -1)
	var extc int
	for i, s := range m {
		var skip bool

		// determine target
		target := xx
		k, ok := shortCapNameMap[s[1]]
		if !ok {
			target, k, extn[extc] = yy, extc, s[1]
			extc++
		}

		// get cap value
		var v interface{}
		switch {
		case s[2] == "TRUE" || s[2] == "FALSE":
			v = s[2] == "TRUE"

		case numRE.MatchString(s[2]):
			var j int64
			j, err = strconv.ParseInt(s[2], 10, 16)
			if err != nil {
				return fmt.Errorf("line %d could not parse: %v", i, err)
			}
			v = int(j)

		case absCanRE.MatchString(s[2]):
			if !ok { // absent/canceled extended cap
				if strings.HasSuffix(s[2], "NUMERIC") {
					v = -1
				} else {
					skip = true
				}
			}

		default:
			v, ok = caps[s[2]]
			if !ok {
				return fmt.Errorf("cap '%s' not defined in cap table", s[2])
			}
		}

		if !skip {
			target[k] = v
		}
	}

	return nil
}