63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package toolkit
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
// MarkIntegrationTest marks a test as being an integration test. It will be
|
|
// skipped if the ISLE_INTEGRATION_TEST envvar isn't set.
|
|
func MarkIntegrationTest(t *testing.T) {
|
|
if os.Getenv("ISLE_INTEGRATION_TEST") == "" {
|
|
t.Skip("Skipped because ISLE_INTEGRATION_TEST isn't set")
|
|
}
|
|
}
|
|
|
|
type mlogMessageHandlerWithOffset struct {
|
|
mlog.MessageHandler
|
|
start time.Time
|
|
}
|
|
|
|
func (h *mlogMessageHandlerWithOffset) Handle(msg mlog.FullMessage) error {
|
|
msg.Description = fmt.Sprintf(
|
|
"(+%v) %s",
|
|
time.Since(h.start).Truncate(time.Millisecond),
|
|
msg.Description,
|
|
)
|
|
return h.MessageHandler.Handle(msg)
|
|
}
|
|
|
|
// NewTestLogger returns a Logger which should be used for testing purposes. The
|
|
// log level of the Logger can be adjusted using the ISLE_LOG_LEVEL envvar.
|
|
func NewTestLogger(t *testing.T) *mlog.Logger {
|
|
level := mlog.LevelInfo
|
|
if levelStr := os.Getenv("ISLE_LOG_LEVEL"); levelStr != "" {
|
|
if level = LogLevelFromString(levelStr); level == nil {
|
|
panic(fmt.Sprintf("invalid log level: %q", levelStr))
|
|
}
|
|
}
|
|
|
|
handler := mlog.NewTestMessageHandler(t)
|
|
handler = &mlogMessageHandlerWithOffset{
|
|
MessageHandler: handler,
|
|
start: time.Now(),
|
|
}
|
|
|
|
return mlog.NewLogger(&mlog.LoggerOpts{
|
|
MessageHandler: handler,
|
|
MaxLevel: level.Int(),
|
|
})
|
|
}
|
|
|
|
// MockArg returns a value which can be used as a [mock.Call] argument, and
|
|
// which will match any value of type T. If T is an interface then also values
|
|
// implementing that interface will be matched.
|
|
func MockArg[T any]() any {
|
|
return mock.MatchedBy(func(T) bool { return true })
|
|
}
|