Skip to content

Commit 203b0f5

Browse files
doc: development: test examples via doctest
Signed-off-by: Bastian Krause <bst@pengutronix.de>
1 parent a62c6f5 commit 203b0f5

1 file changed

Lines changed: 62 additions & 20 deletions

File tree

doc/development.rst

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ classes.
5656
First of all import attr, the protocol and the common driver class
5757
into your new driver file.
5858

59-
::
59+
.. testcode::
6060

6161
import attr
6262

@@ -68,7 +68,7 @@ driver class.
6868
Try to avoid subclassing existing other drivers, as this limits the flexibility
6969
provided by connecting drivers and resources on a given target at runtime.
7070

71-
::
71+
.. testcode::
7272

7373
import attr
7474

@@ -85,7 +85,7 @@ subclass list.
8585
Using the mixin class allows sharing common code, which would otherwise need to
8686
be added into multiple drivers.
8787

88-
::
88+
.. testcode::
8989

9090
import attr
9191

@@ -101,7 +101,12 @@ Additionally the driver needs to be registered with the :any:`target_factory`
101101
and provide a bindings dictionary, so that the :any:`Target` can resolve
102102
dependencies on other drivers or resources.
103103

104-
::
104+
.. testsetup:: example-driver1
105+
106+
from labgrid.factory import target_factory
107+
target_factory.all_classes.pop('ExampleDriver', None)
108+
109+
.. testcode:: example-driver1
105110

106111
import attr
107112

@@ -134,7 +139,12 @@ If you need to do something during instantiation, you need to add a
134139
for non-attr-classes).
135140
The minimum requirement is a call to :code:`super().__attrs_post_init__()`.
136141

137-
::
142+
.. testsetup:: example-driver2
143+
144+
from labgrid.factory import target_factory
145+
target_factory.all_classes.pop('ExampleDriver', None)
146+
147+
.. testcode:: example-driver2
138148

139149
import attr
140150

@@ -160,7 +170,7 @@ Writing a Resource
160170
To add a new resource to labgrid, we import attr into our new resource file.
161171
Additionally we need the :any:`target_factory` and the common ``Resource`` class.
162172

163-
::
173+
.. testcode::
164174

165175
import attr
166176

@@ -170,7 +180,7 @@ Additionally we need the :any:`target_factory` and the common ``Resource`` class
170180
Next we add our own resource with the :code:`Resource` parent class and
171181
register it with the :any:`target_factory`.
172182

173-
::
183+
.. testcode::
174184

175185
import attr
176186

@@ -185,7 +195,12 @@ register it with the :any:`target_factory`.
185195
All that is left now is to add attributes via :code:`attr.ib()` member
186196
variables.
187197

188-
::
198+
.. testsetup:: example-resource
199+
200+
from labgrid.factory import target_factory
201+
target_factory.all_classes.pop('ExampleResource', None)
202+
203+
.. testcode:: example-resource
189204

190205
import attr
191206

@@ -208,7 +223,7 @@ labgrid offers only basic strategies, for complex use cases a customized
208223
strategy is required.
209224
Start by creating a strategy skeleton:
210225

211-
::
226+
.. testcode::
212227

213228
import enum
214229

@@ -383,8 +398,10 @@ duplication.
383398
Example
384399
~~~~~~~
385400

401+
``teststrategy.py``:
402+
386403
.. code-block:: python
387-
:caption: teststrategy.py
404+
:name: teststrategy.py
388405
389406
from labgrid.strategy import GraphStrategy
390407
from labgrid.factory import target_factory
@@ -410,10 +427,10 @@ Example
410427
def state_linux_shell(self):
411428
pass
412429
413-
The class can also render a graph as PNG (using GraphViz):
430+
``test.yaml``:
414431

415432
.. code-block:: yaml
416-
:caption: test.yaml
433+
:name: test.yaml
417434
418435
targets:
419436
main:
@@ -424,8 +441,10 @@ The class can also render a graph as PNG (using GraphViz):
424441
imports:
425442
- teststrategy.py
426443
444+
The class can also render a graph as PNG (using GraphViz):
427445

428-
::
446+
.. doctest::
447+
:skipif: shutil.which('dot') is None
429448

430449
>>> from labgrid.environment import Environment
431450
>>> env = Environment('test.yaml')
@@ -513,21 +532,26 @@ SSHManager
513532
labgrid provides a SSHManager to allow connection reuse with control sockets.
514533
To use the SSHManager in your code, import it from :any:`labgrid.util.ssh`:
515534

516-
.. code-block:: python
535+
.. doctest::
517536

518537
>>> from labgrid.util import sshmanager
519538

520539
you can now request or remove port forwardings:
521540

522-
.. code-block:: python
541+
.. testsetup:: sshmanager
542+
543+
from labgrid.util import sshmanager
544+
sshmanager.get = Mock()
545+
546+
.. doctest:: sshmanager
523547

524548
>>> from labgrid.util import sshmanager
525549
>>> localport = sshmanager.request_forward('localhost', 'somehost', 3000)
526550
>>> sshmanager.remove_forward('localhost', 'somehost', 3000)
527551

528552
or get and put files:
529553

530-
.. code-block:: python
554+
.. doctest:: sshmanager
531555

532556
>>> from labgrid.util import sshmanager
533557
>>> sshmanager.put_file('somehost', '/path/to/local/file', '/path/to/remote/file')
@@ -551,10 +575,20 @@ Additionally it provides `get_remote_path()` to retrieve the complete file path,
551575
to easily employ it for driver implementations.
552576
To use it in conjunction with a `Resource` and a file:
553577

554-
.. code-block:: python
578+
.. testsetup:: managed-file
579+
580+
import tempfile
581+
from labgrid.resource import Resource
582+
from labgrid import Target
583+
584+
f = tempfile.NamedTemporaryFile()
585+
your_file = f.name
586+
your_resource = Resource(Target("main"), "example")
587+
588+
.. doctest:: managed-file
555589

556590
>>> from labgrid.util.managedfile import ManagedFile
557-
>>> mf = ManagedFile(<your-file>, <your-resource>)
591+
>>> mf = ManagedFile(your_file, your_resource)
558592
>>> mf.sync_to_resource()
559593
>>> path = mf.get_remote_path()
560594

@@ -579,10 +613,18 @@ used in the `SerialDriver` for proxy connections.
579613

580614
Usage:
581615

582-
.. code-block:: python
616+
.. testsetup:: proxy-manager
617+
618+
from labgrid.resource import Resource
619+
from labgrid import Target
620+
621+
your_resource = Resource(Target("main"), "example")
622+
your_resource.host = "localhost"
623+
624+
.. doctest:: proxy-manager
583625

584626
>>> from labgrid.util.proxy import proxymanager
585-
>>> proxymanager.get_host_and_port(<resource>)
627+
>>> host, port = proxymanager.get_host_and_port(your_resource)
586628

587629

588630
.. _contributing:

0 commit comments

Comments
 (0)