Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,10 +1391,10 @@ def bake(self, *args, **kwargs):
return fn

def __str__(self):
baked_args = " ".join(self._partial_baked_args)
if baked_args:
baked_args = " " + baked_args
return self._path + baked_args
if not self._partial_baked_args:
return self._path
baked_args = " ".join(shlex_quote(arg) for arg in self._partial_baked_args)
return f"{self._path} {baked_args}"

def __eq__(self, other):
return str(self) == str(other)
Expand Down
26 changes: 26 additions & 0 deletions tests/sh_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,12 @@ def test_multiple_args_long_option(self):
num_args = int(python(py.name, "--long-option", "one's two's three's"))
self.assertEqual(num_args, 3)

num_args = int(python(py.name, long_option="one's two's three's"))
self.assertEqual(num_args, 3)

cmd = str(python.bake(py.name, long_option="one two three"))
self.assertTrue(cmd.endswith(" '--long-option=one two three'"), cmd)

def test_short_bool_option(self):
py = create_tmp_test(
"""
Expand Down Expand Up @@ -2686,6 +2692,26 @@ def test_baked_command_can_be_printed(self):
ll = ls.bake("-l")
self.assertTrue(str(ll).endswith("/ls -l"))

def test_baked_command_can_be_printed_with_whitespace_args(self):
from sh import ls

ls_himym = ls.bake("How I Met Your Mother")
self.assertTrue(str(ls_himym).endswith("/ls 'How I Met Your Mother'"))
ls_himym = ls.bake("How I 'Met' Your Mother")
self.assertTrue(
str(ls_himym).endswith("""/ls 'How I '"'"'Met'"'"' Your Mother'""")
)
ls_himym = ls.bake('How I "Met" Your Mother')
self.assertTrue(str(ls_himym).endswith("""/ls 'How I "Met" Your Mother'"""))

def test_baked_command_can_be_printed_with_whitespace_in_options(self):
from sh import ls

cmd = ls.bake(o="one two")
self.assertTrue(str(cmd).endswith("""/ls -o 'one two'"""), str(cmd))
cmd = ls.bake(opt="one two")
self.assertTrue(str(cmd).endswith("""/ls '--opt=one two'"""), str(cmd))

# https://github.com/amoffat/sh/issues/185
def test_done_callback(self):
import time
Expand Down