Skip to content

Commit

Permalink
tests: Update tests to use new temporary_inspected_package helper
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Aug 13, 2024
1 parent 3c4ba16 commit 23da375
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
19 changes: 7 additions & 12 deletions tests/test_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

from __future__ import annotations

from pathlib import Path

import pytest

from griffe import inspect, temporary_inspected_module, temporary_pypackage
from griffe import inspect, temporary_inspected_module, temporary_inspected_package, temporary_pypackage
from tests.helpers import clear_sys_modules


Expand Down Expand Up @@ -50,12 +48,10 @@ def method(self, p: StringIO):

def test_missing_dependency() -> None:
"""Assert missing dependencies are handled during dynamic imports."""
with temporary_pypackage("package", ["module.py"]) as tmp_package:
filepath = Path(tmp_package.path, "module.py")
filepath.write_text("import missing")
with pytest.raises(ImportError, match="ModuleNotFoundError: No module named 'missing'"):
inspect("package.module", filepath=filepath, import_paths=[tmp_package.tmpdir])
clear_sys_modules("package")
with pytest.raises(ImportError, match="ModuleNotFoundError: No module named 'missing'"), temporary_inspected_module(
"import missing",
):
pass


def test_inspect_properties_as_attributes() -> None:
Expand Down Expand Up @@ -98,9 +94,8 @@ def test_inspecting_parameters_with_functions_as_default_values() -> None:

def test_inspecting_package_and_module_with_same_names() -> None:
"""Package and module having same name shouldn't cause issues."""
with temporary_pypackage("package", {"package.py": "a = 0"}) as tmp_package:
inspect("package.package", filepath=tmp_package.path / "package.py", import_paths=[tmp_package.tmpdir])
clear_sys_modules("package")
with temporary_inspected_package("package", {"package.py": "a = 0"}):
pass


def test_inspecting_module_with_submodules() -> None:
Expand Down
33 changes: 19 additions & 14 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

import pytest

from griffe import ExprName, GriffeLoader, load, temporary_pyfile, temporary_pypackage, temporary_visited_package
from tests.helpers import clear_sys_modules
from griffe import (
ExprName,
GriffeLoader,
temporary_inspected_package,
temporary_pyfile,
temporary_pypackage,
temporary_visited_package,
)

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -454,16 +460,15 @@ def test_side_loading_sibling_private_module(wildcard: bool, external: bool | No

def test_forcing_inspection() -> None:
"""Load a package with forced dynamic analysis."""
with temporary_pypackage("pkg", {"__init__.py": "a = 0", "mod.py": "b = 1"}) as pkg:
static_loader = GriffeLoader(force_inspection=False, search_paths=[pkg.tmpdir])
dynamic_loader = GriffeLoader(force_inspection=True, search_paths=[pkg.tmpdir])
static_package = static_loader.load("pkg")
dynamic_package = dynamic_loader.load("pkg")
modules = {"__init__.py": "a = 0", "mod.py": "b = 1"}
with temporary_visited_package("static_pkg", modules) as static_package, temporary_inspected_package(
"dynamic_pkg",
modules,
) as dynamic_package:
for name in static_package.members:
assert name in dynamic_package.members
for name in static_package["mod"].members:
assert name in dynamic_package["mod"].members
clear_sys_modules("pkg")


def test_relying_on_modules_path_attribute(monkeypatch: pytest.MonkeyPatch) -> None:
Expand All @@ -486,22 +491,22 @@ def test_not_calling_package_loaded_hook_on_something_else_than_package() -> Non
assert not alias.resolved


@pytest.mark.parametrize("force_inspection", [True, False])
@pytest.mark.parametrize("dynamic", [True, False])
def test_warning_on_objects_from_non_submodules_being_exported(
caplog: pytest.LogCaptureFixture,
force_inspection: bool,
dynamic: bool,
) -> None:
"""Warn when objects from non-submodules are exported."""
with temporary_pypackage(
temporary_package = temporary_inspected_package if dynamic else temporary_visited_package
with caplog.at_level(logging.DEBUG, logger="griffe"), temporary_package(
"pkg",
{
"__init__.py": "from typing import List\nfrom pkg import moda, modb\n__all__ = ['List']",
"moda.py": "class Thing: ...",
"modb.py": "from pkg.moda import Thing\n\n__all__ = ['Thing']",
},
) as pkg:
with caplog.at_level(logging.DEBUG, logger="griffe"):
load("pkg", search_paths=[pkg.tmpdir], force_inspection=force_inspection, resolve_aliases=True)
resolve_aliases=True,
):
messages = [record.message for record in caplog.records]
assert any("Name `List` exported by module" in msg for msg in messages)
assert any("Name `Thing` exported by module" in msg for msg in messages)

0 comments on commit 23da375

Please # to comment.