Skip to content

Commit 214a6dd

Browse files
nisimondjuledwar
authored andcommitted
Pretty-print the command in shell-friendly notation
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.
1 parent f4adf00 commit 214a6dd

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

README.rst

+18-9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Test command line utilities and applications by whitelisting them with app-speci
3939
>>> tester = Runner()
4040
>>> tester.call_engines['echo'] = SubprocessValidator()
4141
>>> tester.teststring(test_str)
42+
# echo 'Pining for the fjords'
4243
4344
Click applications
4445
~~~~~~~~~~~~~~~~~~
@@ -66,6 +67,7 @@ This can now be tested in docstrings:
6667
...
6768
... $ hello Polly Parrot
6869
... Usage: hello [OPTIONS] NAME
70+
... Try "hello --help" for help.
6971
... <BLANKLINE>
7072
... Error: Got unexpected extra argument (Parrot)
7173
...
@@ -83,6 +85,9 @@ Click applications can be tested with a ``ClickValidator`` engine:
8385
>>> tester.call_engines['hello'] = ClickValidator(hello)
8486
8587
>>> tester.teststring(test_str)
88+
# hello Polly
89+
# hello Polly Parrot
90+
# hello 'Polly Parrot'
8691
8792
8893
Mixed applications
@@ -120,6 +125,10 @@ Your app can be combined with other command-line utilities by adding multiple en
120125
>>> tester.call_engines['cat'] = SubprocessValidator()
121126
122127
>>> tester.teststring(test_str)
128+
# hello Polly
129+
# echo 'Pining for the fjords'
130+
# python -c "with open('tmp.txt', 'w+') as f: f.write('Pushing up daisies')"
131+
# cat tmp.txt
123132
124133
Suppressing commands
125134
~~~~~~~~~~~~~~~~~~~~
@@ -131,14 +140,15 @@ Commands can be skipped altogether with a ``SkipValidator``:
131140
>>> test_str = '''
132141
... .. code-block:: bash
133142
...
134-
... $ aws storage buckets list
143+
... $ aws storage buckets list --password $MY_PASSWORD
135144
...
136145
... '''
137146
138147
>>> from bashdoctest.validators import SkipValidator
139148
>>> tester.call_engines['aws'] = SkipValidator()
140149
141150
>>> tester.teststring(test_str)
151+
# aws storage ...
142152
143153
144154
Illegal commands
@@ -190,6 +200,7 @@ Unrecognized commands will not raise an error if +SKIP is specified
190200
...
191201
... '''
192202
>>> tester.teststring(test_str)
203+
# nmake all
193204
194205
Error handling
195206
~~~~~~~~~~~~~~
@@ -212,10 +223,9 @@ Lines failing to match the command's output will raise an error
212223
>>> tester.teststring(test_str) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
213224
Traceback (most recent call last):
214225
...
215-
ValueError: bashdoctest test failed. There, it moved!
216-
!= "No it didn't!"
217-
+ There, it moved!
218-
- "No it didn't!"
226+
ValueError: Differences (ndiff with -expected +actual):
227+
- "No it didn't!"
228+
+ There, it moved!
219229
220230
Known issues
221231
------------
@@ -245,10 +255,9 @@ All arguments to commands are passed as arguments to the first command. Therefor
245255
>>> tester.teststring(test_str) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
246256
Traceback (most recent call last):
247257
...
248-
ValueError: bashhdoctest test failed. hello > test.txt
249-
!=
250-
+ hello > test.txt
251-
-
258+
ValueError: Differences (ndiff with -expected +actual):
259+
+ hello > test.txt
260+
<BLANKLINE>
252261
253262
254263

bashdoctest/core.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,17 @@ def teststring(self, command):
9090
'''
9191

9292
for command, expected, options in self._parse_cli_statement(command):
93+
end = None
94+
pretty = '# {}'
9395
if 'password' in ' '.join(command):
9496
# Ignore anything after 1st arg so passwords are not
9597
# shown.
96-
print(">>> {} ...".format(command[0:2]))
97-
else:
98-
print(">>> {}".format(command))
98+
end = 2
99+
pretty += ' ...'
100+
101+
# Pretty-print the command in shell-friendly notation
102+
quote = lambda x: repr(x) if ' ' in x else str(x) # noqa: E731
103+
print(pretty.format(' '.join(quote(x) for x in command[0:end])))
99104

100105
if options & doctest.SKIP:
101106
continue

tests/test_validator.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def test_string_command():
4040
4141
$ hello Polly Parrot
4242
Usage: hello [OPTIONS] NAME
43+
Try "hello --help" for help.
4344
<BLANKLINE>
4445
Error: Got unexpected extra argument (Parrot)
4546

0 commit comments

Comments
 (0)