File: logger_test.go

package info (click to toggle)
golang-github-scaleway-scaleway-sdk-go 1.0.0~beta32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,444 kB
  • sloc: sh: 70; makefile: 3
file content (82 lines) | stat: -rw-r--r-- 1,814 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package logger

import (
	"bytes"
	"os"
	"strings"
	"testing"

	"github.com/scaleway/scaleway-sdk-go/internal/testhelpers"
)

var (
	expectedErrorf   = "ERROR: cd"
	expectedWarningf = "WARNING: ij"
	expectedInfof    = "INFO: op"
	expectedDebugf   = "DEBUG: uv"
)

func TestDebug(t *testing.T) {
	buf := &bytes.Buffer{}
	logThings(newLogger(buf, LogLevelDebug))
	testThings(t, []string{
		expectedErrorf,
		expectedWarningf,
		expectedInfof,
		expectedDebugf,
	}, buf.String())
}

func TestInfo(t *testing.T) {
	buf := &bytes.Buffer{}
	logThings(newLogger(buf, LogLevelInfo))
	testThings(t, []string{
		expectedErrorf,
		expectedWarningf,
		expectedInfof,
	}, buf.String())
}

func TestWarning(t *testing.T) {
	buf := &bytes.Buffer{}
	logThings(newLogger(buf, LogLevelWarning))
	testThings(t, []string{
		expectedErrorf,
		expectedWarningf,
	}, buf.String())
}

func TestError(t *testing.T) {
	buf := &bytes.Buffer{}
	logThings(newLogger(buf, LogLevelError))
	testThings(t, []string{
		expectedErrorf,
	}, buf.String())
}

func TestEnableDebugMode(t *testing.T) {
	_defaultLogger := DefaultLogger

	DefaultLogger = newLogger(os.Stderr, LogLevelWarning)
	EnableDebugMode()
	testhelpers.Equals(t, true, DefaultLogger.ShouldLog(LogLevelDebug))

	DefaultLogger = _defaultLogger
}

func testThings(t *testing.T, expectedEvents []string, actualOutput string) {
	t.Helper()
	lines := strings.Split(actualOutput, "\n")
	for i, line := range lines[:len(lines)-1] { // last line is always empty
		tmp := strings.Split(line, " ")
		actualMessage := strings.Join(append([]string{tmp[0]}, tmp[3:]...), " ") // date and hour is not kept
		testhelpers.Equals(t, expectedEvents[i], actualMessage)
	}
}

func logThings(log Logger) {
	log.Errorf("c%s", "d")
	log.Warningf("i%s", "j")
	log.Infof("o%s", "p")
	log.Debugf("u%s", "v")
}