2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 22:27:35 +00:00

redirect test log output to t.Log()

This commit is contained in:
Michael Eischer 2024-10-18 21:18:22 +02:00
parent 1f5791222a
commit 841f8bfef0

View File

@ -9,6 +9,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"sync" "sync"
"testing" "testing"
@ -168,6 +169,16 @@ type testEnvironment struct {
gopts GlobalOptions gopts GlobalOptions
} }
type logOutputter struct {
t testing.TB
}
func (l *logOutputter) Write(p []byte) (n int, err error) {
l.t.Helper()
l.t.Log(strings.TrimSuffix(string(p), "\n"))
return len(p), nil
}
// withTestEnvironment creates a test environment and returns a cleanup // withTestEnvironment creates a test environment and returns a cleanup
// function which removes it. // function which removes it.
func withTestEnvironment(t testing.TB) (env *testEnvironment, cleanup func()) { func withTestEnvironment(t testing.TB) (env *testEnvironment, cleanup func()) {
@ -200,8 +211,11 @@ func withTestEnvironment(t testing.TB) (env *testEnvironment, cleanup func()) {
Quiet: true, Quiet: true,
CacheDir: env.cache, CacheDir: env.cache,
password: rtest.TestPassword, password: rtest.TestPassword,
stdout: os.Stdout, // stdout and stderr are written to by Warnf etc. That is the written data
stderr: os.Stderr, // usually consists of one or multiple lines and therefore can be handled well
// by t.Log.
stdout: &logOutputter{t},
stderr: &logOutputter{t},
extended: make(options.Options), extended: make(options.Options),
// replace this hook with "nil" if listing a filetype more than once is necessary // replace this hook with "nil" if listing a filetype more than once is necessary