Skip to content

Commit

Permalink
Fix missing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmaxwell committed Nov 13, 2024
1 parent cf470d3 commit 8ea3517
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/djpress/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
try:
import tomllib
except ImportError:
import tomli as tomllib
import tomli as tomllib # Alias tomli as tomllib for compatibility with Python versions < 3.11

# Define the path to pyproject.toml
pyproject_path = Path(__file__).parent.parent.parent / "pyproject.toml"

# Read the version from pyproject.toml
with pyproject_path.open("rb") as f:
pyproject_data = tomllib.load(f)
__version__ = pyproject_data["project"]["version"]
def load_version() -> str:
"""Load the version from pyproject.toml."""
# Define the path to pyproject.toml
pyproject_path = Path(__file__).parent.parent.parent / "pyproject.toml"

# Read the version from pyproject.toml
with pyproject_path.open("rb") as f:
pyproject_data = tomllib.load(f)
return pyproject_data["project"]["version"]


__version__ = load_version()
49 changes: 49 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import importlib
from unittest.mock import mock_open, patch

import pytest


def test_version_loading():
mock_toml_content = """
[project]
version = "1.0.0"
"""

with patch("pathlib.Path.open", mock_open(read_data=mock_toml_content)):
with patch("tomllib.load") as mock_load:
mock_load.return_value = {"project": {"version": "1.0.0"}}

import djpress

importlib.reload(djpress) # Reload the module to apply the mock

assert djpress.__version__ == "1.0.0"


def test_version_loading_error():
with patch("pathlib.Path.open", mock_open()) as mock_file:
mock_file.side_effect = FileNotFoundError()

with pytest.raises(FileNotFoundError):
import djpress

importlib.reload(djpress) # Reload the module to apply the mock


def test_tomllib_import_error():
mock_toml_content = """
[project]
version = "1.0.0"
"""

with patch.dict("sys.modules", {"tomllib": None}):
with patch("pathlib.Path.open", mock_open(read_data=mock_toml_content)):
with patch("tomli.load") as mock_load:
mock_load.return_value = {"project": {"version": "1.0.0"}}

import djpress

importlib.reload(djpress) # Reload the module to apply the mock

assert djpress.__version__ == "1.0.0"

0 comments on commit 8ea3517

Please sign in to comment.