github.com/jfrog/jfrog-cli-core/v2@v2.51.0/utils/tests/test_cli.go (about) 1 package tests 2 3 import ( 4 "io" 5 "os" 6 "strings" 7 "testing" 8 9 corelog "github.com/jfrog/jfrog-cli-core/v2/utils/log" 10 "github.com/jfrog/jfrog-client-go/utils/log" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 type JfrogCli struct { 15 main func() error 16 prefix string 17 credentials string 18 } 19 20 func NewJfrogCli(mainFunc func() error, prefix, credentials string) *JfrogCli { 21 return &JfrogCli{mainFunc, prefix, credentials} 22 } 23 24 func (cli *JfrogCli) SetPrefix(prefix string) *JfrogCli { 25 cli.prefix = prefix 26 return cli 27 } 28 29 func (cli *JfrogCli) Exec(args ...string) error { 30 spaceSplit := " " 31 os.Args = strings.Split(cli.prefix, spaceSplit) 32 output := strings.Split(cli.prefix, spaceSplit) 33 for _, v := range args { 34 if v == "" { 35 continue 36 } 37 args := strings.Split(v, spaceSplit) 38 os.Args = append(os.Args, v) 39 output = append(output, args...) 40 } 41 if cli.credentials != "" { 42 args := strings.Split(cli.credentials, spaceSplit) 43 os.Args = append(os.Args, args...) 44 } 45 46 log.Info("[Command]", strings.Join(output, " ")) 47 return cli.main() 48 } 49 50 // Run `jfrog` command, redirect the stdout and return the output 51 func (cli *JfrogCli) RunCliCmdWithOutput(t *testing.T, args ...string) string { 52 return RunCmdWithOutput(t, func() error { return cli.Exec(args...) }) 53 } 54 55 // Run a command, redirect the stdout and return the output 56 func RunCmdWithOutput(t *testing.T, executeCmd func() error) string { 57 newStdout, stdWriter, cleanUp := redirectOutToPipe(t) 58 defer cleanUp() 59 60 go func() { 61 assert.NoError(t, executeCmd()) 62 // Closing the temp stdout in order to be able to read it's content. 63 assert.NoError(t, stdWriter.Close()) 64 }() 65 66 content, err := io.ReadAll(newStdout) 67 assert.NoError(t, err) 68 log.Debug(string(content)) 69 return string(content) 70 } 71 72 func redirectOutToPipe(t *testing.T) (*os.File, *os.File, func()) { 73 newStdout, stdWriter, previousStdout := RedirectStdOutToPipe() 74 previousLog := log.Logger 75 log.SetLogger(log.NewLogger(corelog.GetCliLogLevel(), nil)) 76 // Restore previous stdout when the function returns 77 return newStdout, stdWriter, func() { 78 os.Stdout = previousStdout 79 log.SetLogger(previousLog) 80 assert.NoError(t, newStdout.Close()) 81 } 82 } 83 84 func (cli *JfrogCli) LegacyBuildToolExec(args ...string) error { 85 spaceSplit := " " 86 os.Args = strings.Split(cli.prefix, spaceSplit) 87 os.Args = append(os.Args, args...) 88 89 log.Info("[Command]", os.Args) 90 91 if cli.credentials != "" { 92 args := strings.Split(cli.credentials, spaceSplit) 93 os.Args = append(os.Args, args...) 94 } 95 return cli.main() 96 } 97 98 func (cli *JfrogCli) WithoutCredentials() *JfrogCli { 99 return &JfrogCli{cli.main, cli.prefix, ""} 100 } 101 102 // Redirect stdout to new temp, os.pipe 103 // Caller is responsible to close the pipe and to set the old stdout back. 104 func RedirectStdOutToPipe() (reader *os.File, writer *os.File, previousStdout *os.File) { 105 previousStdout = os.Stdout 106 reader, writer, _ = os.Pipe() 107 os.Stdout = writer 108 return 109 }