-
Notifications
You must be signed in to change notification settings - Fork 261
/
testing.go
91 lines (76 loc) · 2.34 KB
/
testing.go
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
package main
import (
"fmt"
"os"
"path/filepath"
"sync"
)
// TestOptions is the set of options for the testing functionality.
type TestOptions struct {
Output string
Golden string
}
// DefaultTestOptions returns the default set of options for the testing functionality.
func DefaultTestOptions() TestOptions {
return TestOptions{
Output: "out.test",
}
}
// Alternatively, `var separator = strings.Repeat("─", 80)`.
const separator = "────────────────────────────────────────────────────────────────────────────────"
var (
once sync.Once
file *os.File
)
// SaveOutput saves the current buffer to the output file.
func (v *VHS) SaveOutput() error {
var err error
// Create output file (once)
once.Do(func() {
err = os.MkdirAll(filepath.Dir(v.Options.Test.Output), os.ModePerm)
if err != nil {
file, err = os.CreateTemp(os.TempDir(), "vhs-*.txt")
return
}
file, err = os.Create(v.Options.Test.Output)
})
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
lines, err := v.Buffer()
if err != nil {
return fmt.Errorf("failed to get buffer: %w", err)
}
for _, line := range lines {
_, err = file.WriteString(line + "\n")
if err != nil {
return fmt.Errorf("failed to write buffer to file: %w", err)
}
}
_, err = file.WriteString(separator + "\n")
if err != nil {
return fmt.Errorf("failed to write separator to file: %w", err)
}
return nil
}
// Buffer returns the current buffer.
func (v *VHS) Buffer() ([]string, error) {
// Get the current buffer.
buf, err := v.Page.Eval("() => Array(term.rows).fill(0).map((e, i) => term.buffer.active.getLine(i).translateToString().trimEnd())")
if err != nil {
return nil, fmt.Errorf("read buffer: %w", err)
}
var lines []string
for _, line := range buf.Value.Arr() {
lines = append(lines, line.Str())
}
return lines, nil
}
// CurrentLine returns the current line from the buffer.
func (v *VHS) CurrentLine() (string, error) {
buf, err := v.Page.Eval("() => term.buffer.active.getLine(term.buffer.active.cursorY+term.buffer.active.viewportY).translateToString().trimEnd()")
if err != nil {
return "", fmt.Errorf("read current line from buffer: %w", err)
}
return buf.Value.Str(), nil
}