go.dedis.ch/onet/v4@v4.0.0-pre1/log/lvl_test.go (about)

     1  package log
     2  
     3  import (
     4  	"flag"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  	"golang.org/x/xerrors"
    13  )
    14  
    15  func init() {
    16  	outputLines = false
    17  	SetUseColors(false)
    18  	clearEnv()
    19  }
    20  
    21  func TestTime(t *testing.T) {
    22  	SetShowTime(false)
    23  	SetDebugVisible(1)
    24  	GetStdOut()
    25  	Lvl1("No time")
    26  	assert.True(t, containsStdOut("1 : "))
    27  	SetShowTime(true)
    28  	defer func() { SetShowTime(false) }()
    29  	Lvl1("With time")
    30  	str := GetStdOut()
    31  	if strings.Contains(str, "1 : ") {
    32  		t.Fatal("Didn't get correct string: ", str)
    33  	}
    34  	if strings.Contains(str, " +") {
    35  		t.Fatal("Didn't get correct string: ", str)
    36  	}
    37  	if !strings.Contains(str, "With time") {
    38  		t.Fatal("Didn't get correct string: ", str)
    39  	}
    40  }
    41  
    42  func TestFlags(t *testing.T) {
    43  	lvl := DebugVisible()
    44  	time := ShowTime()
    45  	color := UseColors()
    46  	padding := Padding()
    47  	SetDebugVisible(1)
    48  
    49  	clearEnv()
    50  	ParseEnv()
    51  	if DebugVisible() != 1 {
    52  		t.Fatal("Debugvisible should be 1")
    53  	}
    54  	if ShowTime() {
    55  		t.Fatal("ShowTime should be false")
    56  	}
    57  	if AbsoluteFilePath() {
    58  		t.Fatal("AbsoluteFilePath should be false")
    59  	}
    60  	if UseColors() {
    61  		t.Fatal("UseColors should be false")
    62  	}
    63  	if !Padding() {
    64  		t.Fatal("Padding should be true")
    65  	}
    66  
    67  	os.Setenv("DEBUG_LVL", "3")
    68  	os.Setenv("DEBUG_TIME", "true")
    69  	os.Setenv("DEBUG_FILEPATH", "true")
    70  	os.Setenv("DEBUG_COLOR", "false")
    71  	os.Setenv("DEBUG_PADDING", "false")
    72  	ParseEnv()
    73  	if DebugVisible() != 3 {
    74  		t.Fatal("DebugVisible should be 3")
    75  	}
    76  	if !ShowTime() {
    77  		t.Fatal("ShowTime should be true")
    78  	}
    79  	if !AbsoluteFilePath() {
    80  		t.Fatal("AbsoluteFilePath sgould be true")
    81  	}
    82  	if UseColors() {
    83  		t.Fatal("UseColors should be false")
    84  	}
    85  	if Padding() {
    86  		t.Fatal("Padding should be false")
    87  	}
    88  
    89  	clearEnv()
    90  	SetDebugVisible(lvl)
    91  	SetShowTime(time)
    92  	SetUseColors(color)
    93  	SetPadding(padding)
    94  }
    95  
    96  func TestOutputFuncs(t *testing.T) {
    97  	ErrFatal(checkOutput(func() {
    98  		Lvl1("Testing stdout")
    99  	}, true, false))
   100  	ErrFatal(checkOutput(func() {
   101  		LLvl1("Testing stdout")
   102  	}, true, false))
   103  	ErrFatal(checkOutput(func() {
   104  		Print("Testing stdout")
   105  	}, true, false))
   106  	ErrFatal(checkOutput(func() {
   107  		Warn("Testing stdout")
   108  	}, false, true))
   109  	ErrFatal(checkOutput(func() {
   110  		Error("Testing errout")
   111  	}, false, true))
   112  }
   113  
   114  func TestMainTestWait(t *testing.T) {
   115  	toOld := flag.Lookup("test.timeout").Value.String()
   116  	lvlOld := DebugVisible()
   117  	defer func() {
   118  		setFlag(toOld)
   119  		SetDebugVisible(lvlOld)
   120  	}()
   121  	SetDebugVisible(1)
   122  	setFlag("0s")
   123  	require.Equal(t, time.Duration(10*time.Minute), interpretWait())
   124  	setFlag("10s")
   125  	require.Equal(t, time.Duration(10*time.Second), interpretWait())
   126  	require.Equal(t, "", GetStdOut())
   127  
   128  	MainTestWait = 20 * time.Second
   129  	setFlag("0s")
   130  	require.Equal(t, time.Duration(20*time.Second), interpretWait())
   131  	require.NotEqual(t, "", GetStdErr())
   132  	setFlag("10s")
   133  	require.Equal(t, time.Duration(10*time.Second), interpretWait())
   134  	require.NotEqual(t, "", GetStdErr())
   135  }
   136  
   137  func setFlag(t string) {
   138  	timeoutFlagMutex.Lock()
   139  	flag.Lookup("test.timeout").Value.Set(t)
   140  	timeoutFlagMutex.Unlock()
   141  }
   142  
   143  func checkOutput(f func(), wantsStd, wantsErr bool) error {
   144  	f()
   145  	stdStr := GetStdOut()
   146  	errStr := GetStdErr()
   147  	if wantsStd {
   148  		if len(stdStr) == 0 {
   149  			return xerrors.New("Stdout was empty")
   150  		}
   151  	} else {
   152  		if len(stdStr) > 0 {
   153  			return xerrors.New("Stdout was full")
   154  		}
   155  	}
   156  	if wantsErr {
   157  		if len(errStr) == 0 {
   158  			return xerrors.New("Stderr was empty")
   159  		}
   160  	} else {
   161  		if len(errStr) > 0 {
   162  			return xerrors.New("Stderr was full")
   163  		}
   164  	}
   165  	return nil
   166  }
   167  
   168  func ExampleLvl2() {
   169  	SetDebugVisible(2)
   170  	OutputToOs()
   171  	Lvl1("Level1")
   172  	Lvl2("Level2")
   173  	Lvl3("Level3")
   174  	Lvl4("Level4")
   175  	Lvl5("Level5")
   176  	OutputToBuf()
   177  	SetDebugVisible(1)
   178  
   179  	// Output:
   180  	// 1 : fake_name.go:0 (log.ExampleLvl2)         - Level1
   181  	// 2 : fake_name.go:0 (log.ExampleLvl2)         - Level2
   182  }
   183  
   184  func ExampleLvl1() {
   185  	OutputToOs()
   186  	Lvl1("Multiple", "parameters")
   187  	OutputToBuf()
   188  
   189  	// Output:
   190  	// 1 : fake_name.go:0 (log.ExampleLvl1)         - Multiple parameters
   191  }
   192  
   193  func ExampleLLvl1() {
   194  	OutputToOs()
   195  	Lvl1("Lvl output")
   196  	LLvl1("LLvl output")
   197  	Lvlf1("Lvlf output")
   198  	LLvlf1("LLvlf output")
   199  	OutputToBuf()
   200  
   201  	// Output:
   202  	// 1 : fake_name.go:0 (log.ExampleLLvl1)        - Lvl output
   203  	// 1!: fake_name.go:0 (log.ExampleLLvl1)        - LLvl output
   204  	// 1 : fake_name.go:0 (log.ExampleLLvl1)        - Lvlf output
   205  	// 1!: fake_name.go:0 (log.ExampleLLvl1)        - LLvlf output
   206  }
   207  
   208  func thisIsAVeryLongFunctionNameThatWillOverflow() {
   209  	OutputToOs()
   210  	Lvl1("Overflow")
   211  }
   212  
   213  func ExampleLvlf1() {
   214  	OutputToOs()
   215  	Lvl1("Before")
   216  	thisIsAVeryLongFunctionNameThatWillOverflow()
   217  	Lvl1("After")
   218  	OutputToBuf()
   219  
   220  	// Output:
   221  	// 1 : fake_name.go:0 (log.ExampleLvlf1)        - Before
   222  	// 1 : fake_name.go:0 (log.thisIsAVeryLongFunctionNameThatWillOverflow) - Overflow
   223  	// 1 : fake_name.go:0 (log.ExampleLvlf1)        - After
   224  }
   225  
   226  func ExampleLvl3() {
   227  	NamePadding = -1
   228  	OutputToOs()
   229  	Lvl1("Before")
   230  	thisIsAVeryLongFunctionNameThatWillOverflow()
   231  	Lvl1("After")
   232  	OutputToBuf()
   233  
   234  	// Output:
   235  	// 1 : fake_name.go:0 (log.ExampleLvl3) - Before
   236  	// 1 : fake_name.go:0 (log.thisIsAVeryLongFunctionNameThatWillOverflow) - Overflow
   237  	// 1 : fake_name.go:0 (log.ExampleLvl3) - After
   238  }
   239  
   240  func clearEnv() {
   241  	os.Setenv("DEBUG_LVL", "")
   242  	os.Setenv("DEBUG_TIME", "")
   243  	os.Setenv("DEBUG_COLOR", "")
   244  	os.Setenv("DEBUG_PADDING", "")
   245  }