-
Notifications
You must be signed in to change notification settings - Fork 42
/
logger_loc_test.go
75 lines (57 loc) · 1.69 KB
/
logger_loc_test.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MIT
package hclog
import (
"bytes"
"fmt"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// This file contains tests that are sensitive to their location in the file,
// because they contain line numbers. They're basically "quarantined" from the
// other tests because they break all the time when new tests are added.
func TestLoggerLoc(t *testing.T) {
t.Run("includes the caller location", func(t *testing.T) {
var buf bytes.Buffer
logger := New(&LoggerOptions{
Name: "test",
Output: &buf,
IncludeLocation: true,
})
_, _, line, _ := runtime.Caller(0)
logger.Info("this is test", "who", "programmer", "why", "testing is fun")
str := buf.String()
dataIdx := strings.IndexByte(str, ' ')
rest := str[dataIdx+1:]
assert.Equal(t,
fmt.Sprintf(
"[INFO] go-hclog/logger_loc_test.go:%d: test: this is test: who=programmer why=\"testing is fun\"\n",
line+2),
rest)
})
t.Run("includes the caller location excluding helper functions", func(t *testing.T) {
var buf bytes.Buffer
logMe := func(l Logger) {
l.Info("this is test", "who", "programmer", "why", "testing is fun")
}
logger := New(&LoggerOptions{
Name: "test",
Output: &buf,
IncludeLocation: true,
AdditionalLocationOffset: 1,
})
_, _, line, _ := runtime.Caller(0)
logMe(logger)
str := buf.String()
dataIdx := strings.IndexByte(str, ' ')
rest := str[dataIdx+1:]
assert.Equal(t,
fmt.Sprintf(
"[INFO] go-hclog/logger_loc_test.go:%d: test: this is test: who=programmer why=\"testing is fun\"\n",
line+2,
),
rest)
})
}