From a9a27e2f5a3221238abc8766312981d4af7ffd97 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Mon, 20 Mar 2023 12:44:27 -0700 Subject: [PATCH] Fix loc dependent tests using runtime.Caller --- logger_loc_test.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/logger_loc_test.go b/logger_loc_test.go index c9cc991..a8b25ab 100644 --- a/logger_loc_test.go +++ b/logger_loc_test.go @@ -2,6 +2,8 @@ package hclog import ( "bytes" + "fmt" + "runtime" "strings" "testing" @@ -22,14 +24,19 @@ func TestLoggerLoc(t *testing.T) { IncludeLocation: true, }) + _, _, line, _ := runtime.Caller(0) + logger.Info("this is test", "who", "programmer", "why", "testing is fun") str := buf.String() dataIdx := strings.IndexByte(str, ' ') rest := str[dataIdx+1:] - // This test will break if you move this around, it's line dependent, just fyi - assert.Equal(t, "[INFO] go-hclog/logger_loc_test.go:25: test: this is test: who=programmer why=\"testing is fun\"\n", rest) + assert.Equal(t, + fmt.Sprintf( + "[INFO] go-hclog/logger_loc_test.go:%d: test: this is test: who=programmer why=\"testing is fun\"\n", + line+2), + rest) }) t.Run("includes the caller location excluding helper functions", func(t *testing.T) { @@ -46,14 +53,20 @@ func TestLoggerLoc(t *testing.T) { AdditionalLocationOffset: 1, }) + _, _, line, _ := runtime.Caller(0) + logMe(logger) str := buf.String() dataIdx := strings.IndexByte(str, ' ') rest := str[dataIdx+1:] - // This test will break if you move this around, it's line dependent, just fyi - assert.Equal(t, "[INFO] go-hclog/logger_loc_test.go:49: test: this is test: who=programmer why=\"testing is fun\"\n", rest) + assert.Equal(t, + fmt.Sprintf( + "[INFO] go-hclog/logger_loc_test.go:%d: test: this is test: who=programmer why=\"testing is fun\"\n", + line+2, + ), + rest) }) }