github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/logging/logging_test.go (about)

     1  // Unit tests for the logging module
     2  
     3  package logging
     4  
     5  import (
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  type Test1Handler struct {
    14  	formatter Formatter
    15  	file      *os.File
    16  	messages  []string
    17  }
    18  
    19  func (l *Test1Handler) SetFormatter(f Formatter) {
    20  	l.formatter = f
    21  }
    22  
    23  func (l *Test1Handler) SetVerbose(v bool) {
    24  }
    25  
    26  func (l *Test1Handler) Output() io.Writer {
    27  	return nil
    28  }
    29  
    30  func (l *Test1Handler) Emit(ctx *MessageContext, message string, args ...interface{}) error {
    31  	l.messages = append(l.messages, message)
    32  
    33  	return nil
    34  }
    35  
    36  func (l *Test1Handler) Printf(msg string, args ...interface{}) {}
    37  
    38  func (l *Test1Handler) Reset() {
    39  	l.messages = make([]string, 0)
    40  }
    41  
    42  func (l *Test1Handler) Len() int {
    43  	return len(l.messages)
    44  }
    45  
    46  func (l *Test1Handler) Close() {}
    47  
    48  func logAllLevels(msg string) {
    49  	Debug(msg)
    50  	Info(msg)
    51  	Warning(msg)
    52  	Error(msg)
    53  	// we do not test critical as it calls GoExit
    54  	// Critical(msg)
    55  }
    56  
    57  func Test_SetLevelByString(t *testing.T) {
    58  
    59  	w := &Test1Handler{DefaultFormatter, nil, nil}
    60  	SetHandler(w)
    61  
    62  	w.Reset()
    63  	// test levels
    64  	e := SetMinimalLevelByName("DEBUG")
    65  	fmt.Println(level)
    66  	if e != nil {
    67  		t.Fatalf("Could not set level by name: %s", e)
    68  	}
    69  	logAllLevels("Hello world")
    70  
    71  	if w.Len() != 4 {
    72  		t.Errorf("Not all levels logged - got  %d messages", w.Len())
    73  	}
    74  	e = SetMinimalLevelByName("ERROR")
    75  	if e != nil {
    76  		t.Fatalf("Could not set level by name: %s", e)
    77  	}
    78  	w.Reset()
    79  	logAllLevels("Hello world")
    80  	if w.Len() != 1 {
    81  		t.Errorf("Not all levels logged - got  %d messages", w.Len())
    82  	}
    83  
    84  	e = SetMinimalLevelByName("FOOBAR")
    85  	if e == nil {
    86  		t.Fatalf("This should have raised an error...")
    87  	}
    88  
    89  }
    90  func Test_Logging(t *testing.T) {
    91  
    92  	w := &Test1Handler{DefaultFormatter, nil, nil}
    93  	SetHandler(w)
    94  
    95  	w.Reset()
    96  	// test levels
    97  	SetLevel(0)
    98  	logAllLevels("Hello world")
    99  
   100  	if w.Len() > 0 {
   101  		t.Errorf("Got messages for level 0")
   102  	}
   103  
   104  	SetLevel(ALL)
   105  	w.Reset()
   106  	logAllLevels("Hello world")
   107  	fmt.Printf("Received %d messages\n", w.Len())
   108  	if w.Len() != 4 {
   109  		fmt.Println(w.messages)
   110  		t.Errorf("Did not log all errors (%d)", w.Len())
   111  	}
   112  
   113  	levels := []int{DEBUG, INFO, WARNING, ERROR}
   114  
   115  	for l := range levels {
   116  
   117  		w.Reset()
   118  		SetLevel(levels[l])
   119  		logAllLevels("Testing")
   120  
   121  		if !(w.Len() == 1 || (levels[l] == CRITICAL && w.Len() == 2)) {
   122  			t.Errorf("Wrong number of messages written: %d. level: %d", w.Len(), levels[l])
   123  		}
   124  	}
   125  
   126  }
   127  
   128  type TestHandler struct {
   129  	output    [][]interface{}
   130  	formatter Formatter
   131  	t         *testing.T
   132  }
   133  
   134  func (t *TestHandler) Emit(ctx *MessageContext, message string, args ...interface{}) error {
   135  	t.output = append(t.output, []interface{}{ctx.Level, ctx.File, message, ctx.Line, args})
   136  	fmt.Println(*ctx)
   137  	if ctx.Line <= 0 || ctx.Level == "" {
   138  		t.t.Fatalf("Invalid args")
   139  	}
   140  	return nil
   141  }
   142  
   143  func (t *TestHandler) SetFormatter(fmt Formatter) {
   144  	t.formatter = fmt
   145  }
   146  
   147  func (l *TestHandler) Output() io.Writer {
   148  	return nil
   149  }
   150  
   151  func (l *TestHandler) SetVerbose(v bool) {
   152  }
   153  
   154  func (l *TestHandler) Printf(msg string, args ...interface{}) {
   155  }
   156  
   157  func (l *TestHandler) Close() {}
   158  
   159  func Test_Handler(t *testing.T) {
   160  
   161  	handler := &TestHandler{
   162  		make([][]interface{}, 0),
   163  		DefaultFormatter,
   164  		t,
   165  	}
   166  	SetHandler(handler)
   167  
   168  	SetLevel(ALL)
   169  	Info("Foo Bar %s", 1)
   170  	Warning("Bar Baz %s", 2)
   171  
   172  	if len(handler.output) != 2 {
   173  		t.Fatal("Wrong len of output handler ")
   174  	}
   175  
   176  	fmt.Println("Passed testHandler")
   177  }
   178  
   179  func TestLogTail(t *testing.T) {
   180  	handler := &TestHandler{
   181  		make([][]interface{}, 0),
   182  		DefaultFormatter,
   183  		t,
   184  	}
   185  	SetHandler(handler)
   186  
   187  	SetLevel(ALL)
   188  	Info("Foo Bar %d", 1)
   189  	Warning("Bar Baz %d", 2)
   190  
   191  	contents := ReadTail()
   192  	fmt.Println(contents)
   193  	if !strings.Contains(contents, "[INF ") {
   194  		t.Fatal("Tail does not contain '[INF '")
   195  	}
   196  	if !strings.Contains(contents, "] Foo Bar 1") {
   197  		t.Fatal("Tail does not contain '] Foo Bar 1'")
   198  	}
   199  	if !strings.Contains(contents, "[WRN ") {
   200  		t.Fatal("Tail does not contain '[WRN '")
   201  	}
   202  	if !strings.Contains(contents, "] Bar Baz 2") {
   203  		t.Fatal("Tail does not contain '] Bar Baz 2'")
   204  	}
   205  }