"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/testutil/testutil.go" (19 Nov 2020, 1038 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Package testutil contains utilities for testing.
    2 package testutil
    3 
    4 import (
    5     "io"
    6     "io/ioutil"
    7     "os"
    8     "testing"
    9 
   10     "github.com/AdguardTeam/golibs/log"
   11 )
   12 
   13 // DiscardLogOutput runs tests with discarded logger output.
   14 func DiscardLogOutput(m *testing.M) {
   15     // TODO(e.burkov): Refactor code and tests to not use the global mutable
   16     // logger.
   17     log.SetOutput(ioutil.Discard)
   18 
   19     os.Exit(m.Run())
   20 }
   21 
   22 // ReplaceLogWriter moves logger output to w and uses Cleanup method of t to
   23 // revert changes.
   24 func ReplaceLogWriter(t *testing.T, w io.Writer) {
   25     stdWriter := log.Writer()
   26     t.Cleanup(func() {
   27         log.SetOutput(stdWriter)
   28     })
   29     log.SetOutput(w)
   30 }
   31 
   32 // ReplaceLogLevel sets logging level to l and uses Cleanup method of t to
   33 // revert changes.
   34 func ReplaceLogLevel(t *testing.T, l int) {
   35     switch l {
   36     case log.INFO, log.DEBUG, log.ERROR:
   37         // Go on.
   38     default:
   39         t.Fatalf("wrong l value (must be one of %v, %v, %v)", log.INFO, log.DEBUG, log.ERROR)
   40     }
   41 
   42     stdLevel := log.GetLevel()
   43     t.Cleanup(func() {
   44         log.SetLevel(stdLevel)
   45     })
   46     log.SetLevel(l)
   47 }