forked from charmbracelet/vhs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing.go
54 lines (45 loc) · 1.38 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
package main
import (
"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() {
// Create output file (once)
once.Do(func() {
err := os.MkdirAll(filepath.Dir(v.Options.Test.Output), os.ModePerm)
if err != nil {
file, _ = os.CreateTemp(os.TempDir(), "vhs-*.txt")
return
}
file, _ = os.Create(v.Options.Test.Output)
})
// 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
}
for _, line := range buf.Value.Arr() {
str := line.Str()
_, _ = file.WriteString(str + "\n")
}
_, _ = file.WriteString(separator + "\n")
}