Skip to content

Commit 91e42c8

Browse files
committed
t0213: add trace2 cmd_ancestry tests
Add a new test script t0213-trace2-ancestry.sh that verifies cmd_ancestry events across all three trace2 output formats (normal, perf, and event). The tests use the "400ancestry" test helper to spawn child processes with controlled trace2 environments. Git alias resolution (which spawns a child git process) creates a predictable multi-level process tree. Filter functions extract cmd_ancestry events from each format, truncating the ancestor list at the outermost "test-tool" so that only the controlled portion of the tree is verified, regardless of the test runner environment. A runtime prerequisite (TRACE2_ANCESTRY) is used to detect whether the platform has a real procinfo implementation; platforms with only the stub are skipped. Also update the comment in t0210-trace2-normal.sh to reflect that ancestry testing now has its own dedicated test script. Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
1 parent b9a9429 commit 91e42c8

3 files changed

Lines changed: 172 additions & 2 deletions

File tree

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ integration_tests = [
131131
't0210-trace2-normal.sh',
132132
't0211-trace2-perf.sh',
133133
't0212-trace2-event.sh',
134+
't0213-trace2-ancestry.sh',
134135
't0300-credentials.sh',
135136
't0301-credential-cache.sh',
136137
't0302-credential-store.sh',

t/t0210-trace2-normal.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ scrub_normal () {
7474
# This line is only emitted when RUNTIME_PREFIX is defined,
7575
# so just omit it for testing purposes.
7676
#
77-
# 4. 'cmd_ancestry' is not implemented everywhere, so for portability's
78-
# sake, skip it when parsing normal.
77+
# 4. 'cmd_ancestry' output depends on how the test is run and
78+
# is not relevant to the features we are testing here.
79+
# Ancestry tests are covered in t0213-trace2-ancestry.sh instead.
7980
sed \
8081
-e 's/elapsed:[0-9]*\.[0-9][0-9]*\([eE][-+]\{0,1\}[0-9][0-9]*\)\{0,1\}/elapsed:_TIME_/g' \
8182
-e "s/^start '[^']*' \(.*\)/start _EXE_ \1/" \

t/t0213-trace2-ancestry.sh

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/bin/sh
2+
3+
test_description='test trace2 cmd_ancestry event'
4+
5+
. ./test-lib.sh
6+
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
10+
sane_unset GIT_TRACE2_CONFIG_PARAMS
11+
12+
# Add t/helper directory to PATH so that we can use a relative
13+
# path to run nested instances of test-tool.exe (see 004child).
14+
# This helps with HEREDOC comparisons later.
15+
TTDIR="$GIT_BUILD_DIR/t/helper/" && export TTDIR
16+
PATH="$TTDIR:$PATH" && export PATH
17+
18+
# The 400ancestry helper spawns a child process so that the child
19+
# sees "test-tool" in its process ancestry. We capture only the
20+
# child's trace2 output to a file.
21+
#
22+
# The tests use git commands that spawn child git processes (e.g.,
23+
# alias resolution) to create a controlled multi-level process tree.
24+
# Because cmd_ancestry walks the real process tree, processes will
25+
# also report ancestors above "test-tool" that depend on the test
26+
# runner environment (e.g., bash, make, tmux). The filter functions
27+
# below truncate the ancestry at "test-tool", discarding anything
28+
# above it, so only the controlled portion is verified.
29+
#
30+
# On platforms without a real procinfo implementation (the stub),
31+
# no cmd_ancestry event is emitted. We detect this at runtime and
32+
# skip the format-specific tests accordingly.
33+
34+
# Determine if cmd_ancestry is supported on this platform.
35+
test_expect_success 'detect cmd_ancestry support' '
36+
test_when_finished "rm -f trace.detect" &&
37+
GIT_TRACE2_BRIEF=1 GIT_TRACE2="$(pwd)/trace.detect" \
38+
test-tool trace2 001return 0 &&
39+
if grep -q "^cmd_ancestry" trace.detect
40+
then
41+
test_set_prereq TRACE2_ANCESTRY
42+
fi
43+
'
44+
45+
# Filter functions for each trace2 target format.
46+
#
47+
# Each extracts cmd_ancestry events, strips format-specific syntax,
48+
# and truncates the ancestor list at the outermost "test-tool",
49+
# discarding any higher-level (uncontrolled) ancestors.
50+
#
51+
# For Windows, we also strip ".exe" file extensions to simplify comparisons.
52+
#
53+
# Output is a space-separated list of ancestor names, one line per
54+
# cmd_ancestry event, with the immediate parent listed first:
55+
#
56+
# test-tool
57+
# git test-tool
58+
# git test-tool test-tool
59+
60+
filter_ancestry_normal () {
61+
sed -n '/^cmd_ancestry/{
62+
s/^cmd_ancestry //
63+
s/ <- / /g
64+
s/\.exe//g
65+
s/\(.*test-tool\) .*/\1/
66+
p
67+
}'
68+
}
69+
70+
filter_ancestry_perf () {
71+
sed -n '/cmd_ancestry/{
72+
s/.*ancestry:\[//
73+
s/\]//
74+
s/\.exe//g
75+
s/\(.*test-tool\) .*/\1/
76+
p
77+
}'
78+
}
79+
80+
filter_ancestry_event () {
81+
sed -n '/"cmd_ancestry"/{
82+
s/.*"ancestry":\[//
83+
s/\].*//
84+
s/"//g
85+
s/,/ /g
86+
s/\.exe//g
87+
s/\(.*test-tool\) .*/\1/
88+
p
89+
}'
90+
}
91+
92+
# Git alias resolution spawns the target command as a child process.
93+
# Using "git -c alias.xyz=version xyz" creates a two-level chain:
94+
#
95+
# test-tool (400ancestry)
96+
# -> git (resolves alias xyz -> version)
97+
# -> git (version)
98+
#
99+
# Both git processes are instrumented and emit cmd_ancestry. After
100+
# filtering out ancestors above test-tool, we get:
101+
#
102+
# test-tool (from git alias resolver)
103+
# git test-tool (from git version)
104+
105+
test_expect_success TRACE2_ANCESTRY 'normal: git alias chain, 2 levels' '
106+
test_when_finished "rm -f trace.normal actual expect" &&
107+
test-tool trace2 400ancestry normal "$(pwd)/trace.normal" \
108+
git -c alias.xyz=version xyz &&
109+
filter_ancestry_normal <trace.normal >actual &&
110+
cat >expect <<-\EOF &&
111+
test-tool
112+
git test-tool
113+
EOF
114+
test_cmp expect actual
115+
'
116+
117+
test_expect_success TRACE2_ANCESTRY 'perf: git alias chain, 2 levels' '
118+
test_when_finished "rm -f trace.perf actual expect" &&
119+
test-tool trace2 400ancestry perf "$(pwd)/trace.perf" \
120+
git -c alias.xyz=version xyz &&
121+
filter_ancestry_perf <trace.perf >actual &&
122+
cat >expect <<-\EOF &&
123+
test-tool
124+
git test-tool
125+
EOF
126+
test_cmp expect actual
127+
'
128+
129+
test_expect_success TRACE2_ANCESTRY 'event: git alias chain, 2 levels' '
130+
test_when_finished "rm -f trace.event actual expect" &&
131+
test-tool trace2 400ancestry event "$(pwd)/trace.event" \
132+
git -c alias.xyz=version xyz &&
133+
filter_ancestry_event <trace.event >actual &&
134+
cat >expect <<-\EOF &&
135+
test-tool
136+
git test-tool
137+
EOF
138+
test_cmp expect actual
139+
'
140+
141+
# Use 004child to add a test-tool layer, creating a three-level chain:
142+
#
143+
# test-tool (400ancestry)
144+
# -> test-tool (004child)
145+
# -> git (resolves alias xyz -> version)
146+
# -> git (version)
147+
#
148+
# Three instrumented processes emit cmd_ancestry. After filtering:
149+
#
150+
# test-tool (from test-tool 004child)
151+
# test-tool test-tool (from git alias resolver)
152+
# git test-tool test-tool (from git version)
153+
154+
test_expect_success TRACE2_ANCESTRY 'normal: deeper chain, 3 levels' '
155+
test_when_finished "rm -f trace.normal actual expect" &&
156+
test-tool trace2 400ancestry normal "$(pwd)/trace.normal" \
157+
test-tool trace2 004child \
158+
git -c alias.xyz=version xyz &&
159+
filter_ancestry_normal <trace.normal >actual &&
160+
cat >expect <<-\EOF &&
161+
test-tool
162+
test-tool test-tool
163+
git test-tool test-tool
164+
EOF
165+
test_cmp expect actual
166+
'
167+
168+
test_done

0 commit comments

Comments
 (0)