Skip to content

Commit e5699bc

Browse files
committed
Added more tests exercising the pyscript bridge.
1 parent ab7ac49 commit e5699bc

File tree

9 files changed

+46
-5
lines changed

9 files changed

+46
-5
lines changed

cmd2/pyscript_bridge.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def __getattr__(self, item):
3333
self._args[action.dest] = item
3434
return self
3535

36-
return super().__getatttr__(item)
36+
raise AttributeError(item)
37+
# return super().__getattr__(item)
3738

3839
def __call__(self, *args, **kwargs):
3940
"""
@@ -57,15 +58,17 @@ def __call__(self, *args, **kwargs):
5758
else:
5859
# This is a positional argument, search the positional arguments passed in.
5960
if not isinstance(action, argparse._SubParsersAction):
60-
if next_pos_index < len(args):
61+
if action.dest in kwargs:
62+
self._args[action.dest] = kwargs[action.dest]
63+
elif next_pos_index < len(args):
6164
self._args[action.dest] = args[next_pos_index]
6265
next_pos_index += 1
6366
else:
6467
has_subcommand = True
6568

6669
# Check if there are any extra arguments we don't know how to handle
6770
for kw in kwargs:
68-
if kw not in consumed_kw:
71+
if kw not in self._args: # consumed_kw:
6972
raise TypeError('{}() got an unexpected keyword argument \'{}\''.format(
7073
self.__current_subcommand_parser.prog, kw))
7174

@@ -116,6 +119,8 @@ def traverse_parser(parser):
116119

117120
traverse_parser(self._parser)
118121

122+
# print('Command: {}'.format(cmd_str[0]))
123+
119124
func(cmd_str[0])
120125
return self._cmd2_app._last_result
121126

@@ -146,7 +151,7 @@ def wrap_func(args=''):
146151
# Command does use argparse, return an object that can traverse the argparse subcommands and arguments
147152
return ArgparseFunctor(self._cmd2_app, item, parser)
148153

149-
return super().__getattr__(item)
154+
raise AttributeError(item)
150155

151156
def __call__(self, args):
152157
self._cmd2_app.onecmd_plus_hooks(args + '\n')

examples/subcommands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
1515

16+
1617
class SubcommandsExample(cmd2.Cmd):
1718
"""
1819
Example cmd2 application where we a base command which has a couple subcommands

examples/tab_autocompletion.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'Lupita Nyong\'o', 'Andy Serkis', 'Liam Neeson', 'Ewan McGregor', 'Natalie Portman',
1919
'Jake Lloyd', 'Hayden Christensen', 'Christopher Lee']
2020

21+
2122
def query_actors() -> List[str]:
2223
"""Simulating a function that queries and returns a completion values"""
2324
return actors
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app.media.movies.add('My Movie', 'PG-13', director=('George Lucas', 'J. J. Abrams'))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
app.media.movies.list()
1+
app.media.movies.list(actor='Mark Hamill')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app.media.movies.list(actor=('Mark Hamill', 'Carrie Fisher'))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app.media.movies.list(rating='PG')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app.media.movies.list(rating=('PG', 'PG-13'))

tests/test_pyscript.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ def test_pyscript_help(ps_app, capsys, request, command, pyscript_file):
100100
('media movies list', 'media_movies_list1.py'),
101101
('media movies list', 'media_movies_list2.py'),
102102
('media movies list', 'media_movies_list3.py'),
103+
('media movies list -a "Mark Hamill"', 'media_movies_list4.py'),
104+
('media movies list -a "Mark Hamill" -a "Carrie Fisher"', 'media_movies_list5.py'),
105+
('media movies list -r PG', 'media_movies_list6.py'),
106+
('media movies list -r PG PG-13', 'media_movies_list7.py'),
107+
('media movies add "My Movie" PG-13 --director "George Lucas" "J. J. Abrams"',
108+
'media_movies_add1.py'),
103109
])
104110
def test_pyscript_out(ps_app, capsys, request, command, pyscript_file):
105111
test_dir = os.path.dirname(request.module.__file__)
@@ -113,3 +119,27 @@ def test_pyscript_out(ps_app, capsys, request, command, pyscript_file):
113119
assert len(out) > 0
114120
assert out == expected
115121

122+
123+
@pytest.mark.parametrize('command', [
124+
'app.noncommand',
125+
'app.media.noncommand',
126+
])
127+
def test_pyscript_unknown_command(ps_app, capsys, command):
128+
run_cmd(ps_app, 'py {}'.format(command))
129+
_, err = capsys.readouterr()
130+
131+
assert len(err) > 0
132+
assert 'Traceback' in err
133+
assert 'AttributeError' in err
134+
135+
136+
@pytest.mark.parametrize('command', [
137+
'app.media.movies.list(artist="Invalid Keyword")',
138+
])
139+
def test_pyscript_unknown_kw(ps_app, capsys, command):
140+
run_cmd(ps_app, 'py {}'.format(command))
141+
_, err = capsys.readouterr()
142+
143+
assert len(err) > 0
144+
assert 'Traceback' in err
145+
assert 'TypeError' in err

0 commit comments

Comments
 (0)