MCPcopy
hub / github.com/uber-go/zap / TestConfig

Function TestConfig

config_test.go:34–86  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

32)
33
34func TestConfig(t *testing.T) {
35 tests := []struct {
36 desc string
37 cfg Config
38 expectN int64
39 expectRe string
40 }{
41 {
42 desc: "production",
43 cfg: NewProductionConfig(),
44 expectN: 2 + 100 + 1, // 2 from initial logs, 100 initial sampled logs, 1 from off-by-one in sampler
45 expectRe: `{"level":"info","caller":"[a-z0-9_-]+/config_test.go:\d+","msg":"info","k":"v","z":"zz"}` + "\n" +
46 `{"level":"warn","caller":"[a-z0-9_-]+/config_test.go:\d+","msg":"warn","k":"v","z":"zz"}` + "\n",
47 },
48 {
49 desc: "development",
50 cfg: NewDevelopmentConfig(),
51 expectN: 3 + 200, // 3 initial logs, all 200 subsequent logs
52 expectRe: "DEBUG\t[a-z0-9_-]+/config_test.go:" + `\d+` + "\tdebug\t" + `{"k": "v", "z": "zz"}` + "\n" +
53 "INFO\t[a-z0-9_-]+/config_test.go:" + `\d+` + "\tinfo\t" + `{"k": "v", "z": "zz"}` + "\n" +
54 "WARN\t[a-z0-9_-]+/config_test.go:" + `\d+` + "\twarn\t" + `{"k": "v", "z": "zz"}` + "\n" +
55 `go.uber.org/zap.TestConfig.\w+`,
56 },
57 }
58
59 for _, tt := range tests {
60 t.Run(tt.desc, func(t *testing.T) {
61 logOut := filepath.Join(t.TempDir(), "test.log")
62
63 tt.cfg.OutputPaths = []string{logOut}
64 tt.cfg.EncoderConfig.TimeKey = "" // no timestamps in tests
65 tt.cfg.InitialFields = map[string]interface{}{"z": "zz", "k": "v"}
66
67 hook, count := makeCountingHook()
68 logger, err := tt.cfg.Build(Hooks(hook))
69 require.NoError(t, err, "Unexpected error constructing logger.")
70
71 logger.Debug("debug")
72 logger.Info("info")
73 logger.Warn("warn")
74
75 byteContents, err := os.ReadFile(logOut)
76 require.NoError(t, err, "Couldn't read log contents from temp file.")
77 logs := string(byteContents)
78 assert.Regexp(t, tt.expectRe, logs, "Unexpected log output.")
79
80 for i := 0; i < 200; i++ {
81 logger.Info("sampling")
82 }
83 assert.Equal(t, tt.expectN, count.Load(), "Hook called an unexpected number of times.")
84 })
85 }
86}
87
88func TestConfigWithInvalidPaths(t *testing.T) {
89 tests := []struct {

Callers

nothing calls this directly

Calls 8

NewProductionConfigFunction · 0.85
NewDevelopmentConfigFunction · 0.85
makeCountingHookFunction · 0.85
HooksFunction · 0.85
BuildMethod · 0.80
DebugMethod · 0.45
InfoMethod · 0.45
WarnMethod · 0.45

Tested by

no test coverage detected