forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
142 lines (129 loc) · 3.33 KB
/
helpers.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
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
package tests
import (
"bytes"
"fmt"
"io"
"os"
"runtime/debug"
"strings"
"testing"
"time"
expect "github.com/Netflix/go-expect"
"github.com/hinshun/vt10x"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// IsDebugLog debug log?
func IsDebugLog() bool {
return strings.ToLower(os.Getenv("JX_TEST_DEBUG")) == "true"
}
// Debugf debug format
func Debugf(message string, args ...interface{}) {
if IsDebugLog() {
log.Infof(message, args...)
}
}
// Output returns the output to use for tests
func Output() terminal.FileWriter {
if IsDebugLog() {
return os.Stdout
}
return terminal.Stdio{}.Out
}
// TestShouldDisableMaven should disable maven
func TestShouldDisableMaven() bool {
cmd := util.Command{
Name: "mvn",
Args: []string{"-v"},
}
_, err := cmd.RunWithoutRetry()
return err != nil
}
// CreateAuthConfigService creates and returns a fixture AuthConfigService
func CreateAuthConfigService() auth.AuthConfigService {
authConfigSvc := auth.AuthConfigService{
FileName: "test-auth-config-service",
}
userAuth := auth.UserAuth{
Username: "jx-testing-user",
ApiToken: "someapitoken",
BearerToken: "somebearertoken",
Password: "password",
}
authServer := auth.AuthServer{
Users: []*auth.UserAuth{&userAuth},
CurrentUser: userAuth.Username,
URL: "https://github.com",
Kind: gits.KindGitHub,
Name: "jx-testing-server",
}
authConfig := auth.AuthConfig{
Servers: []*auth.AuthServer{&authServer},
DefaultUsername: userAuth.Username,
PipeLineUsername: userAuth.Username,
CurrentServer: authServer.URL,
PipeLineServer: authServer.URL,
}
authConfigSvc.SetConfig(&authConfig)
return authConfigSvc
}
//newTerminal Returns a fake terminal to test input and output.
func newTerminal(c *expect.Console) *terminal.Stdio {
return &terminal.Stdio{
In: c.Tty(),
Out: c.Tty(),
Err: c.Tty(),
}
}
// NewTerminal mock terminal to control stdin and stdout
func NewTerminal(t *testing.T) (*expect.Console, *vt10x.State, *terminal.Stdio) {
buf := new(bytes.Buffer)
timeout := time.Second * 1
opts := []expect.ConsoleOpt{
expectNoTimeoutError(t),
sendNoError(t),
expect.WithStdout(buf),
expect.WithDefaultTimeout(timeout),
}
c, state, err := vt10x.NewVT10XConsole(opts...)
if err != nil {
panic(err)
}
term := newTerminal(c)
return c, state, term
}
// TestCloser closes io
func TestCloser(t *testing.T, closer io.Closer) {
if err := closer.Close(); err != nil {
t.Errorf("Close failed: %s", err)
debug.PrintStack()
}
}
func expectNoTimeoutError(t *testing.T) expect.ConsoleOpt {
return expect.WithExpectObserver(
func(matcher expect.Matcher, buf string, err error) {
if err != nil {
if e, ok := err.(*os.PathError); ok {
if e.Timeout() {
panic("Test: " + t.Name() + " Timout waiting for Terminal output: " + fmt.Sprintf("%q", buf))
}
}
}
},
)
}
func sendNoError(t *testing.T) expect.ConsoleOpt {
return expect.WithSendObserver(
func(msg string, n int, err error) {
if err != nil {
t.Fatalf("Failed to send %q: %s\n%s", msg, err, string(debug.Stack()))
}
if len(msg) != n {
t.Fatalf("Only sent %d of %d bytes for %q\n%s", n, len(msg), msg, string(debug.Stack()))
}
},
)
}