From c24b7f0a90c55766eb2dbe4a8bd04c4cbdd137cd Mon Sep 17 00:00:00 2001 From: John Trimble Date: Sat, 29 Jul 2023 16:01:49 -0700 Subject: [PATCH] Fix nested contexts duplicating commands --- sh.py | 5 ++++- tests/test.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/sh.py b/sh.py index 04fe4ad4..cab0cf9c 100644 --- a/sh.py +++ b/sh.py @@ -1425,7 +1425,10 @@ def __call__(self, *args, **kwargs): pcall_args.pop("with", None) call_args.update(pcall_args) - cmd.extend(prepend.cmd) + # we do not prepend commands used as a 'with' context as they will + # be prepended to any nested commands + if not kwargs.get("_with", False): + cmd.extend(prepend.cmd) cmd.append(self._path) diff --git a/tests/test.py b/tests/test.py index 8ffac583..babaff92 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1101,6 +1101,13 @@ def test_with_context_args(self): out = whoami() self.assertEqual(out.strip(), "") + def test_with_context_nested(self): + echo_path = sh.echo._path + with sh.echo.bake("test1", _with=True): + with sh.echo.bake("test2", _with=True): + out = sh.echo("test3") + self.assertEqual(out.strip(), f"test1 {echo_path} test2 {echo_path} test3") + def test_binary_input(self): py = create_tmp_test( """