From 7017def1853777b02706c55541d9a3cacf3cfee6 Mon Sep 17 00:00:00 2001 From: rootile Date: Mon, 27 Apr 2026 18:44:12 +0200 Subject: [PATCH] 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 --- tests/functional2/testlib/fixtures/command.py | 10 ++++++++++ tests/functional2/testlib/fixtures/test_command.py | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/tests/functional2/testlib/fixtures/command.py b/tests/functional2/testlib/fixtures/command.py index fb91c1248..9c80ef443 100644 --- a/tests/functional2/testlib/fixtures/command.py +++ b/tests/functional2/testlib/fixtures/command.py @@ -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 diff --git a/tests/functional2/testlib/fixtures/test_command.py b/tests/functional2/testlib/fixtures/test_command.py index 8087e0158..19336d830 100644 --- a/tests/functional2/testlib/fixtures/test_command.py +++ b/tests/functional2/testlib/fixtures/test_command.py @@ -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"