Skip to content

Commit

Permalink
feat: adds a test for single file module
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Aug 25, 2024
1 parent a705c42 commit 9810000
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 1 addition & 2 deletions litestar/utils/module_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import os.path
import sys
from importlib import import_module
from importlib.util import find_spec
Expand Down Expand Up @@ -39,7 +38,7 @@ def module_to_os_path(dotted_path: str = "app") -> Path:
except ModuleNotFoundError as e:
raise TypeError(f"Couldn't find the path for {dotted_path}") from e

path = Path(src.origin)
path = Path(str(src.origin))
return path.parent if path.is_file() else path


Expand Down
7 changes: 5 additions & 2 deletions tests/unit/test_utils/test_module_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ def test_import_string() -> None:
_ = import_string("imaginary_module_that_doesnt_exist.Config") # a random nonexistent class


def test_module_path() -> None:
def test_module_path(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
the_path = module_to_os_path("litestar.config.compression")
assert the_path.exists()

tmp_path.joinpath("simple_module.py").write_text("x = 'foo'")
monkeypatch.syspath_prepend(tmp_path)
os_path = module_to_os_path("simple_module")
assert os_path == Path(tmp_path)
with pytest.raises(TypeError):
_ = module_to_os_path("litestar.config.compression.Config")
_ = module_to_os_path("litestar.config.compression.extra.module")
Expand All @@ -35,5 +39,4 @@ def test_import_string_cached(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
tmp_path.joinpath("testmodule.py").write_text("x = 'foo'")
monkeypatch.chdir(tmp_path)
monkeypatch.syspath_prepend(tmp_path)

assert import_string("testmodule.x") == "foo"

0 comments on commit 9810000

Please sign in to comment.