From 30ae1c663587b9ea155b5e5d1344efba8335b270 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Wed, 23 Apr 2025 21:24:46 +0300 Subject: [PATCH 1/5] Start adding tests --- tests/__init__.py | 1 + tests/conftest.py | 39 +++++++++++++++++++++++++++++++++ tests/traversal/__init__.py | 1 + tests/traversal/test_visitor.py | 23 +++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/traversal/__init__.py create mode 100644 tests/traversal/test_visitor.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..ffd823a --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +"""Test suite for Docstringify.""" diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..996076f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,39 @@ +"""Shared test fixtures.""" + +from collections import namedtuple +from textwrap import dedent + +import pytest + +EXAMPLE_MODULE_NAME = 'example_module' +EXAMPLE_FUNCTION_NAME = 'function' + +DocstringifyTestCase = namedtuple( + 'DocstringifyTestCase', ('file', 'total_docstrings', 'missing_docstrings') +) + + +@pytest.fixture +def function_without_args(tmp_path): + """ + Fixture for generating an example module with a function without args, type + annotations, or a docstring. + """ + source_code = dedent( + f""" + def {EXAMPLE_FUNCTION_NAME}(): + pass + """ + ) + + tmp_file = tmp_path / f'{EXAMPLE_MODULE_NAME}.py' + tmp_file.write_text(source_code) + + return DocstringifyTestCase( + file=tmp_file, + total_docstrings=2, + missing_docstrings=[ + EXAMPLE_MODULE_NAME, + f'{EXAMPLE_MODULE_NAME}.{EXAMPLE_FUNCTION_NAME}', + ], + ) diff --git a/tests/traversal/__init__.py b/tests/traversal/__init__.py new file mode 100644 index 0000000..c1a7016 --- /dev/null +++ b/tests/traversal/__init__.py @@ -0,0 +1 @@ +"""Tests for the docstringify.traversal subpackage.""" diff --git a/tests/traversal/test_visitor.py b/tests/traversal/test_visitor.py new file mode 100644 index 0000000..710e4a8 --- /dev/null +++ b/tests/traversal/test_visitor.py @@ -0,0 +1,23 @@ +"""Test the docstring.traversal.visitor module.""" + +import pytest + +from docstringify.traversal import DocstringVisitor + + +class TestDocstringVisitor: + """Test the DocstringVisitor.""" + + @pytest.mark.parametrize('file', ['function_without_args']) + def test_process_file(self, capsys, request, file): + """Test that DocstringVisitor.process_file() correctly processes files.""" + test_case = request.getfixturevalue(file) + visitor = DocstringVisitor(test_case.file) + visitor.process_file() + + assert visitor.docstrings_inspected == test_case.total_docstrings + assert len(visitor.missing_docstrings) == len(test_case.missing_docstrings) + + stderr = capsys.readouterr().err.strip().split('\n') + for missing_docstring in test_case.missing_docstrings: + assert f'{missing_docstring} is missing a docstring' in stderr From 4b09edff72e9717940983e26efb787f81383ad5b Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Wed, 23 Apr 2025 21:26:29 +0300 Subject: [PATCH 2/5] Run tests in ci workflow --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 84f9bb2..e6e18d6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,3 +65,6 @@ jobs: - name: Validate pre-commit hook run: pre-commit try-repo . docstringify + + - name: Run tests + run: pytest From 15233e913a09525b4ad9162bdc3ef340b2019208 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Wed, 23 Apr 2025 21:30:04 +0300 Subject: [PATCH 3/5] Add pytest config to pyproject.toml --- pyproject.toml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index ca24e00..c4cf8fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,6 +95,25 @@ lint.isort.known-first-party = [ "docstringify", ] +[tool.pytest.ini_options] +addopts = [ + "-ra", + "-l", + "-v", + "--tb=short", + "--import-mode=importlib", + "--strict-markers", + "--strict-config", + "--cov=docstringify", + "--cov=tests", + "--no-cov-on-fail", + "--cov-report=term-missing", +] +xfail_strict = true +testpaths = [ + "tests", +] + [tool.numpydoc_validation] checks = [ "all", # report on all checks From 7de07992cc9ad17d50d4fe1fcef765b72cebc843 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Sun, 13 Jul 2025 17:02:00 -0700 Subject: [PATCH 4/5] Reorder sections for pyproject-fmt hook --- pyproject.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4a8eacf..0409d3c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -84,6 +84,9 @@ lint.isort.known-first-party = [ "docstringify", ] +[tool.codespell] +ignore-regex = 'https://([\w/\.])+' + [tool.pytest.ini_options] addopts = [ "-ra", @@ -103,9 +106,6 @@ testpaths = [ "tests", ] -[tool.codespell] -ignore-regex = 'https://([\w/\.])+' - [tool.numpydoc_validation] checks = [ "all", # report on all checks From f2c110f224048f879aecad58a40c89687025c017 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Sun, 13 Jul 2025 17:06:10 -0700 Subject: [PATCH 5/5] Update pytest filterwarnings setting --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 0409d3c..251e464 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,6 +88,7 @@ lint.isort.known-first-party = [ ignore-regex = 'https://([\w/\.])+' [tool.pytest.ini_options] +filterwarnings = [ "error" ] addopts = [ "-ra", "-l",