Skip to content

Commit

Permalink
Pretty-print the command in shell-friendly notation
Browse files Browse the repository at this point in the history
When tests fail, it's highly useful to be able to copy+paste the
command that was run.

Get the unit tests passing again, while I'm in there.
  • Loading branch information
nisimond authored and juledwar committed Mar 2, 2022
1 parent f4adf00 commit 214a6dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
27 changes: 18 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Test command line utilities and applications by whitelisting them with app-speci
>>> tester = Runner()
>>> tester.call_engines['echo'] = SubprocessValidator()
>>> tester.teststring(test_str)
# echo 'Pining for the fjords'
Click applications
~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -66,6 +67,7 @@ This can now be tested in docstrings:
...
... $ hello Polly Parrot
... Usage: hello [OPTIONS] NAME
... Try "hello --help" for help.
... <BLANKLINE>
... Error: Got unexpected extra argument (Parrot)
...
Expand All @@ -83,6 +85,9 @@ Click applications can be tested with a ``ClickValidator`` engine:
>>> tester.call_engines['hello'] = ClickValidator(hello)
>>> tester.teststring(test_str)
# hello Polly
# hello Polly Parrot
# hello 'Polly Parrot'
Mixed applications
Expand Down Expand Up @@ -120,6 +125,10 @@ Your app can be combined with other command-line utilities by adding multiple en
>>> tester.call_engines['cat'] = SubprocessValidator()
>>> tester.teststring(test_str)
# hello Polly
# echo 'Pining for the fjords'
# python -c "with open('tmp.txt', 'w+') as f: f.write('Pushing up daisies')"
# cat tmp.txt
Suppressing commands
~~~~~~~~~~~~~~~~~~~~
Expand All @@ -131,14 +140,15 @@ Commands can be skipped altogether with a ``SkipValidator``:
>>> test_str = '''
... .. code-block:: bash
...
... $ aws storage buckets list
... $ aws storage buckets list --password $MY_PASSWORD
...
... '''
>>> from bashdoctest.validators import SkipValidator
>>> tester.call_engines['aws'] = SkipValidator()
>>> tester.teststring(test_str)
# aws storage ...
Illegal commands
Expand Down Expand Up @@ -190,6 +200,7 @@ Unrecognized commands will not raise an error if +SKIP is specified
...
... '''
>>> tester.teststring(test_str)
# nmake all
Error handling
~~~~~~~~~~~~~~
Expand All @@ -212,10 +223,9 @@ Lines failing to match the command's output will raise an error
>>> tester.teststring(test_str) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
ValueError: bashdoctest test failed. There, it moved!
!= "No it didn't!"
+ There, it moved!
- "No it didn't!"
ValueError: Differences (ndiff with -expected +actual):
- "No it didn't!"
+ There, it moved!
Known issues
------------
Expand Down Expand Up @@ -245,10 +255,9 @@ All arguments to commands are passed as arguments to the first command. Therefor
>>> tester.teststring(test_str) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
ValueError: bashhdoctest test failed. hello > test.txt
!=
+ hello > test.txt
-
ValueError: Differences (ndiff with -expected +actual):
+ hello > test.txt
<BLANKLINE>
Expand Down
11 changes: 8 additions & 3 deletions bashdoctest/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,17 @@ def teststring(self, command):
'''

for command, expected, options in self._parse_cli_statement(command):
end = None
pretty = '# {}'
if 'password' in ' '.join(command):
# Ignore anything after 1st arg so passwords are not
# shown.
print(">>> {} ...".format(command[0:2]))
else:
print(">>> {}".format(command))
end = 2
pretty += ' ...'

# Pretty-print the command in shell-friendly notation
quote = lambda x: repr(x) if ' ' in x else str(x) # noqa: E731
print(pretty.format(' '.join(quote(x) for x in command[0:end])))

if options & doctest.SKIP:
continue
Expand Down
1 change: 1 addition & 0 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test_string_command():
$ hello Polly Parrot
Usage: hello [OPTIONS] NAME
Try "hello --help" for help.
<BLANKLINE>
Error: Got unexpected extra argument (Parrot)
Expand Down

0 comments on commit 214a6dd

Please sign in to comment.