AdGuardHome/internal/testutil/testutil.go

48 lines
1.0 KiB
Go
Raw Normal View History

Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
// Package testutil contains utilities for testing.
package testutil
import (
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
"io"
Pull request: 2273 clean tests output Merge in DNS/adguard-home from 2273-clean-tests-output to master Closes #2273. Squashed commit of the following: commit 7571a33fc1f76300bd256578b3afa95338e399c4 Merge: f17df0f9c a19523b25 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:45:30 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit f17df0f9ce2a3ed25db33fbc6a2e7cabd33f657b Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:35:42 2020 +0300 home: move build constraint on top line commit 3717c8ef5a51f9dcaa7e524bfa7b0f1d90bec93d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:24:50 2020 +0300 all: add improvements to changelog commit de6d5afe39d74a3c3d3e0bbe6d0e09aea0214d56 Merge: 43d4f7acf 394fc5a9d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:04:38 2020 +0300 Merge branch 'master' into 2273-clean-tests-output commit 43d4f7acf188e810aa7277cb6479110c9842e8be Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 13:38:13 2020 +0300 dnsfilter: remove redundant test logging commit 7194c1413006b8f52fc454e89ab052bf52e4e517 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:19:14 2020 +0300 testutil: improve comments commit 9f17ab08e287fa69ce30d9e7eec8ea8880f87716 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:22:08 2020 +0300 nclient4: fix wrong function name commit f355749149b2a4485792ba2bdcbc0bb4b629d726 Author: Eugene Burkov <e.burkov@adguard.com> Date: Sat Nov 14 01:07:22 2020 +0300 testutil: fix comments and naming commit f8c50a260bfae08d594a7f37d603941d3680a45e Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 14:09:50 2020 +0300 testutil: create a package and include it commit f169cdc4084b719de65aa0cdc65200b48785322e Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:15:58 2020 +0300 agherr: discard loggers output commit 360e736b5a2a30f2c5350448234f14b841e3ea27 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 20:09:55 2020 +0300 all: replace default log writer with ioutil.Discard Closes #2273.
2020-11-16 12:52:05 +00:00
"io/ioutil"
"os"
"testing"
"github.com/AdguardTeam/golibs/log"
)
// DiscardLogOutput runs tests with discarded logger output.
func DiscardLogOutput(m *testing.M) {
// TODO(e.burkov): Refactor code and tests to not use the global mutable
// logger.
log.SetOutput(ioutil.Discard)
os.Exit(m.Run())
}
Pull request: cover with tests Merge in DNS/adguard-home from 2271-cover-with-tests to master Updates #2271. Squashed commit of the following: commit db6440efe05171bc15367a2996521848ca348053 Merge: db7fa726b bf4c256c7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:23:09 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit db7fa726bb91b08ec7aaa6c0c818c88b5feb87cd Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:26:51 2020 +0300 all: clean dependencies sum commit b8dc6078c4bcc0de1b7e9073832de122f6fe38a4 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:46:00 2020 +0300 testutil: improve code quality commit 001b7194682b1f00aa54dc5a28236faed5a5b02d Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 16:29:49 2020 +0300 testutil: enhance functionality commit f6ccd91a4df6c56778eab8ae50e88e3818b20dd3 Merge: 43fa2eefb 6358240e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 15:57:37 2020 +0300 Merge branch 'master' into 2271-cover-with-tests commit 43fa2eefbc10ef361603cacc1ca12092b12a057a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 14:55:15 2020 +0300 querylog: replace fake log with real in tests commit b95bee7565a14a02c80c78131b3ced224663dd8a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:38:59 2020 +0300 dnsfilter: replace thoughtless declaration with idiomatic one commit a210b1586092e7ae91a9e67c972fa2d2f6baded6 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 19:00:25 2020 +0300 all: refresh golibs dependencies commit 4ff97bd1ade6c80e274ff5716e44df4eba55bdd9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 18:38:47 2020 +0300 all: remove std log commit 542dbda10fefce9f46d15489712b163d919b1291 Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 13 13:46:39 2020 +0300 querylog: improve test logic and readability commit 796d402385925e8e62a1b4c7bf56e4ceec22418c Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 19:06:42 2020 +0300 all: improve code quality commit e81894c11ef15b0453e8e5297f1349936a32f9dd Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 18:32:30 2020 +0300 all: cover with tests commit 252d81fc8a50a91b02cf0f6f35cc22178a2a4d90 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Nov 12 17:32:01 2020 +0300 all: cover with tests
2020-11-16 16:45:31 +00:00
// ReplaceLogWriter moves logger output to w and uses Cleanup method of t to
// revert changes.
func ReplaceLogWriter(t *testing.T, w io.Writer) {
stdWriter := log.Writer()
t.Cleanup(func() {
log.SetOutput(stdWriter)
})
log.SetOutput(w)
}
// ReplaceLogLevel sets logging level to l and uses Cleanup method of t to
// revert changes.
func ReplaceLogLevel(t *testing.T, l int) {
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)
}
stdLevel := log.GetLevel()
t.Cleanup(func() {
log.SetLevel(stdLevel)
})
log.SetLevel(l)
}