Skip to content

Commit

Permalink
client/pkg/logutil: add test for MergeOutputPaths
Browse files Browse the repository at this point in the history
Signed-off-by: Jes Cok <[email protected]>
  • Loading branch information
callthingsoff authored and gangli113 committed Jan 13, 2025
1 parent 0c18f4d commit 271cf45
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions client/pkg/logutil/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"encoding/json"
"regexp"
"slices"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -56,3 +57,79 @@ func TestEncodeTimePrecisionToMicroSeconds(t *testing.T) {
require.Len(t, matches, 3)
require.Lenf(t, matches[1], fractionSecondsPrecision, "unexpected timestamp %s", fields.Timestamp)
}

func TestMergeOutputPaths(t *testing.T) {
tests := []struct {
name string
cfg zap.Config
want zap.Config
}{
{
name: "OutputPaths /dev/null",
cfg: zap.Config{
OutputPaths: []string{"c", "/dev/null"},
ErrorOutputPaths: []string{"c", "a", "a", "b"},
},
want: zap.Config{
OutputPaths: []string{"/dev/null"},
ErrorOutputPaths: []string{"a", "b", "c"},
},
},
{
name: "ErrorOutputPaths /dev/null",
cfg: zap.Config{
OutputPaths: []string{"c", "a", "a", "b"},
ErrorOutputPaths: []string{"/dev/null", "c"},
},
want: zap.Config{
OutputPaths: []string{"a", "b", "c"},
ErrorOutputPaths: []string{"/dev/null"},
},
},
{
name: "empty slice",
cfg: zap.Config{
OutputPaths: []string{},
ErrorOutputPaths: []string{"c", "a", "a", "b"},
},
want: zap.Config{
OutputPaths: []string{},
ErrorOutputPaths: []string{"a", "b", "c"},
},
},
{
name: "nil slice",
cfg: zap.Config{
OutputPaths: []string{"c", "a", "a", "b"},
ErrorOutputPaths: nil,
},
want: zap.Config{
OutputPaths: []string{"a", "b", "c"},
ErrorOutputPaths: []string{},
},
},
{
name: "normal",
cfg: zap.Config{
OutputPaths: []string{"c", "a", "a", "b"},
ErrorOutputPaths: []string{"c", "a", "a", "b"},
},
want: zap.Config{
OutputPaths: []string{"a", "b", "c"},
ErrorOutputPaths: []string{"a", "b", "c"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
outputPaths := slices.Clone(tt.cfg.OutputPaths)
errorOutputPaths := slices.Clone(tt.cfg.ErrorOutputPaths)

require.Equal(t, tt.want, MergeOutputPaths(tt.cfg))

// ensure the OutputPaths and ErrorOutputPaths have not been modified
require.Equal(t, outputPaths, tt.cfg.OutputPaths)
require.Equal(t, errorOutputPaths, tt.cfg.ErrorOutputPaths)
})
}
}

0 comments on commit 271cf45

Please sign in to comment.