Skip to content

Commit 4879ffa

Browse files
committed
test: close file
1 parent bad0205 commit 4879ffa

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

internal/zapencoder/encoder_test.go renamed to internal/zapencoder/patch_test.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ func encodeTimeLayout(t time.Time, layout string, enc zapcore.PrimitiveArrayEnco
5050
enc.AppendString(t.Format(layout))
5151
}
5252

53-
// ShortCallerEncoder serializes a caller in package/file:line format, trimming
54-
// all but the final directory from the full path.
55-
func shortCallerEncoder(_ zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
56-
// TODO: consider using a byte-oriented API to save an allocation.
57-
enc.AppendString("caller.go")
58-
}
59-
6053
func TestEncoder(t *testing.T) {
6154
const encoderName = "zapencoder"
6255
err := zap.RegisterEncoder(encoderName, func(config zapcore.EncoderConfig) (zapcore.Encoder, error) {
@@ -65,19 +58,21 @@ func TestEncoder(t *testing.T) {
6558
})
6659
require.NoError(t, err)
6760

68-
cfg := zap.NewProductionConfig()
69-
cfg.Encoding = encoderName
70-
cfg.EncoderConfig.EncodeTime = constantTimeEncoder(time.Date(2024, 1, 2, 15, 4, 5, 0, time.UTC))
71-
cfg.EncoderConfig.EncodeCaller = shortCallerEncoder
72-
61+
// open a file, and hold reference to the close
7362
dir := t.TempDir()
7463
outPath := filepath.Join(dir, "output.jsonl")
75-
outErrPath := filepath.Join(dir, "error.jsonl")
76-
cfg.OutputPaths = []string{outPath}
77-
cfg.ErrorOutputPaths = []string{outErrPath}
7864

79-
lg, err := cfg.Build()
65+
writer, closeFile, err := zap.Open(outPath)
8066
require.NoError(t, err)
67+
t.Cleanup(func() {
68+
closeFile()
69+
})
70+
71+
encoderConfig := zap.NewProductionEncoderConfig()
72+
encoderConfig.EncodeTime = constantTimeEncoder(time.Date(2024, 1, 2, 15, 4, 5, 0, time.UTC))
73+
74+
core := zapcore.NewCore(zapencoder.New(encoderConfig), writer, zap.NewAtomicLevel())
75+
lg := zap.New(core)
8176

8277
ctx := t.Context()
8378
lg.Info("With context",

0 commit comments

Comments
 (0)