github.com/jenkins-x/jx/v2@v2.1.155/pkg/tests/helpers.go (about) 1 package tests 2 3 import ( 4 "bytes" 5 "io" 6 "os" 7 "runtime" 8 "runtime/debug" 9 "strings" 10 "testing" 11 "time" 12 13 "github.com/petergtz/pegomock" 14 15 expect "github.com/Netflix/go-expect" 16 "github.com/acarl005/stripansi" 17 "github.com/hinshun/vt10x" 18 "github.com/jenkins-x/jx-logging/pkg/log" 19 "github.com/jenkins-x/jx/v2/pkg/auth" 20 auth_test "github.com/jenkins-x/jx/v2/pkg/auth/mocks" 21 "github.com/jenkins-x/jx/v2/pkg/gits" 22 "github.com/jenkins-x/jx/v2/pkg/util" 23 "github.com/stretchr/testify/assert" 24 "gopkg.in/AlecAivazis/survey.v1/terminal" 25 ) 26 27 var ( 28 defaultConsoleTimeout = 1 * time.Second 29 ) 30 31 // IsDebugLog debug log? 32 func IsDebugLog() bool { 33 return strings.ToLower(os.Getenv("JX_TEST_DEBUG")) == "true" 34 } 35 36 // Debugf debug format 37 func Debugf(message string, args ...interface{}) { 38 if IsDebugLog() { 39 log.Logger().Infof(message, args...) 40 } 41 } 42 43 // Output returns the output to use for tests 44 func Output() terminal.FileWriter { 45 if IsDebugLog() { 46 return os.Stdout 47 } 48 return terminal.Stdio{}.Out 49 } 50 51 // TestShouldDisableMaven should disable maven 52 func TestShouldDisableMaven() bool { 53 cmd := util.Command{ 54 Name: "mvn", 55 Args: []string{"-v"}, 56 } 57 _, err := cmd.RunWithoutRetry() 58 return err != nil 59 } 60 61 // CreateAuthConfigService creates and returns a fixture ConfigService 62 func CreateAuthConfigService() auth.ConfigService { 63 userAuth := auth.UserAuth{ 64 Username: "jx-testing-user", 65 ApiToken: "someapitoken", 66 BearerToken: "somebearertoken", 67 Password: "password", 68 } 69 authServer := auth.AuthServer{ 70 Users: []*auth.UserAuth{&userAuth}, 71 CurrentUser: userAuth.Username, 72 URL: "https://github.com", 73 Kind: gits.KindGitHub, 74 Name: "jx-testing-server", 75 } 76 authConfig := auth.AuthConfig{ 77 Servers: []*auth.AuthServer{&authServer}, 78 DefaultUsername: userAuth.Username, 79 CurrentServer: authServer.URL, 80 PipeLineUsername: "jx-pipeline-user", 81 PipeLineServer: "https://github.com", 82 } 83 handler := auth_test.NewMockConfigHandler() 84 pegomock.When(handler.LoadConfig()).ThenReturn(&authConfig, nil) 85 authConfigSvc := auth.NewAuthConfigService(handler) 86 authConfigSvc.SetConfig(&authConfig) 87 return authConfigSvc 88 } 89 90 //newTerminal Returns a fake terminal to test input and output. 91 func newTerminal(c *expect.Console) *terminal.Stdio { 92 return &terminal.Stdio{ 93 In: c.Tty(), 94 Out: c.Tty(), 95 Err: c.Tty(), 96 } 97 } 98 99 // NewTerminal mock terminal to control stdin and stdout 100 func NewTerminal(t assert.TestingT, timeout *time.Duration) *ConsoleWrapper { 101 buf := new(bytes.Buffer) 102 if timeout == nil { 103 timeout = &defaultConsoleTimeout 104 } 105 opts := []expect.ConsoleOpt{ 106 sendNoError(t), 107 expect.WithStdout(buf), 108 expect.WithDefaultTimeout(*timeout), 109 } 110 111 c, state, err := vt10x.NewVT10XConsole(opts...) 112 if err != nil { 113 panic(err) 114 } 115 return &ConsoleWrapper{ 116 tester: t, 117 console: c, 118 state: state, 119 Stdio: *newTerminal(c), 120 } 121 } 122 123 // TestCloser closes io 124 func TestCloser(t *testing.T, closer io.Closer) { 125 if err := closer.Close(); err != nil { 126 t.Errorf("Close failed: %s", err) 127 debug.PrintStack() 128 } 129 } 130 131 func sendNoError(t assert.TestingT) expect.ConsoleOpt { 132 return expect.WithSendObserver( 133 func(msg string, n int, err error) { 134 if err != nil { 135 t.Errorf("Failed to send %q: %s\n%s", msg, err, string(debug.Stack())) 136 } 137 if len(msg) != n { 138 t.Errorf("Only sent %d of %d bytes for %q\n%s", n, len(msg), msg, string(debug.Stack())) 139 } 140 }, 141 ) 142 } 143 144 // SkipForWindows skips tests if they are running on Windows 145 // This is to be used for valid tests that just don't work on windows for whatever reason 146 func SkipForWindows(t *testing.T, reason string) { 147 if runtime.GOOS == "windows" { 148 t.Skipf("Test skipped on windows. Reason: %s", reason) 149 } 150 } 151 152 // ExpectString does the same as the go-expect console.ExpectString method, but also reports failures to the testing object in a sensible format 153 func ExpectString(t *testing.T, console *expect.Console, s string) { 154 out, err := console.ExpectString(s) 155 assert.NoError(t, err, "Expected string: %q\nActual string: %q", s, stripansi.Strip(out)) 156 }