github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/common/loggers/logger_test.go (about)

     1  // Copyright 2023 The Hugo Authors. All rights reserved.
     2  // Some functions in this file (see comments) is based on the Go source code,
     3  // copyright The Go Authors and  governed by a BSD-style license.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  // http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package loggers_test
    17  
    18  import (
    19  	"io"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/bep/logg"
    24  	qt "github.com/frankban/quicktest"
    25  	"github.com/gohugoio/hugo/common/loggers"
    26  )
    27  
    28  func TestLogDistinct(t *testing.T) {
    29  	c := qt.New(t)
    30  
    31  	opts := loggers.Options{
    32  		Distinct:    true,
    33  		StoreErrors: true,
    34  		Stdout:      io.Discard,
    35  		Stderr:      io.Discard,
    36  	}
    37  
    38  	l := loggers.New(opts)
    39  
    40  	for i := 0; i < 10; i++ {
    41  		l.Errorln("error 1")
    42  		l.Errorln("error 2")
    43  		l.Warnln("warn 1")
    44  	}
    45  	c.Assert(strings.Count(l.Errors(), "error 1"), qt.Equals, 1)
    46  	c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 2)
    47  	c.Assert(l.LoggCount(logg.LevelWarn), qt.Equals, 1)
    48  }
    49  
    50  func TestHookLast(t *testing.T) {
    51  	c := qt.New(t)
    52  
    53  	opts := loggers.Options{
    54  		HandlerPost: func(e *logg.Entry) error {
    55  			panic(e.Message)
    56  		},
    57  		Stdout: io.Discard,
    58  		Stderr: io.Discard,
    59  	}
    60  
    61  	l := loggers.New(opts)
    62  
    63  	c.Assert(func() { l.Warnln("warn 1") }, qt.PanicMatches, "warn 1")
    64  }
    65  
    66  func TestOptionStoreErrors(t *testing.T) {
    67  	c := qt.New(t)
    68  
    69  	var sb strings.Builder
    70  
    71  	opts := loggers.Options{
    72  		StoreErrors: true,
    73  		Stderr:      &sb,
    74  		Stdout:      &sb,
    75  	}
    76  
    77  	l := loggers.New(opts)
    78  	l.Errorln("error 1")
    79  	l.Errorln("error 2")
    80  
    81  	errorsStr := l.Errors()
    82  
    83  	c.Assert(errorsStr, qt.Contains, "error 1")
    84  	c.Assert(errorsStr, qt.Not(qt.Contains), "ERROR")
    85  
    86  	c.Assert(sb.String(), qt.Contains, "error 1")
    87  	c.Assert(sb.String(), qt.Contains, "ERROR")
    88  
    89  }
    90  
    91  func TestLogCount(t *testing.T) {
    92  	c := qt.New(t)
    93  
    94  	opts := loggers.Options{
    95  		StoreErrors: true,
    96  	}
    97  
    98  	l := loggers.New(opts)
    99  	l.Errorln("error 1")
   100  	l.Errorln("error 2")
   101  	l.Warnln("warn 1")
   102  
   103  	c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 2)
   104  	c.Assert(l.LoggCount(logg.LevelWarn), qt.Equals, 1)
   105  	c.Assert(l.LoggCount(logg.LevelInfo), qt.Equals, 0)
   106  }
   107  
   108  func TestSuppressStatements(t *testing.T) {
   109  	c := qt.New(t)
   110  
   111  	opts := loggers.Options{
   112  		StoreErrors: true,
   113  		SuppressStatements: map[string]bool{
   114  			"error-1": true,
   115  		},
   116  	}
   117  
   118  	l := loggers.New(opts)
   119  	l.Error().WithField(loggers.FieldNameStatementID, "error-1").Logf("error 1")
   120  	l.Errorln("error 2")
   121  
   122  	errorsStr := l.Errors()
   123  
   124  	c.Assert(errorsStr, qt.Not(qt.Contains), "error 1")
   125  	c.Assert(errorsStr, qt.Contains, "error 2")
   126  	c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 1)
   127  
   128  }
   129  
   130  func TestReset(t *testing.T) {
   131  	c := qt.New(t)
   132  
   133  	opts := loggers.Options{
   134  		StoreErrors: true,
   135  		Distinct:    true,
   136  		Stdout:      io.Discard,
   137  		Stderr:      io.Discard,
   138  	}
   139  
   140  	l := loggers.New(opts)
   141  
   142  	for i := 0; i < 3; i++ {
   143  		l.Errorln("error 1")
   144  		l.Errorln("error 2")
   145  		l.Errorln("error 1")
   146  		c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 2)
   147  
   148  		l.Reset()
   149  
   150  		errorsStr := l.Errors()
   151  
   152  		c.Assert(errorsStr, qt.Equals, "")
   153  		c.Assert(l.LoggCount(logg.LevelError), qt.Equals, 0)
   154  
   155  	}
   156  }