Skip to content

Commit

Permalink
feat: Provide test helpers and pytest fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed May 26, 2023
1 parent de0b183 commit 611ed58
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/griffe/agents/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ def handle_function(self, node: ast.AsyncFunctionDef | ast.FunctionDef, labels:
lineno = node.decorator_list[0].lineno
for decorator_node in node.decorator_list:
decorator_value = safe_get_value(decorator_node, self.filepath)
if decorator_value is None:
continue
overload = (
decorator_value in typing_overload
or decorator_value == "overload"
Expand Down
25 changes: 23 additions & 2 deletions tests/helpers.py → src/griffe/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
"""General helpers for tests."""
"""Test helpers and pytest fixtures.
Load fixtures in your own tests by adding `griffe.tests`
to the [`pytest_plugins`][pytest_plugins] list:
```python title="conftest.py"
pytest_plugins = ["griffe.tests"]
```
[pytest_plugins]: https://docs.pytest.org/en/7.1.x/how-to/plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file
"""

from __future__ import annotations

Expand All @@ -12,13 +22,24 @@
from typing import Iterator, Mapping, Sequence

from griffe.agents.inspector import inspect
from griffe.agents.visitor import visit
from griffe.agents.visitor import patch_ast, visit
from griffe.dataclasses import Module, Object
from griffe.loader import GriffeLoader

TMPDIR_PREFIX = "griffe_"


try:
import pytest

@pytest.fixture(scope="session", autouse=True)
def _fixture_patch_ast() -> None:
patch_ast()

except ImportError:
pass


@contextmanager
def temporary_pyfile(code: str) -> Iterator[tuple[str, Path]]:
"""Create a module.py file containing the given code in a temporary directory.
Expand Down
4 changes: 1 addition & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

from __future__ import annotations

from griffe.agents.visitor import patch_ast

patch_ast()
pytest_plugins = ["griffe.tests"]
2 changes: 1 addition & 1 deletion tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from griffe.dataclasses import Docstring, Module
from griffe.loader import GriffeLoader
from tests.helpers import module_vtree, temporary_pypackage
from griffe.tests import module_vtree, temporary_pypackage


def test_submodule_exports() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

from griffe.diff import Breakage, BreakageKind, find_breaking_changes
from tests.helpers import temporary_visited_module, temporary_visited_package
from griffe.tests import temporary_visited_module, temporary_visited_package


@pytest.mark.skipif(sys.version_info < (3, 8), reason="no positional-only parameters on Python 3.7")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from griffe.docstrings.parsers import Parser
from tests.helpers import temporary_visited_module
from griffe.tests import temporary_visited_module


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

from griffe.finder import ModuleFinder, NamespacePackage, _handle_editable_module, _handle_pth_file
from tests.helpers import temporary_pypackage
from griffe.tests import temporary_pypackage


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

from griffe.dataclasses import ParameterKind
from tests.helpers import temporary_visited_module
from griffe.tests import temporary_visited_module


def test_visit_simple_function() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from griffe.agents.inspector import inspect
from griffe.expressions import Name
from tests.helpers import temporary_inspected_module, temporary_pypackage
from griffe.tests import temporary_inspected_module, temporary_pypackage


def test_annotations_from_builtin_types() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from griffe.expressions import Name
from griffe.loader import GriffeLoader
from tests.helpers import temporary_pyfile, temporary_pypackage
from griffe.tests import temporary_pyfile, temporary_pypackage

if TYPE_CHECKING:
import pytest
Expand Down
2 changes: 1 addition & 1 deletion tests/test_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from textwrap import dedent

from griffe.loader import GriffeLoader
from tests.helpers import temporary_pypackage
from griffe.tests import temporary_pypackage


def test_dont_trigger_alias_resolution_when_merging_stubs() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from tests.helpers import module_vtree
from griffe.tests import module_vtree


def test_access_members_using_string_and_tuples() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from griffe.agents.nodes import get_value, relative_to_absolute
from griffe.expressions import Expression, Name
from tests.helpers import module_vtree, temporary_visited_module
from griffe.tests import module_vtree, temporary_visited_module


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

from griffe.loader import GriffeLoader
from tests.helpers import temporary_pypackage, temporary_visited_module
from griffe.tests import temporary_pypackage, temporary_visited_module

# @given(hs.from_node(node=libcst.Module))
# @pytest.mark.skipif(sys.version_info >= (3, 11, 0), reason="Too slow on Python 3.11?")
Expand Down

0 comments on commit 611ed58

Please sign in to comment.