Skip to content

Commit

Permalink
Test Cli outputs of cmd with errors (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas authored Jan 1, 2025
1 parent 203c916 commit b26e9a6
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions utils/tests/test_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,37 @@ func (cli *JfrogCli) RunCliCmdWithOutput(t *testing.T, args ...string) string {
return RunCmdWithOutput(t, func() error { return cli.Exec(args...) })
}

// Run a command, redirect the stdout and return the output
func RunCmdWithOutput(t *testing.T, executeCmd func() error) string {
func (cli *JfrogCli) RunCliCmdWithOutputs(t *testing.T, args ...string) (string, error) {
return RunCmdWithOutputs(t, func() error { return cli.Exec(args...) })
}

func RunCmdWithOutputs(t *testing.T, executeCmd func() error) (output string, err error) {
newStdout, stdWriter, cleanUp := redirectOutToPipe(t)
defer cleanUp()

errCh := make(chan error, 1)

go func() {
assert.NoError(t, executeCmd())
errCh <- executeCmd()
// Closing the temp stdout in order to be able to read it's content.
assert.NoError(t, stdWriter.Close())
}()

content, err := io.ReadAll(newStdout)
content, e := io.ReadAll(newStdout)
assert.NoError(t, e)
output = string(content)
log.Debug(output)

err = <-errCh

return
}

// Run a command, redirect the stdout and return the output
func RunCmdWithOutput(t *testing.T, executeCmd func() error) string {
output, err := RunCmdWithOutputs(t, executeCmd)
assert.NoError(t, err)
log.Debug(string(content))
return string(content)
return output
}

func redirectOutToPipe(t *testing.T) (*os.File, *os.File, func()) {
Expand Down

0 comments on commit b26e9a6

Please sign in to comment.