-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix no logs in stdout/stderr if uses stdoutConfig #6162
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
@@ -39,10 +41,48 @@ func TestRealRunnerStdoutAndStderrPaths(t *testing.T) { | |
stdoutPath: filepath.Join(tmp, "stdout"), | ||
stderrPath: filepath.Join(tmp, "subpath/stderr"), | ||
} | ||
|
||
// capture the std{out/err} output to verify whether we print log in the std | ||
oldStdout := os.Stdout // keep backup of the real stdout | ||
outReader, outWriter, _ := os.Pipe() | ||
os.Stdout = outWriter | ||
|
||
oldStderr := os.Stderr | ||
errReader, errWriter, _ := os.Pipe() | ||
os.Stderr = errWriter | ||
Comment on lines
+46
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems brittle since it modifies global state. How about we:
This way in tests we can just use a buffer and remove a bunch of the path manipulation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test here is to verify that logs can be printed to standard output. If you modify realRunner to accept io.Writers, you need to pass in os.stdout, which is the same as the current situation. |
||
|
||
if err := rr.Run(context.Background(), "sh", "-c", fmt.Sprintf("echo %s && echo %s >&2", expectedString, expectedString)); err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
|
||
outC := make(chan string) | ||
errC := make(chan string) | ||
// copy the output in a separate goroutine so realRunner command can't block indefinitely | ||
go func() { | ||
var stdOutBuf bytes.Buffer | ||
io.Copy(&stdOutBuf, outReader) | ||
outC <- stdOutBuf.String() | ||
|
||
var stdErrBuf bytes.Buffer | ||
io.Copy(&stdErrBuf, errReader) | ||
errC <- stdErrBuf.String() | ||
}() | ||
// back to normal state | ||
outWriter.Close() | ||
errWriter.Close() | ||
os.Stdout = oldStdout // restoring the real stdout | ||
os.Stderr = oldStderr // restoring the real stderr | ||
stdOut := <-outC | ||
stdErr := <-errC | ||
|
||
// echo command will auto add \n in end, so we should remove trailing newline | ||
if strings.TrimSuffix(stdOut, "\n") != expectedString { | ||
t.Fatalf("Unexpected stdout output: %s, wanted stdout output: %s", stdOut, expectedString) | ||
} | ||
if strings.TrimSuffix(stdErr, "\n") != expectedString { | ||
t.Fatalf("Unexpected stderr output: %s, wanted stderr output: %s", stdErr, expectedString) | ||
} | ||
|
||
for _, path := range []string{"stdout", "subpath/stderr"} { | ||
if got, err := os.ReadFile(filepath.Join(tmp, path)); err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this docstring missing after "note that close after use"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not missing, I just modified the note here and added it to where it was used.
notes related to teaReader are no longer needed here