|
| 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