Skip to content

Commit 0e094fd

Browse files
committed
Add API docs chapter, document RandomDirectoryContext
1 parent d7f1b43 commit 0e094fd

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

cli_test_helpers/decorators.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
class ArgvContext:
1515
"""
1616
A simple context manager allowing to temporarily override ``sys.argv``.
17+
18+
Use it to mimic the command line arguments of the CLI application.
19+
Note that the first argument (index ``0``) is always the script or
20+
application name.
1721
"""
1822

1923
def __init__(self, *new_args):
@@ -57,6 +61,12 @@ def __enter__(self):
5761
class RandomDirectoryContext(TemporaryDirectory):
5862
"""
5963
Change the execution directory to a random location, temporarily.
64+
65+
Keyword arguments are optional and identical to the ones of
66+
`tempfile.TemporaryDirectory`_ of the Python standard library.
67+
68+
.. _tempfile.TemporaryDirectory:
69+
https://docs.python.org/3/library/tempfile.html#tempfile.TemporaryDirectory
6070
"""
6171

6272
def __enter__(self):

docs/api.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
API Documentation
2+
=================
3+
4+
This section describes the context managers and helper functions provided
5+
by the CLI test helpers package.
6+
7+
.. module:: cli_test_helpers
8+
9+
Context Managers
10+
----------------
11+
12+
.. autoclass:: ArgvContext
13+
14+
.. autoclass:: EnvironContext
15+
16+
.. autoclass:: RandomDirectoryContext
17+
18+
Utilities
19+
---------
20+
21+
.. autofunction:: shell

docs/conf.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
# add these directories to sys.path here. If the directory is relative to the
1111
# documentation root, use os.path.abspath to make it absolute, like shown here.
1212
#
13-
# import os
14-
# import sys
15-
# sys.path.insert(0, os.path.abspath('.'))
13+
import os
14+
import sys
15+
sys.path.insert(0, os.path.abspath('..'))
1616

1717

1818
# -- Project information -----------------------------------------------------
@@ -28,6 +28,7 @@
2828
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
2929
# ones.
3030
extensions = [
31+
'sphinx.ext.autodoc',
3132
'sphinx.ext.extlinks',
3233
'sphinx.ext.viewcode',
3334
]

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Contents
99

1010
installation
1111
tutorial
12+
api
1213
other
1314
techniques
1415
contributing

docs/tutorial.rst

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ Start with a simple set of functional tests:
3636
installed)
3737
- Is command XYZ available? etc. Cover your entire CLI usage here!
3838

39-
This is almost a stupid exercise: Run the command as a shell command
40-
and inspect the exit code of the exiting process, e.g.
39+
This is almost a stupid exercise: Run the command as a :func:`~cli_test_helpers.shell`
40+
command and inspect the exit code of the exiting process, e.g.
4141

4242
.. code-block:: python
4343
@@ -67,7 +67,8 @@ Then you're ready to take advantage of our helpers.
6767
``ArgvContext``
6868
+++++++++++++++
6969

70-
``ArgvContext`` allows you to mimic the use of specific CLI arguments:
70+
:class:`~cli_test_helpers.ArgvContext` allows you to mimic the use of
71+
specific CLI arguments:
7172

7273
.. code-block:: python
7374
@@ -96,8 +97,8 @@ See more |example code (argparse-cli)|_.
9697
``EnvironContext``
9798
++++++++++++++++++
9899

99-
``EnvironContext`` allows you to mimic the presence (or absence) of
100-
environment variables:
100+
:class:`~cli_test_helpers.EnvironContext` allows you to mimic the presence
101+
(or absence) of environment variables:
101102

102103
.. code-block:: python
103104
@@ -112,6 +113,20 @@ environment variables:
112113
113114
See more |example code (click-command)|_.
114115

116+
``RandomDirectoryContext``
117+
++++++++++++++++++++++++++
118+
119+
:class:`~cli_test_helpers.RandomDirectoryContext` allows you to verify that
120+
your CLI program logic is independent of where it is executed in the file
121+
system:
122+
123+
.. code-block:: python
124+
125+
def test_load_configfile():
126+
"""Must not fail when executed anywhere in the filesystem."""
127+
with ArgvContext('foobar', 'load'), RandomDirectoryContext():
128+
foobar.cli.main()
129+
115130
116131
.. |example code (argparse-cli)| replace:: example code
117132
.. |example code (click-cli)| replace:: example code

0 commit comments

Comments
 (0)