logger.TestLogger   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 71
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 54
dl 0
loc 71
c 0
b 0
f 0
rs 7.1054
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package logger
2
3
import (
4
	"bytes"
5
	"errors"
6
	"fmt"
7
	"os"
8
	"os/exec"
9
	"testing"
10
11
	"github.com/stretchr/testify/require"
12
)
13
14
func TestLogger(t *testing.T) {
15
	tests := []struct {
16
		name        string
17
		level       string
18
		funcName    string
19
		message     string
20
		expectedMsg string
21
	}{
22
		{
23
			name:        "error msg",
24
			level:       "ERROR",
25
			funcName:    "Error",
26
			message:     "This is error message",
27
			expectedMsg: "ERROR:This is error message",
28
		},
29
		{
30
			name:        "skipp_warn",
31
			level:       "ERROR",
32
			funcName:    "Warn",
33
			message:     "This is error message",
34
			expectedMsg: "",
35
		},
36
		{
37
			name:        "skipp_info",
38
			level:       "ERROR",
39
			funcName:    "Info",
40
			message:     "This is error message",
41
			expectedMsg: "",
42
		},
43
		{
44
			name:        "skipp_debug",
45
			level:       "ERROR",
46
			funcName:    "Debug",
47
			message:     "This is error message",
48
			expectedMsg: "",
49
		},
50
		{
51
			name:        "debug",
52
			level:       "DEBUG",
53
			funcName:    "Debug",
54
			message:     "This is error message",
55
			expectedMsg: "DEBUG:This is error message",
56
		},
57
		{
58
			name:        "skipp_debug2",
59
			level:       "INFO",
60
			funcName:    "Debug",
61
			message:     "This is error message",
62
			expectedMsg: "",
63
		},
64
	}
65
66
	t.Parallel()
67
	for _, tc := range tests {
68
		t.Run(fmt.Sprintf("case %s", tc.name), func(t *testing.T) {
69
			var b bytes.Buffer
70
			tc := tc
71
			l := NewLogger(tc.level, &b)
72
73
			switch tc.funcName {
74
			case "Error":
75
				l.Errorf(tc.message)
0 ignored issues
show
introduced by
can't check non-constant format in call to Errorf
Loading history...
76
			case "Warn":
77
				l.Warningf(tc.message)
78
			case "Info":
79
				l.Infof(tc.message)
80
			case "Debug":
81
				l.Debugf(tc.message)
82
			}
83
84
			require.Equal(t, tc.expectedMsg, b.String(), "error output message")
85
		})
86
	}
87
}
88
89
func TestFatalf(t *testing.T) {
90
	if os.Getenv("BE_CRASHER") == "1" {
91
		var b bytes.Buffer
92
		_ = NewLogger("WRONG DB TYPE", &b)
93
		return
94
	}
95
	cmd := exec.Command(os.Args[0], "-test.run=TestFatalf")
96
	cmd.Env = append(os.Environ(), "BE_CRASHER=1")
97
	err := cmd.Run()
98
99
	var e *exec.ExitError
100
	require.True(t, err != nil && errors.As(err, &e), "process ran with err %v, want exit status 1", err)
101
}
102