From 8c4c850a7bf7d732350401e7bc218a1a94a5a955 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 4 Jul 2023 17:58:17 +0530 Subject: [PATCH] Show invocation line for failing test --- tools/rsync/api_test.go | 3 ++- tools/utils/misc.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tools/rsync/api_test.go b/tools/rsync/api_test.go index 7772615c0ef..a67014c900f 100644 --- a/tools/rsync/api_test.go +++ b/tools/rsync/api_test.go @@ -20,7 +20,8 @@ func run_roundtrip_test(t *testing.T, src_data, changed []byte, num_of_patches, using_serialization := false prefix_msg := func() string { q := utils.IfElse(using_serialization, "with", "without") - return fmt.Sprintf("Running %s serialization: src size: %d changed size: %d difference: %d\n", q, len(src_data), len(changed), len(changed)-len(src_data)) + return fmt.Sprintf("%s: Running %s serialization: src size: %d changed size: %d difference: %d\n", + utils.SourceLoc(1), q, len(src_data), len(changed), len(changed)-len(src_data)) } test_equal := func(src_data, output []byte) { diff --git a/tools/utils/misc.go b/tools/utils/misc.go index 4cd0fd0f175..d83460eb8b0 100644 --- a/tools/utils/misc.go +++ b/tools/utils/misc.go @@ -5,7 +5,9 @@ package utils import ( "fmt" "os" + "path/filepath" "reflect" + "runtime" "strconv" "golang.org/x/exp/constraints" @@ -270,3 +272,25 @@ func IfElse[T any](condition bool, if_val T, else_val T) T { } return else_val } + +func SourceLine(skip_frames ...int) int { + s := 1 + if len(skip_frames) > 0 { + s += skip_frames[0] + } + if _, _, ans, ok := runtime.Caller(s); ok { + return ans + } + return -1 +} + +func SourceLoc(skip_frames ...int) string { + s := 1 + if len(skip_frames) > 0 { + s += skip_frames[0] + } + if _, file, line, ok := runtime.Caller(s); ok { + return filepath.Base(file) + ":" + strconv.Itoa(line) + } + return "unknown" +}