Skip to content

Instantly share code, notes, and snippets.

@twidi
Last active June 13, 2018 12:50
Show Gist options
  • Select an option

  • Save twidi/9ce8ed0e8f92788031daaa78a765da75 to your computer and use it in GitHub Desktop.

Select an option

Save twidi/9ce8ed0e8f92788031daaa78a765da75 to your computer and use it in GitHub Desktop.

Revisions

  1. twidi revised this gist Jul 1, 2016. 2 changed files with 14 additions and 6 deletions.
    14 changes: 10 additions & 4 deletions README
    Original file line number Diff line number Diff line change
    @@ -15,11 +15,17 @@ Blah blah blah

    from django.db import connection
    connection.creation.create_test_db(verbosity=0) # I use sqlite3 inmemory

    .. testcode::

    .. code-block:: python
    print('this python code will be tested because in `testcode`, but not displayed by github")


    .. code-block::

    print("this python code won't be tested because in `code-block`")
    print('this python code will be tested because in `code-block` which is replaced by `testcode` in our bash cript")

    .. testcode::

    print('this python code will be tested because in `testcode`")
    .. code:: python

    print("this python code won't be tested because in `code`")
    6 changes: 4 additions & 2 deletions test-readme.sh
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,10 @@ TMPDIR=.tmpdocs
    rm -rf $TMPDIR
    mkdir $TMPDIR

    # We copy in it our file to be tested
    cp README.rst $TMPDIR/
    # We copy in it our file to be tested, replacing "code-block" by "testcode" (we need to do it
    # because github don't display the "testcode" sections, but we want them to be displayed, and
    # the tests only work on "testcode" sections.
    sed -e 's/.. code-block:: python/.. testcode::/' README.rst > $TMPDIR/README.rst

    # Create the smallest configuration file possible for shpinx
    cat > $TMPDIR/conf.py <<EOF
  2. twidi renamed this gist Jun 30, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. twidi revised this gist Jun 30, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions REAME
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Real name of this file is README.rst but I don't want github to render the rst ;)

    Blah blah blah

    .. testsetup::
  4. twidi renamed this gist Jun 30, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. twidi created this gist Jun 30, 2016.
    23 changes: 23 additions & 0 deletions REAME.rst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    Blah blah blah

    .. testsetup::

    # This prepare the django environment to run all the "testcode" in this file
    # This is not visible in the rendered README

    import os
    os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'

    import django
    django.setup()

    from django.db import connection
    connection.creation.create_test_db(verbosity=0) # I use sqlite3 inmemory

    .. code-block:: python
    print("this python code won't be tested because in `code-block`")
    .. testcode::

    print('this python code will be tested because in `testcode`")
    30 changes: 30 additions & 0 deletions test-readme.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/usr/bin/env bash
    # Run the tests included in the README.rst file

    # Temporary directory to be used by sphinx
    TMPDIR=.tmpdocs

    # Will reset it before
    rm -rf $TMPDIR
    mkdir $TMPDIR

    # We copy in it our file to be tested
    cp README.rst $TMPDIR/

    # Create the smallest configuration file possible for shpinx
    cat > $TMPDIR/conf.py <<EOF
    extensions = ['sphinx.ext.doctest']
    master_doc = 'README'
    EOF

    # And we can run the tests
    sphinx-build -q -c $TMPDIR -b doctest $TMPDIR $TMPDIR $TMPDIR/README.rst

    # Save the exit status to return it at the end
    ERROR=$?

    # Clean our mess
    rm -fr $TMPDIR

    # We want to tell the world if something was wrong
    exit $ERROR
    37 changes: 37 additions & 0 deletions test_readme.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    """Test all examples from the README.rst file"""

    import os
    import subprocess

    from unittest import TestCase


    class ExamplesTestCase(TestCase):
    """Run all the example in the readme file"""

    def test_all(self):
    """The example are tested by running sphinx with the doctest plugin
    And it's done by running the ``test-readme.sh`` file, and in case of errors, the whole
    output is displayed and the test marked as failed.
    """

    test_file_dir = os.path.join(os.path.dirname(__file__), '..')
    test_file_path = os.path.join(test_file_dir, 'test-readme.sh')
    try:
    subprocess.check_output(
    [test_file_path],
    shell=True,
    stderr=subprocess.STDOUT,
    cwd=test_file_dir,
    )
    except subprocess.CalledProcessError as exception:

    try:
    output = 'Error while running examples in README.rst :\n\n%s' % \
    exception.output.decode('utf-8')
    except Exception: # pylint: disable=broad-except
    output = exception.output

    self.fail(output)