test-lib: test_region looks for trace2 regions

From ff15d509b89edd4830d85d53cea3079a6b0c1c08 Mon Sep 17 00:00:00 2001 From: Derrick Stolee <dstolee@microsoft.com> Date: Mon, 11 Jan 2021 08:53:09 -0500 Subject: [PATCH 8/9] test-lib: test_region looks for trace2 regions Most test cases can verify Git's behavior using input/output expectations or changes to the .git directory. However, sometimes we want to check that Git did or did not run a certain section of code. This is particularly important for performance-only features that we want to ensure have been enabled in certain cases. Add a new 'test_region' function that checks if a trace2 region was entered and left in a given trace2 event log. There is one existing test (t0500-progress-display.sh) that performs this check already, so use the helper function instead. Note that this changes the expectations slightly. The old test (incorrectly) used two patterns for the 'grep' invocation, but this performs an OR of the patterns, not an AND. This means that as long as one region_enter event was logged, the test would succeed, even if it was not due to the progress category. More uses will be added in a later change. t6423-merge-rename-directories.sh also greps for region_enter lines, but it verifies the number of such lines, which is not the same as an existence check. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jan 23, 2021 at 16:07 UTC 3b14436364f0a68167723541bef1a625c5c6f163
2 files changed +43 -2
t/t0500-progress-display.sh
+1 -2
@@ -303,8 +303,7 @@ test_expect_success 'progress generates traces' '
303 "Working hard" <in 2>stderr &&
304
305 # t0212/parse_events.perl intentionally omits regions and data.
306 - grep -e "region_enter" -e "\"category\":\"progress\"" trace.event &&
307 - grep -e "region_leave" -e "\"category\":\"progress\"" trace.event &&
306 + test_region progress "Working hard" trace.event &&
307 grep "\"key\":\"total_objects\",\"value\":\"40\"" trace.event &&
308 grep "\"key\":\"total_bytes\",\"value\":\"409600\"" trace.event
309 '
t/test-lib-functions.sh
+42
@@ -1655,3 +1655,45 @@ test_subcommand () {
1655 grep "\[$expr\]"
1656 fi
1657 }
1658 +
1659 +# Check that the given command was invoked as part of the
1660 +# trace2-format trace on stdin.
1661 +#
1662 +# test_region [!] <category> <label> git <command> <args>...
1663 +#
1664 +# For example, to look for trace2_region_enter("index", "do_read_index", repo)
1665 +# in an invocation of "git checkout HEAD~1", run
1666 +#
1667 +# GIT_TRACE2_EVENT="$(pwd)/trace.txt" GIT_TRACE2_EVENT_NESTING=10 \
1668 +# git checkout HEAD~1 &&
1669 +# test_region index do_read_index <trace.txt
1670 +#
1671 +# If the first parameter passed is !, this instead checks that
1672 +# the given region was not entered.
1673 +#
1674 +test_region () {
1675 + local expect_exit=0
1676 + if test "$1" = "!"
1677 + then
1678 + expect_exit=1
1679 + shift
1680 + fi
1681 +
1682 + grep -e '"region_enter".*"category":"'"$1"'","label":"'"$2"\" "$3"
1683 + exitcode=$?
1684 +
1685 + if test $exitcode != $expect_exit
1686 + then
1687 + return 1
1688 + fi
1689 +
1690 + grep -e '"region_leave".*"category":"'"$1"'","label":"'"$2"\" "$3"
1691 + exitcode=$?
1692 +
1693 + if test $exitcode != $expect_exit
1694 + then
1695 + return 1
1696 + fi
1697 +
1698 + return 0
1699 +}