mirror of
https://github.com/awalsh128/cache-apt-pkgs-action.git
synced 2025-12-28 14:21:28 +00:00
43 lines
940 B
Go
43 lines
940 B
Go
package testing
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// CaptureStd captures stdout and stderr output during the execution of a function.
|
|
// It temporarily redirects the standard streams, executes the provided function,
|
|
// and returns the captured output as strings. The original streams are restored
|
|
// after execution, even if the function panics.
|
|
//
|
|
// Example:
|
|
//
|
|
// stdout, stderr := CaptureStd(func() {
|
|
// fmt.Println("captured")
|
|
// fmt.Fprintf(os.Stderr, "error")
|
|
// })
|
|
func CaptureStd(fn func()) (stdout, stderr string) {
|
|
oldStdout := os.Stdout
|
|
oldStderr := os.Stderr
|
|
rOut, wOut, _ := os.Pipe()
|
|
rErr, wErr, _ := os.Pipe()
|
|
os.Stdout = wOut
|
|
os.Stderr = wErr
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
bufOut, _ := io.ReadAll(rOut)
|
|
bufErr, _ := io.ReadAll(rErr)
|
|
stdout, stderr = string(bufOut), string(bufErr)
|
|
close(done)
|
|
}()
|
|
|
|
fn()
|
|
wOut.Close()
|
|
wErr.Close()
|
|
os.Stdout = oldStdout
|
|
os.Stderr = oldStderr
|
|
<-done
|
|
return
|
|
}
|