From e8fd0ba60913563af62ac5011b8bc436da0931b9 Mon Sep 17 00:00:00 2001 From: Lisa Ugray Date: Wed, 2 Jan 2019 14:58:51 -0500 Subject: [PATCH] Remove sensitivity to file line changes --- logrus_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/logrus_test.go b/logrus_test.go index b12d71c..afa41d2 100644 --- a/logrus_test.go +++ b/logrus_test.go @@ -3,9 +3,11 @@ package logrus_test import ( "bytes" "encoding/json" + "fmt" "io/ioutil" "os" "path/filepath" + "runtime" "sync" "testing" "time" @@ -338,6 +340,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) { llog := logger.WithField("context", "eating raw fish") llog.Info("looks delicious") + _, _, line, _ := runtime.Caller(0) err := json.Unmarshal(buffer.Bytes(), &fields) require.NoError(t, err, "should have decoded first message") @@ -348,7 +351,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) { "github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"]) cwd, err := os.Getwd() require.NoError(t, err) - assert.Equal(t, filepath.ToSlash(cwd+"/logrus_test.go:340"), filepath.ToSlash(fields["file"].(string))) + assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string))) buffer.Reset() @@ -363,6 +366,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) { }).WithFields(Fields{ "James": "Brown", }).Print("The hardest workin' man in show business") + _, _, line, _ = runtime.Caller(0) err = json.Unmarshal(buffer.Bytes(), &fields) assert.NoError(t, err, "should have decoded second message") @@ -377,7 +381,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) { assert.Equal(t, "github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"]) require.NoError(t, err) - assert.Equal(t, filepath.ToSlash(cwd+"/logrus_test.go:365"), filepath.ToSlash(fields["file"].(string))) + assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string))) logger.ReportCaller = false // return to default value }