@@ -25,7 +25,7 @@ import (
2525 "unsafe"
2626
2727 "github.com/pingcap/errors"
28- "github.com/stretchr/testify/assert "
28+ "github.com/stretchr/testify/require "
2929 "go.uber.org/zap"
3030 "go.uber.org/zap/zapcore"
3131)
@@ -118,28 +118,30 @@ func TestLog(t *testing.T) {
118118 `<car><mirror>XML</mirror></car>]"] [duration=10s]` ,
119119 )
120120
121- assert .PanicsWithValue (t , "unknown" , func () { sugar .Panic ("unknown" ) })
121+ require .PanicsWithValue (t , "unknown" , func () { sugar .Panic ("unknown" ) })
122122}
123123
124124func TestTimeEncoder (t * testing.T ) {
125125 sec := int64 (1547192741 )
126126 nsec := int64 (165279177 )
127127 as , err := time .LoadLocation ("Asia/Shanghai" )
128- assert . Nil (t , err )
128+ require . NoError (t , err )
129129
130130 tt := time .Unix (sec , nsec ).In (as )
131131 conf := & Config {Level : "debug" , File : FileLogConfig {}, DisableTimestamp : true }
132- enc := NewTextEncoder (conf ).(* textEncoder )
133- DefaultTimeEncoder (tt , enc )
134- assert .Equal (t , "2019/01/11 15:45:41.165 +08:00" , enc .buf .String ())
132+ enc , err := NewTextEncoder (conf )
133+ require .NoError (t , err )
134+ DefaultTimeEncoder (tt , enc .(* textEncoder ))
135+ buf := enc .(* textEncoder ).buf
136+ require .Equal (t , "2019/01/11 15:45:41.165 +08:00" , buf .String ())
135137
136- enc . buf .Reset ()
138+ buf .Reset ()
137139 utc , err := time .LoadLocation ("UTC" )
138- assert . Nil (t , err )
140+ require . NoError (t , err )
139141
140142 utcTime := tt .In (utc )
141- DefaultTimeEncoder (utcTime , enc )
142- assert .Equal (t , "2019/01/11 07:45:41.165 +00:00" , enc . buf .String ())
143+ DefaultTimeEncoder (utcTime , enc .( * textEncoder ) )
144+ require .Equal (t , "2019/01/11 07:45:41.165 +00:00" , buf .String ())
143145}
144146
145147// See [logger-header]https://github.com/tikv/rfcs/blob/master/text/2018-12-19-unified-log-format.md#log-header-section.
@@ -156,13 +158,15 @@ func TestZapCaller(t *testing.T) {
156158 "ztest_coordinator1.go:20" ,
157159 "<unknown>" ,
158160 }
159- conf := & Config {Level : "deug" , File : FileLogConfig {}, DisableTimestamp : true }
160- enc := NewTextEncoder (conf ).(* textEncoder )
161+ conf := & Config {Level : "debug" , File : FileLogConfig {}, DisableTimestamp : true }
162+ enc , err := NewTextEncoder (conf )
163+ require .NoError (t , err )
161164
162165 for i , d := range data {
163- ShortCallerEncoder (d , enc )
164- assert .Equal (t , expect [i ], enc .buf .String ())
165- enc .buf .Reset ()
166+ ShortCallerEncoder (d , enc .(* textEncoder ))
167+ buf := enc .(* textEncoder ).buf
168+ require .Equal (t , expect [i ], buf .String ())
169+ buf .Reset ()
166170 }
167171}
168172
@@ -176,7 +180,7 @@ func TestRotateLog(t *testing.T) {
176180 },
177181 }
178182 logger , _ , err := InitLogger (conf )
179- assert . Nil (t , err )
183+ require . NoError (t , err )
180184
181185 var data []byte
182186 for i := 1 ; i <= 1 * 1024 * 1024 ; i ++ {
@@ -188,7 +192,7 @@ func TestRotateLog(t *testing.T) {
188192 data = data [:0 ]
189193 }
190194 files , _ := os .ReadDir (tempDir )
191- assert .Len (t , files , 2 )
195+ require .Len (t , files , 2 )
192196 _ = os .RemoveAll (tempDir )
193197}
194198
@@ -227,8 +231,8 @@ func TestLogJSON(t *testing.T) {
227231 "backoff" , time .Second ,
228232 )
229233 logger .With (zap .String ("connID" , "1" ), zap .String ("traceID" , "dse1121" )).Info ("new connection" )
230- ts .assertMessages ("{\" level\" :\" INFO\" ,\" caller\" :\" zap_log_test.go:224 \" ,\" message\" :\" failed to fetch URL\" ,\" url\" :\" http://example.com\" ,\" attempt\" :3,\" backoff\" :\" 1s\" }" ,
231- "{\" level\" :\" INFO\" ,\" caller\" :\" zap_log_test.go:229 \" ,\" message\" :\" new connection\" ,\" connID\" :\" 1\" ,\" traceID\" :\" dse1121\" }" )
234+ ts .assertMessages ("{\" level\" :\" INFO\" ,\" caller\" :\" zap_log_test.go:228 \" ,\" message\" :\" failed to fetch URL\" ,\" url\" :\" http://example.com\" ,\" attempt\" :3,\" backoff\" :\" 1s\" }" ,
235+ "{\" level\" :\" INFO\" ,\" caller\" :\" zap_log_test.go:233 \" ,\" message\" :\" new connection\" ,\" connID\" :\" 1\" ,\" traceID\" :\" dse1121\" }" )
232236}
233237
234238// testLogSpy is a testing.TB that captures logged messages.
@@ -270,23 +274,23 @@ func (t *testLogSpy) Logf(format string, args ...interface{}) {
270274}
271275
272276func (t * testLogSpy ) assertMessages (msgs ... string ) {
273- assert .Equal (t .TB , msgs , t .Messages )
277+ require .Equal (t .TB , msgs , t .Messages )
274278}
275279
276280func (t * testLogSpy ) assertMessagesContains (msg string ) {
277281 for _ , actualMsg := range t .Messages {
278- assert .Contains (t .TB , actualMsg , msg )
282+ require .Contains (t .TB , actualMsg , msg )
279283 }
280284}
281285
282286func (t * testLogSpy ) assertLastMessageContains (msg string ) {
283- assert .NotEmpty (t .TB , t .Messages )
287+ require .NotEmpty (t .TB , t .Messages )
284288 lastMsg := t .Messages [len (t .Messages )- 1 ]
285- assert .Contains (t .TB , lastMsg , msg )
289+ require .Contains (t .TB , lastMsg , msg )
286290}
287291
288292func (t * testLogSpy ) assertMessagesNotContains (msg string ) {
289293 for _ , actualMsg := range t .Messages {
290- assert .NotContains (t .TB , actualMsg , msg )
294+ require .NotContains (t .TB , actualMsg , msg )
291295 }
292296}
0 commit comments