github.com/teknogeek/dnscontrol@v0.2.8/pkg/printer/printer_test.go (about)

     1  package printer
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  // TestDefaultPrinter checks that the DefaultPrinter properly controls output from the package-level
    11  // Warnf/Printf/Debugf functions.
    12  func TestDefaultPrinter(t *testing.T) {
    13  	old := DefaultPrinter
    14  	defer func() {
    15  		DefaultPrinter = old
    16  	}()
    17  
    18  	output := &bytes.Buffer{}
    19  	DefaultPrinter = &ConsolePrinter{
    20  		Writer:  output,
    21  		Verbose: true,
    22  	}
    23  
    24  	Warnf("warn\n")
    25  	Printf("output\n")
    26  	Debugf("debugging\n")
    27  	assert.Equal(t, "WARNING: warn\noutput\ndebugging\n", output.String())
    28  }
    29  
    30  func TestVerbose(t *testing.T) {
    31  	output := &bytes.Buffer{}
    32  	p := ConsolePrinter{
    33  		Writer:  output,
    34  		Verbose: false,
    35  	}
    36  
    37  	// Test that verbose output is suppressed.
    38  	p.Warnf("a dire warning!\n")
    39  	p.Printf("output\n")
    40  	p.Debugf("debugging\n")
    41  	assert.Equal(t, "WARNING: a dire warning!\noutput\n", output.String())
    42  
    43  	// Test that Verbose output can be dynamically enabled.
    44  	p.Verbose = true
    45  	p.Debugf("more debugging\n")
    46  	assert.Equal(t, "WARNING: a dire warning!\noutput\nmore debugging\n", output.String())
    47  }