test-lib-functions: improve diagnostic output for trace2 data assertions

test_trace2_data is a bare grep that silently exits on failure. Add a more informative variant that verifies the event appears exactly once and reports what went wrong: key not found, multiple entries, or value mismatch. Diagnostics go to FD 4 like test_grep. Before (value mismatch): $ test_trace2_data status count/changed 999 <trace2.txt $ echo $? 1 (no output) After: $ test_trace2_data_singular status count/changed 999 <trace2.txt error: trace2 data 'status/count/changed' expected: 999 actual: 0 Signed-off-by: Kristofer Karlsson <krka@spotify.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kristofer Karlsson committed Jul 11, 2026 at 13:27 UTC e69450189128ae285871bd821f78c71e9f729599
1 file changed +36
t/test-lib-functions.sh
+36
@@ -1996,6 +1996,42 @@ test_trace2_data () {
1996 grep -e '"category":"'"$1"'","key":"'"$2"'","value":"'"$3"'"'
1997 }
1998
1999 +# Check that the given trace2 data event has the expected value and
2000 +# appears exactly once. Produces a diagnostic on failure.
2001 +#
2002 +# test_trace2_data_singular <category> <key> <value> [<label>]
2003 +test_trace2_data_singular () {
2004 + local category="$1" key="$2" expect_val="$3"
2005 + local label_suffix="${4:+ [$4]}"
2006 + local kv_pattern='"category":"'"$category"'","key":"'"$key"'","value":"\([^"]*\)"'
2007 + local actual
2008 +
2009 + actual=$(sed -n "s|.*${kv_pattern}.*|\1|p") &&
2010 +
2011 + if test -z "$actual"
2012 + then
2013 + echo >&4 "error: trace2 data '$category/$key'$label_suffix not found"
2014 + return 1
2015 + fi &&
2016 +
2017 + case "$actual" in
2018 + *"
2019 +"*)
2020 + echo >&4 "error: trace2 data '$category/$key'$label_suffix has multiple entries, expected 1"
2021 + printf '%s\n' "$actual" | sed 's/^/ actual: /' >&4
2022 + return 1
2023 + ;;
2024 + esac &&
2025 +
2026 + if test "$actual" != "$expect_val"
2027 + then
2028 + echo >&4 "error: trace2 data '$category/$key'$label_suffix"
2029 + echo >&4 " expected: $expect_val"
2030 + echo >&4 " actual: $actual"
2031 + return 1
2032 + fi
2033 +}
2034 +
2035 # Given a GIT_TRACE2_EVENT log over stdin, writes to stdout a list of URLs
2036 # sent to git-remote-https child processes.
2037 test_remote_https_urls() {