Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let Command know about baked call args when wrapping the module #573

Merged
merged 3 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 12 additions & 1 deletion sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -3512,7 +3512,18 @@ def __init__(self, self_module, baked_args=None):
# if we set this to None. and 3.3 needs a value for __path__
self.__path__ = []
self.__self_module = self_module
self.__env = Environment(globals(), baked_args=baked_args)

# Copy the Command class and add any baked call kwargs to it
cls_attrs = Command.__dict__.copy()
if baked_args:
call_args, _ = Command._extract_call_args(baked_args)
cls_attrs['_call_args'] = cls_attrs['_call_args'].copy()
cls_attrs['_call_args'].update(call_args)
command_cls = type(Command.__name__, Command.__bases__, cls_attrs)
globs = globals().copy()
globs[Command.__name__] = command_cls

self.__env = Environment(globs, baked_args=baked_args)

def __getattr__(self, name):
return self.__env[name]
Expand Down
7 changes: 7 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3138,6 +3138,13 @@ def test_reimport_no_interfere(self):
_sh.echo("-n", "TEST")
self.assertEqual("TEST", out.getvalue())

def test_command_with_baked_call_args(self):
# Test that sh.Command() knows about baked call args
import sh
_sh = sh(_ok_code=1)
self.assertEqual(sh.Command._call_args['ok_code'], 0)
self.assertEqual(_sh.Command._call_args['ok_code'], 1)

def test_importer_detects_module_name(self):
import sh
_sh = sh()
Expand Down