Skip to content

Commit b9a9429

Browse files
committed
test-tool: extend trace2 helper with 400ancestry
Add a new test helper "400ancestry" to the trace2 test-tool that spawns a child process with a controlled trace2 environment, capturing only the child's trace2 output (including cmd_ancestry events) in isolation. The helper clears all inherited GIT_TRACE2* variables in the child and enables only the requested target (normal, perf, or event), directing output to a specified file. This gives the test suite a reliable way to capture cmd_ancestry events: the child always sees "test-tool" as its immediate parent in the process ancestry, providing a predictable value to verify in tests. Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
1 parent 6b90541 commit b9a9429

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

t/helper/test-trace2.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,63 @@ static int ut_303redact_def_param(int argc, const char **argv)
466466
return 0;
467467
}
468468

469+
/*
470+
* Run a child process with specific trace2 environment settings so that
471+
* we can capture its trace2 output (including cmd_ancestry) in isolation.
472+
*
473+
* test-tool trace2 400ancestry <target> <output_file> [<child_command_line>]
474+
*
475+
* <target> is one of: normal, perf, event
476+
*
477+
* For example:
478+
* test-tool trace2 400ancestry normal out.normal test-tool trace2 001return 0
479+
*
480+
* The child process inherits a controlled trace2 environment where only
481+
* the specified target is directed to <output_file>. The parent's trace2
482+
* environment variables are cleared in the child so that only the child's
483+
* events are captured.
484+
*
485+
* This is used by t0213-trace2-ancestry.sh to test cmd_ancestry events.
486+
* The child process will see "test-tool" as its immediate parent in the
487+
* process ancestry, giving us a predictable value to verify.
488+
*/
489+
static int ut_400ancestry(int argc, const char **argv)
490+
{
491+
struct child_process cmd = CHILD_PROCESS_INIT;
492+
const char *target;
493+
const char *outfile;
494+
int result;
495+
496+
if (argc < 3)
497+
die("expect <target> <output_file> <child_command_line>");
498+
499+
target = argv[0];
500+
outfile = argv[1];
501+
argv += 2;
502+
argc -= 2;
503+
504+
/* Clear all trace2 environment variables in the child. */
505+
strvec_push(&cmd.env, "GIT_TRACE2=");
506+
strvec_push(&cmd.env, "GIT_TRACE2_PERF=");
507+
strvec_push(&cmd.env, "GIT_TRACE2_EVENT=");
508+
strvec_push(&cmd.env, "GIT_TRACE2_BRIEF=1");
509+
510+
/* Set only the requested target. */
511+
if (!strcmp(target, "normal"))
512+
strvec_pushf(&cmd.env, "GIT_TRACE2=%s", outfile);
513+
else if (!strcmp(target, "perf"))
514+
strvec_pushf(&cmd.env, "GIT_TRACE2_PERF=%s", outfile);
515+
else if (!strcmp(target, "event"))
516+
strvec_pushf(&cmd.env, "GIT_TRACE2_EVENT=%s", outfile);
517+
else
518+
die("invalid target '%s', expected: normal, perf, event",
519+
target);
520+
521+
strvec_pushv(&cmd.args, argv);
522+
result = run_command(&cmd);
523+
exit(result);
524+
}
525+
469526
/*
470527
* Usage:
471528
* test-tool trace2 <ut_name_1> <ut_usage_1>
@@ -497,6 +554,8 @@ static struct unit_test ut_table[] = {
497554
{ ut_301redact_child_start, "301redact_child_start", "<argv...>" },
498555
{ ut_302redact_exec, "302redact_exec", "<exe> <argv...>" },
499556
{ ut_303redact_def_param, "303redact_def_param", "<key> <value>" },
557+
558+
{ ut_400ancestry, "400ancestry", "<target> <output_file> [<child_command_line>]" },
500559
};
501560
/* clang-format on */
502561

0 commit comments

Comments
 (0)