f2/testlib: support wrapping commands

It's useful to obtain a fully assembled command and then wrap it to
execute into something else, e.g. gdb, strace or fakeroot.

Change-Id: Ida5928fd001925549eab89edfbf0facada7ef7f1
This commit is contained in:
rootile
2026-04-30 16:59:28 +02:00
parent e70ae36b3f
commit 7017def185
2 changed files with 17 additions and 0 deletions
@@ -149,6 +149,16 @@ class Command:
self.argv = list(argv)
return self
def with_wrapper(self, cmd: str | Path, *args: list[str]) -> "Command":
"""
Wraps the current command in the given wrapper.
e.g: Command(["nix", "eval", "1+1"]).with_wrapper("strace") => Command(["strace", "nix", "eval", "1+1"])
"""
self.argv = [cmd, *args, *self.argv]
if self.exe:
self.exe = None if isinstance(cmd, str) else cmd
return self
def run(self) -> CommandResult:
"""
Runs the configured command
@@ -79,3 +79,10 @@ def test_command_exec_fails_on_other_bad_exit_code(
out_msg, err_msg = msgs
assert out_msg == "stdout: drgn fops\n"
assert err_msg == "stderr: "
def test_command_wrapping(command: Callable[[list[str]], Command]):
cmd = command(["echo $0 -- $@", "wrapper", "hello", "world"]).with_wrapper("bash", "-c")
res = cmd.run().ok()
assert res.stdout_plain == "wrapper -- hello world"