antmicro / tuttest

A simple Python utility for extracting documentation snippets from tutorials.
Apache License 2.0
13 stars 2 forks source link
documentation md parse rst testing

tuttest

Copyright (c) 2020-2022 Antmicro

Tuttest is a simple utility which lets you easily test tutorials and examples and reuse code between e.g. READMEs and other parts of your project. It provides an interface for extracting code snippets (code blocks) embedded in RST and Markdown files.

CLI usage

You can call tuttest directly by invoking the tuttest command from the console. This option might be useful for checking tutorials from i.e. a GitHub Actions script. Here is what a direct tuttest call looks like:

tuttest <file_name> [<snippet_name>] [--prefix-lines-with <prefix>] [--single-command]

Optional flags

Programmatic use

Naming snippets externally

The names argument helps in the Pythonic use of tuttest - whenever you can't or don't want to change the documentation you want to test by naming snippets inside the docs, but you still want to keep some structure, you might want to sue this feature to name snippets extracted from a doc. An example below:

names = ['first_name', 'some_other_name', 'and_yet_another'] 
s = get_snippets('path/to/file.rst', names=names)
print(s['first_name'])
# prints the snippet text of the first snippet found in the file

Of course this way you will rely on the order of the snippets, but perhaps this is not a bad thing.

Examples

This example presents how to use tuttest for extracting named and unnamed code snippets from files. Both the Markdown and RST files used in this example contain the same code snippets. Therefore, the output produced by tuttest should be the same for both cases.

.. code-block:: bash

   echo "This is the first unnamed snippet"

.. code-block:: bash
   :name: bash-tutorial

   echo "This is a named snippet"
   printf "1 + 2 = %d\n" $((1+2))

.. code-block:: bash

   echo "This is the second unnamed snippet"

echo "This is the first unnamed snippet"


<!-- name="bash-tutorial" -->

echo "This is a named snippet" printf "1 + 2 = %d\n" $((1+2))

echo "This is the second unnamed snippet"

Here are some tuttest usage examples. For clarity, these examples are run based on the above RST test document:

echo "This is a named snippet" printf "1 + 2 = %d\n" $((1+2))

echo "This is the second unnamed snippet"


* `tuttest test/test.rst bash-tutorial`:
<!-- name="test-named" -->

echo "This is a named snippet" printf "1 + 2 = %d\n" $((1+2))


* `tuttest test/test.rst unnamed2`:
<!-- name="test-unnamed2" -->

echo "This is the second unnamed snippet"


* `tuttest test/test.rst unnamed2 --prefix-lines-with "docker exec -t test bash -c"`
<!-- name="test-prefix" -->

docker exec -t test bash -c 'echo "This is the second unnamed snippet";'


* `tuttest test/test.rst bash-tutorial,unnamed2 --prefix-lines-with "docker exec -t test bash -c" --single-command`
<!-- name="single-command" -->

docker exec -t test bash -c 'echo "This is a named snippet";printf "1 + 2 = %d\n" $((1+2));echo "This is the second unnamed snippet";'


By default, `tuttest` extracts the snippets but does not execute them.
To actually execute a snippet, you can e.g. pipe the results to bash like:

this executes the snippet called bash-tutorial from test.rst in bash

tuttest test.rst bash-tutorial | bash -



## License

[Apache 2.0](LICENSE)