t0210: refactor trace2 scrubbing to not use Perl

The output generated by our trace2 mechanism contains several fields that are dependent on the environment they're being run in, which makes it somewhat harder to test it. As a countermeasure we scrub the output and strip out any fields that contain such information. The logic to do so is implemented in Perl, but it can be trivially ported to instead use sed(1). Refactor the code accordingly so that we can drop the PERL_TEST_HELPERS prerequisite. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 3, 2025 at 07:06 UTC 9f4bce35b399508737e30bb5b22787d86cb5001f
2 files changed +43 -72
t/t0210-trace2-normal.sh
+43 -18
@@ -4,12 +4,6 @@ test_description='test trace2 facility (normal target)'
4
5 . ./test-lib.sh
6
7 -if ! test_have_prereq PERL_TEST_HELPERS
8 -then
9 - skip_all='skipping trace2 tests; Perl not available'
10 - test_done
11 -fi
12 -
7 # Turn off any inherited trace2 settings for this test.
8 sane_unset GIT_TRACE2 GIT_TRACE2_PERF GIT_TRACE2_EVENT
9 sane_unset GIT_TRACE2_BRIEF
@@ -59,10 +53,41 @@ GIT_TRACE2_BRIEF=1 && export GIT_TRACE2_BRIEF
53 #
54 # Implicit return from cmd_<verb> function propagates <code>.
55
56 +scrub_normal () {
57 + # Scrub the variable fields from the normal trace2 output to make
58 + # testing easier:
59 + #
60 + # 1. Various messages include an elapsed time in the middle of the
61 + # message. Replace the time with a placeholder to simplify our
62 + # HEREDOC in the test script.
63 + #
64 + # 2. We expect:
65 + #
66 + # start <argv0> [<argv1> [<argv2> [...]]]
67 + #
68 + # where argv0 might be a relative or absolute path, with or
69 + # without quotes, and platform dependent. Replace argv0 with a
70 + # token for HEREDOC matching in the test script.
71 + #
72 + # 3. Likewise, the 'cmd_path' message breaks out argv[0].
73 + #
74 + # This line is only emitted when RUNTIME_PREFIX is defined,
75 + # so just omit it for testing purposes.
76 + #
77 + # 4. 'cmd_ancestry' is not implemented everywhere, so for portability's
78 + # sake, skip it when parsing normal.
79 + sed \
80 + -e 's/elapsed:[0-9]*\.[0-9][0-9]*\([eE][-+]\{0,1\}[0-9][0-9]*\)\{0,1\}/elapsed:_TIME_/g' \
81 + -e "s/^start '[^']*' \(.*\)/start _EXE_ \1/" \
82 + -e 's/^start [^ ][^ ]* \(.*\)/start _EXE_ \1/' \
83 + -e '/^cmd_path/d' \
84 + -e '/^cmd_ancestry/d'
85 +}
86 +
87 test_expect_success 'normal stream, return code 0' '
88 test_when_finished "rm trace.normal actual expect" &&
89 GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 001return 0 &&
65 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
90 + scrub_normal <trace.normal >actual &&
91 cat >expect <<-EOF &&
92 version $V
93 start _EXE_ trace2 001return 0
@@ -76,7 +101,7 @@ test_expect_success 'normal stream, return code 0' '
101 test_expect_success 'normal stream, return code 1' '
102 test_when_finished "rm trace.normal actual expect" &&
103 test_must_fail env GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 001return 1 &&
79 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
104 + scrub_normal <trace.normal >actual &&
105 cat >expect <<-EOF &&
106 version $V
107 start _EXE_ trace2 001return 1
@@ -91,7 +116,7 @@ test_expect_success 'automatic filename' '
116 test_when_finished "rm -r traces actual expect" &&
117 mkdir traces &&
118 GIT_TRACE2="$(pwd)/traces" test-tool trace2 001return 0 &&
94 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <"$(ls traces/*)" >actual &&
119 + scrub_normal <"$(ls traces/*)" >actual &&
120 cat >expect <<-EOF &&
121 version $V
122 start _EXE_ trace2 001return 0
@@ -109,7 +134,7 @@ test_expect_success 'automatic filename' '
134 test_expect_success 'normal stream, exit code 0' '
135 test_when_finished "rm trace.normal actual expect" &&
136 GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 002exit 0 &&
112 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
137 + scrub_normal <trace.normal >actual &&
138 cat >expect <<-EOF &&
139 version $V
140 start _EXE_ trace2 002exit 0
@@ -123,7 +148,7 @@ test_expect_success 'normal stream, exit code 0' '
148 test_expect_success 'normal stream, exit code 1' '
149 test_when_finished "rm trace.normal actual expect" &&
150 test_must_fail env GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 002exit 1 &&
126 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
151 + scrub_normal <trace.normal >actual &&
152 cat >expect <<-EOF &&
153 version $V
154 start _EXE_ trace2 002exit 1
@@ -141,7 +166,7 @@ test_expect_success 'normal stream, exit code 1' '
166 test_expect_success 'normal stream, error event' '
167 test_when_finished "rm trace.normal actual expect" &&
168 GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 003error "hello world" "this is a test" &&
144 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
169 + scrub_normal <trace.normal >actual &&
170 cat >expect <<-EOF &&
171 version $V
172 start _EXE_ trace2 003error '\''hello world'\'' '\''this is a test'\''
@@ -161,7 +186,7 @@ test_expect_success 'normal stream, error event' '
186 test_expect_success 'BUG messages are written to trace2' '
187 test_when_finished "rm trace.normal actual expect" &&
188 test_must_fail env GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 007bug &&
164 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
189 + scrub_normal <trace.normal >actual &&
190 cat >expect <<-EOF &&
191 version $V
192 start _EXE_ trace2 007bug
@@ -185,7 +210,7 @@ test_expect_success 'bug messages with BUG_if_bug() are written to trace2' '
210 sed "s/^.*: //" <err >actual &&
211 test_cmp expect actual &&
212
188 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
213 + scrub_normal <trace.normal >actual &&
214 cat >expect <<-EOF &&
215 version $V
216 start _EXE_ trace2 008bug
@@ -211,7 +236,7 @@ test_expect_success 'bug messages without explicit BUG_if_bug() are written to t
236 sed "s/^.*: //" <err >actual &&
237 test_cmp expect actual &&
238
214 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
239 + scrub_normal <trace.normal >actual &&
240 cat >expect <<-EOF &&
241 version $V
242 start _EXE_ trace2 009bug_BUG
@@ -236,7 +261,7 @@ test_expect_success 'bug messages followed by BUG() are written to trace2' '
261 sed "s/^.*: //" <err >actual &&
262 test_cmp expect actual &&
263
239 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
264 + scrub_normal <trace.normal >actual &&
265 cat >expect <<-EOF &&
266 version $V
267 start _EXE_ trace2 010bug_BUG
@@ -268,7 +293,7 @@ test_expect_success 'using global config, normal stream, return code 0' '
293 test_config_global trace2.normalBrief 1 &&
294 test_config_global trace2.normalTarget "$(pwd)/trace.normal" &&
295 test-tool trace2 001return 0 &&
271 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
296 + scrub_normal <trace.normal >actual &&
297 cat >expect <<-EOF &&
298 version $V
299 start _EXE_ trace2 001return 0
@@ -286,7 +311,7 @@ test_expect_success 'using global config with include' '
311 mv "$(pwd)/.gitconfig" "$(pwd)/real.gitconfig" &&
312 test_config_global include.path "$(pwd)/real.gitconfig" &&
313 test-tool trace2 001return 0 &&
289 - perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
314 + scrub_normal <trace.normal >actual &&
315 cat >expect <<-EOF &&
316 version $V
317 start _EXE_ trace2 001return 0
t/t0210/scrub_normal.perl deleted
-54
@@ -1,54 +0,0 @@
1 -#!/usr/bin/perl
2 -#
3 -# Scrub the variable fields from the normal trace2 output to
4 -# make testing easier.
5 -
6 -use strict;
7 -use warnings;
8 -
9 -my $float = '[0-9]*\.[0-9]+([eE][-+]?[0-9]+)?';
10 -
11 -# This code assumes that the trace2 data was written with bare
12 -# turned on (which omits the "<clock> <file>:<line>" prefix.
13 -
14 -while (<>) {
15 - # Various messages include an elapsed time in the middle
16 - # of the message. Replace the time with a placeholder to
17 - # simplify our HEREDOC in the test script.
18 - s/elapsed:$float/elapsed:_TIME_/g;
19 -
20 - my $line = $_;
21 -
22 - # we expect:
23 - # start <argv0> [<argv1> [<argv2> [...]]]
24 - #
25 - # where argv0 might be a relative or absolute path, with
26 - # or without quotes, and platform dependent. Replace argv0
27 - # with a token for HEREDOC matching in the test script.
28 -
29 - if ($line =~ m/^start/) {
30 - $line =~ /^start\s+(.*)/;
31 - my $argv = $1;
32 - $argv =~ m/(\'[^\']*\'|[^ ]+)\s+(.*)/;
33 - my $argv_0 = $1;
34 - my $argv_rest = $2;
35 -
36 - print "start _EXE_ $argv_rest\n";
37 - }
38 - elsif ($line =~ m/^cmd_path/) {
39 - # Likewise, the 'cmd_path' message breaks out argv[0].
40 - #
41 - # This line is only emitted when RUNTIME_PREFIX is defined,
42 - # so just omit it for testing purposes.
43 - # print "cmd_path _EXE_\n";
44 - }
45 - elsif ($line =~ m/^cmd_ancestry/) {
46 - # 'cmd_ancestry' is not implemented everywhere, so for portability's
47 - # sake, skip it when parsing normal.
48 - #
49 - # print "$line";
50 - }
51 - else {
52 - print "$line";
53 - }
54 -}