From 913b8a9bc6675c0296ca3b6e218126aa4aaf5d96 Mon Sep 17 00:00:00 2001 From: Nicco Kunzmann Date: Mon, 10 Oct 2022 17:28:39 +0100 Subject: [PATCH] test all files with doctest This runs doctest on .py and rst files. See https://github.com/collective/icalendar/issues/443 --- src/icalendar/tests/test_with_doctest.py | 64 ++++++++++++++++++++++++ tox.ini | 1 + 2 files changed, 65 insertions(+) create mode 100644 src/icalendar/tests/test_with_doctest.py diff --git a/src/icalendar/tests/test_with_doctest.py b/src/icalendar/tests/test_with_doctest.py new file mode 100644 index 00000000..43efb13b --- /dev/null +++ b/src/icalendar/tests/test_with_doctest.py @@ -0,0 +1,64 @@ +"""This file tests the source code provided by the documentation. + +See +- doctest documentation: https://docs.python.org/3/library/doctest.html +- Issue 443: https://github.com/collective/icalendar/issues/443 + +This file should be tests, too: + + >>> print("Hello World!") + Hello World! + +""" +import doctest +import os +import pytest +import importlib + +HERE = os.path.dirname(__file__) or "." +ICALENDAR_PATH = os.path.dirname(HERE) + +PYTHON_FILES = [ + os.path.join(dirpath, filename) + for dirpath, dirnames, filenames in os.walk(ICALENDAR_PATH) + for filename in filenames if filename.lower().endswith(".py") +] + +MODULE_NAMES = [ + "icalendar" + python_file[len(ICALENDAR_PATH):-3].replace("/", ".") + for python_file in PYTHON_FILES +] + +def test_this_module_is_among_them(): + assert __name__ in MODULE_NAMES + +@pytest.mark.parametrize("module_name", MODULE_NAMES) +def test_docstring_of_python_file(module_name): + """This test runs doctest on the Python module.""" + module = importlib.import_module(module_name) + test_result = doctest.testmod(module, name=module_name) + assert test_result.failed == 0 + +DOCUMENTATION_PATH = os.path.join(HERE, "../../../") + +DOCUMENT_PATHS = [ + os.path.join(dirpath, filename) + for dirpath, dirnames, filenames in os.walk(DOCUMENTATION_PATH) + for filename in filenames if filename.lower().endswith(".rst") +] + +@pytest.mark.parametrize("filename", [ + "README.rst", + "index.rst", +]) +def test_files_is_included(filename): + assert any(path.endswith(filename) for path in DOCUMENT_PATHS) + +@pytest.mark.parametrize("document", DOCUMENT_PATHS) +def test_documentation_file(document): + """This test runs doctest on a documentation file.""" + test_result = doctest.testfile(document, module_relative=False) + assert test_result.failed == 0 + + + diff --git a/tox.ini b/tox.ini index 8d1e399a..c80a2c40 100644 --- a/tox.ini +++ b/tox.ini @@ -10,6 +10,7 @@ usedevelop=True deps = pytest coverage + hypothesis commands = coverage run --source=src/icalendar --omit=*/tests/* --module pytest [] coverage report