From d1c58bdbbe25c51adf4e3efbe40e5118557560ae Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 3 Jan 2024 00:27:52 +0100 Subject: [PATCH] integration-cli: remove/rename err-returns and remove naked returns Prevent accidentally shadowing these errors, which are used in defers, and remove naked returns. Signed-off-by: Sebastiaan van Stijn --- integration-cli/docker_cli_logs_test.go | 11 +++++------ integration-cli/utils_test.go | 14 ++++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/integration-cli/docker_cli_logs_test.go b/integration-cli/docker_cli_logs_test.go index c9e6eedd11..4909d9951e 100644 --- a/integration-cli/docker_cli_logs_test.go +++ b/integration-cli/docker_cli_logs_test.go @@ -263,17 +263,16 @@ func (s *DockerCLILogsSuite) TestLogsFollowSlowStdoutConsumer(c *testing.T) { // ConsumeWithSpeed reads chunkSize bytes from reader before sleeping // for interval duration. Returns total read bytes. Send true to the // stop channel to return before reading to EOF on the reader. -func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) { +func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, _ error) { buffer := make([]byte, chunkSize) for { - var readBytes int - readBytes, err = reader.Read(buffer) + readBytes, err := reader.Read(buffer) n += readBytes if err != nil { - if err == io.EOF { - err = nil + if err != io.EOF { + return n, err } - return n, err + return n, nil } select { case <-stop: diff --git a/integration-cli/utils_test.go b/integration-cli/utils_test.go index ac28e68817..1d86e0355b 100644 --- a/integration-cli/utils_test.go +++ b/integration-cli/utils_test.go @@ -67,8 +67,9 @@ func RandomTmpDirPath(s string, platform string) string { // of each pipelined with the following (like cmd1 | cmd2 | cmd3 would do). // It returns the final output, the exitCode different from 0 and the error // if something bad happened. +// // Deprecated: use icmd instead -func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error) { +func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, retErr error) { if len(cmds) < 2 { return "", errors.New("pipeline does not have multiple cmds") } @@ -77,6 +78,7 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error) for i, cmd := range cmds { if i > 0 { prevCmd := cmds[i-1] + var err error cmd.Stdin, err = prevCmd.StdoutPipe() if err != nil { return "", fmt.Errorf("cannot set stdout pipe for %s: %v", cmd.Path, err) @@ -86,7 +88,7 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error) // start all cmds except the last for _, cmd := range cmds[:len(cmds)-1] { - if err = cmd.Start(); err != nil { + if err := cmd.Start(); err != nil { return "", fmt.Errorf("starting %s failed with error: %v", cmd.Path, err) } } @@ -95,12 +97,12 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error) var pipeErrMsgs []string // wait all cmds except the last to release their resources for _, cmd := range cmds[:len(cmds)-1] { - if pipeErr := cmd.Wait(); pipeErr != nil { - pipeErrMsgs = append(pipeErrMsgs, fmt.Sprintf("command %s failed with error: %v", cmd.Path, pipeErr)) + if err := cmd.Wait(); err != nil { + pipeErrMsgs = append(pipeErrMsgs, fmt.Sprintf("command %s failed with error: %v", cmd.Path, err)) } } - if len(pipeErrMsgs) > 0 && err == nil { - err = fmt.Errorf("pipelineError from Wait: %v", strings.Join(pipeErrMsgs, ", ")) + if len(pipeErrMsgs) > 0 && retErr == nil { + retErr = fmt.Errorf("pipelineError from Wait: %v", strings.Join(pipeErrMsgs, ", ")) } }()