AdGuardHome/internal/aghtest/aghtest.go
Ainar Garipov 9c9d6b48e3 Pull request: imp-tests
Merge in DNS/adguard-home from imp-tests to master

Squashed commit of the following:

commit ea5b4e7a93359ae4800f75e77d02944a4f5d2df8
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Oct 27 15:12:45 2022 +0300

    all: imp tests, use testutil
2022-10-27 16:11:36 +03:00

36 lines
787 B
Go

// Package aghtest contains utilities for testing.
package aghtest
import (
"io"
"testing"
"github.com/AdguardTeam/golibs/log"
)
// ReplaceLogWriter moves logger output to w and uses Cleanup method of t to
// revert changes.
func ReplaceLogWriter(t testing.TB, w io.Writer) {
t.Helper()
prev := log.Writer()
t.Cleanup(func() { log.SetOutput(prev) })
log.SetOutput(w)
}
// ReplaceLogLevel sets logging level to l and uses Cleanup method of t to
// revert changes.
func ReplaceLogLevel(t testing.TB, l log.Level) {
t.Helper()
switch l {
case log.INFO, log.DEBUG, log.ERROR:
// Go on.
default:
t.Fatalf("wrong l value (must be one of %v, %v, %v)", log.INFO, log.DEBUG, log.ERROR)
}
prev := log.GetLevel()
t.Cleanup(func() { log.SetLevel(prev) })
log.SetLevel(l)
}