Skip to content

Commit ad5614e

Browse files
authored
Merge pull request #569 from python-cmd2/command_result
No longer using stderr and self.data together to determine truthiness of a CommandResult.
2 parents 9fee610 + afd10b3 commit ad5614e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

cmd2/pyscript_bridge.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr
3333
3434
NOTE: Named tuples are immutable. So the contents are there for access, not for modification.
3535
"""
36-
def __bool__(self):
37-
"""If stderr is None and data is not None the command is considered a success"""
38-
return not self.stderr and self.data is not None
36+
def __bool__(self) -> bool:
37+
"""Returns True if the command succeeded, otherwise False"""
38+
39+
# If data has a __bool__ method, then call it to determine success of command
40+
if self.data is not None and callable(getattr(self.data, '__bool__', None)):
41+
return bool(self.data)
42+
43+
# Otherwise check if stderr was filled out
44+
else:
45+
return not self.stderr
3946

4047

4148
def _exec_cmd(cmd2_app, func: Callable, echo: bool) -> CommandResult:

tests/test_cmd2.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,8 +1576,14 @@ def do_affirmative(self, arg):
15761576
self._last_result = cmd2.CommandResult(arg, data=True)
15771577

15781578
def do_negative(self, arg):
1579+
self._last_result = cmd2.CommandResult(arg, data=False)
1580+
1581+
def do_affirmative_no_data(self, arg):
15791582
self._last_result = cmd2.CommandResult(arg)
15801583

1584+
def do_negative_no_data(self, arg):
1585+
self._last_result = cmd2.CommandResult('', arg)
1586+
15811587
@pytest.fixture
15821588
def commandresult_app():
15831589
app = CommandResultApp()
@@ -1590,11 +1596,19 @@ def test_commandresult_truthy(commandresult_app):
15901596
assert commandresult_app._last_result
15911597
assert commandresult_app._last_result == cmd2.CommandResult(arg, data=True)
15921598

1599+
run_cmd(commandresult_app, 'affirmative_no_data {}'.format(arg))
1600+
assert commandresult_app._last_result
1601+
assert commandresult_app._last_result == cmd2.CommandResult(arg)
1602+
15931603
def test_commandresult_falsy(commandresult_app):
15941604
arg = 'bar'
15951605
run_cmd(commandresult_app, 'negative {}'.format(arg))
15961606
assert not commandresult_app._last_result
1597-
assert commandresult_app._last_result == cmd2.CommandResult(arg)
1607+
assert commandresult_app._last_result == cmd2.CommandResult(arg, data=False)
1608+
1609+
run_cmd(commandresult_app, 'negative_no_data {}'.format(arg))
1610+
assert not commandresult_app._last_result
1611+
assert commandresult_app._last_result == cmd2.CommandResult('', arg)
15981612

15991613

16001614
def test_is_text_file_bad_input(base_app):

0 commit comments

Comments
 (0)