@@ -18,6 +18,7 @@ import (
1818 "bytes"
1919 "encoding/json"
2020 "regexp"
21+ "slices"
2122 "testing"
2223
2324 "github.com/stretchr/testify/require"
@@ -56,3 +57,79 @@ func TestEncodeTimePrecisionToMicroSeconds(t *testing.T) {
5657 require .Len (t , matches , 3 )
5758 require .Lenf (t , matches [1 ], fractionSecondsPrecision , "unexpected timestamp %s" , fields .Timestamp )
5859}
60+
61+ func TestMergeOutputPaths (t * testing.T ) {
62+ tests := []struct {
63+ name string
64+ cfg zap.Config
65+ want zap.Config
66+ }{
67+ {
68+ name : "OutputPaths /dev/null" ,
69+ cfg : zap.Config {
70+ OutputPaths : []string {"c" , "/dev/null" },
71+ ErrorOutputPaths : []string {"c" , "a" , "a" , "b" },
72+ },
73+ want : zap.Config {
74+ OutputPaths : []string {"/dev/null" },
75+ ErrorOutputPaths : []string {"a" , "b" , "c" },
76+ },
77+ },
78+ {
79+ name : "ErrorOutputPaths /dev/null" ,
80+ cfg : zap.Config {
81+ OutputPaths : []string {"c" , "a" , "a" , "b" },
82+ ErrorOutputPaths : []string {"/dev/null" , "c" },
83+ },
84+ want : zap.Config {
85+ OutputPaths : []string {"a" , "b" , "c" },
86+ ErrorOutputPaths : []string {"/dev/null" },
87+ },
88+ },
89+ {
90+ name : "empty slice" ,
91+ cfg : zap.Config {
92+ OutputPaths : []string {},
93+ ErrorOutputPaths : []string {"c" , "a" , "a" , "b" },
94+ },
95+ want : zap.Config {
96+ OutputPaths : []string {},
97+ ErrorOutputPaths : []string {"a" , "b" , "c" },
98+ },
99+ },
100+ {
101+ name : "nil slice" ,
102+ cfg : zap.Config {
103+ OutputPaths : []string {"c" , "a" , "a" , "b" },
104+ ErrorOutputPaths : nil ,
105+ },
106+ want : zap.Config {
107+ OutputPaths : []string {"a" , "b" , "c" },
108+ ErrorOutputPaths : []string {},
109+ },
110+ },
111+ {
112+ name : "normal" ,
113+ cfg : zap.Config {
114+ OutputPaths : []string {"c" , "a" , "a" , "b" },
115+ ErrorOutputPaths : []string {"c" , "a" , "a" , "b" },
116+ },
117+ want : zap.Config {
118+ OutputPaths : []string {"a" , "b" , "c" },
119+ ErrorOutputPaths : []string {"a" , "b" , "c" },
120+ },
121+ },
122+ }
123+ for _ , tt := range tests {
124+ t .Run (tt .name , func (t * testing.T ) {
125+ outputPaths := slices .Clone (tt .cfg .OutputPaths )
126+ errorOutputPaths := slices .Clone (tt .cfg .ErrorOutputPaths )
127+
128+ require .Equal (t , tt .want , MergeOutputPaths (tt .cfg ))
129+
130+ // ensure the OutputPaths and ErrorOutputPaths have not been modified
131+ require .Equal (t , outputPaths , tt .cfg .OutputPaths )
132+ require .Equal (t , errorOutputPaths , tt .cfg .ErrorOutputPaths )
133+ })
134+ }
135+ }
0 commit comments