mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-04-02 07:23:35 +03:00
Updates #5035. Squashed commit of the following: commit 3a272842f738da322abb2bc5306aed94da79304b Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Oct 26 20:34:49 2022 +0300 dnsforward: imp docs, tests commit b442ca9b57d730be3af14c68759c706f1742e4c4 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Oct 26 19:51:21 2022 +0300 dnsforward: imp code, tests commit 8fca6de93edb8cfdb0ff5a940d08f8700e12a423 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Oct 26 16:38:27 2022 +0300 dnsforward: mv dhcp hosts to netip.Addr
48 lines
1 KiB
Go
48 lines
1 KiB
Go
// Package aghtest contains utilities for testing.
|
|
package aghtest
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// DiscardLogOutput runs tests with discarded logger output.
|
|
//
|
|
// TODO(a.garipov): Replace with testutil.
|
|
func DiscardLogOutput(m *testing.M) {
|
|
// TODO(e.burkov): Refactor code and tests to not use the global mutable
|
|
// logger.
|
|
log.SetOutput(io.Discard)
|
|
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
// 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)
|
|
}
|