Skip to content

Commit

Permalink
Fix nested contexts duplicating commands
Browse files Browse the repository at this point in the history
  • Loading branch information
johntrimble committed Jul 29, 2023
1 parent 4941fe0 commit c24b7f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down

0 comments on commit c24b7f0

Please sign in to comment.