2019-02-01 15:21:54 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2019-02-01 18:25:04 +00:00
|
|
|
"fmt"
|
2019-02-01 15:21:54 +00:00
|
|
|
"io"
|
2020-01-28 23:50:41 +00:00
|
|
|
"io/ioutil"
|
2019-02-01 15:21:54 +00:00
|
|
|
"log"
|
2019-02-01 18:25:04 +00:00
|
|
|
"os"
|
2019-02-01 15:21:54 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-02-01 15:21:54 +00:00
|
|
|
)
|
|
|
|
|
2019-02-01 18:25:04 +00:00
|
|
|
var sendTestLogsToStdout bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sendTestLogsToStdout = os.Getenv("NOLOGBUFFER") == "1"
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
// Deprecated: use Logger(t)
|
2019-02-01 15:21:54 +00:00
|
|
|
func TestLogger(t testing.TB) *log.Logger {
|
2019-07-12 21:19:37 +00:00
|
|
|
return log.New(&testWriter{t}, t.Name()+": ", log.LstdFlags)
|
2019-02-01 15:21:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
func NewDiscardLogger() hclog.Logger {
|
|
|
|
return hclog.New(&hclog.LoggerOptions{
|
|
|
|
Level: 0,
|
|
|
|
Output: ioutil.Discard,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Logger(t testing.TB) hclog.InterceptLogger {
|
|
|
|
return LoggerWithOutput(t, &testWriter{t})
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoggerWithOutput(t testing.TB, output io.Writer) hclog.InterceptLogger {
|
|
|
|
return hclog.NewInterceptLogger(&hclog.LoggerOptions{
|
|
|
|
Name: t.Name(),
|
2020-02-03 14:26:47 +00:00
|
|
|
Level: hclog.Trace,
|
2020-01-28 23:50:41 +00:00
|
|
|
Output: output,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated: use LoggerWithName(t)
|
2019-02-01 15:21:54 +00:00
|
|
|
func TestLoggerWithName(t testing.TB, name string) *log.Logger {
|
|
|
|
return log.New(&testWriter{t}, "test["+name+"]: ", log.LstdFlags)
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
func LoggerWithName(t testing.TB, name string) hclog.InterceptLogger {
|
|
|
|
return hclog.NewInterceptLogger(&hclog.LoggerOptions{
|
|
|
|
Name: "test[" + name + "]",
|
|
|
|
Level: hclog.Debug,
|
|
|
|
Output: &testWriter{t},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-01 15:21:54 +00:00
|
|
|
func TestWriter(t testing.TB) io.Writer {
|
|
|
|
return &testWriter{t}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testWriter struct {
|
|
|
|
t testing.TB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tw *testWriter) Write(p []byte) (n int, err error) {
|
2019-09-20 21:01:08 +00:00
|
|
|
if tw.t != nil {
|
|
|
|
tw.t.Helper()
|
|
|
|
}
|
|
|
|
if sendTestLogsToStdout || tw.t == nil {
|
2019-02-01 18:25:04 +00:00
|
|
|
fmt.Fprint(os.Stdout, strings.TrimSpace(string(p))+"\n")
|
|
|
|
} else {
|
2019-10-17 16:01:11 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if sr, ok := r.(string); ok {
|
|
|
|
if strings.HasPrefix(sr, "Log in goroutine after ") {
|
|
|
|
// These sorts of panics are undesirable, but requires
|
|
|
|
// total control over goroutine lifetimes to correct.
|
|
|
|
fmt.Fprint(os.Stdout, "SUPPRESSED PANIC: "+sr+"\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}()
|
2019-02-01 18:25:04 +00:00
|
|
|
tw.t.Log(strings.TrimSpace(string(p)))
|
|
|
|
}
|
2019-02-01 15:21:54 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|