-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain_test.go
87 lines (73 loc) · 2.09 KB
/
main_test.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
//go:build !windows && test_with_txtar
package main
import (
"bytes"
"os"
"os/exec"
"regexp"
"strings"
"syscall"
"testing"
"github.com/creack/pty"
"github.com/rogpeppe/go-internal/testscript"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"runme": root,
}))
}
// TestRunme tests runme end-to-end using testscript.
// Check out the package from "import" to learn more.
// More comprehensive tutorial can be found here:
// https://bitfieldconsulting.com/golang/test-scripts
func TestRunme(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/script",
ContinueOnError: true,
})
}
func TestRunmeFlags(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/flags",
ContinueOnError: true,
})
}
func TestRunmeTags(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/tags",
ContinueOnError: true,
})
}
func TestRunmeRunAll(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/runall",
ContinueOnError: true,
})
}
func TestRunmeBeta(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/beta",
ContinueOnError: true,
})
}
func TestSkipPromptsWithinAPty(t *testing.T) {
err := os.Setenv("RUNME_VERBOSE", "false")
require.NoError(t, err)
defer os.Unsetenv("RUNME_VERBOSE")
cmd := exec.Command("go", "run", ".", "run", "skip-prompts-sample", "--chdir", "./examples/frontmatter/skipPrompts")
ptmx, err := pty.StartWithAttrs(cmd, &pty.Winsize{Rows: 25, Cols: 80}, &syscall.SysProcAttr{})
require.NoError(t, err)
defer ptmx.Close()
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(ptmx) // ignoring errors explicitly
expected := "The content of ENV is <insert-env-here>"
current := buf.String()
current = removeAnsiCodes(current)
current = strings.TrimSpace(current)
require.Equal(t, expected, current, "output does not match")
}
func removeAnsiCodes(str string) string {
re := regexp.MustCompile(`\x1b\[.*?[a-zA-Z]|\x1b\].*?\x1b\\`)
return re.ReplaceAllString(str, "")
}