Skip to content

Commit

Permalink
Parse pyproject.toml for testing
Browse files Browse the repository at this point in the history
So far, testing has been limited to the Python modules. However, the
pyproject.toml is an important source of metadata for the project. This
commit will use toml (pytest dependency) to load pyproject.toml, and
Pydantic (FastAPI dependency) will be used for data validation. Unit
tests will verify the presence of select fields in the pyproject.toml.

It is also possible to use `importlib.metadata` to read other package
metadata, as explained in python-poetry/poetry#1036. The metadata
originate in pyproject.toml, and becomes available after Poetry
installs the root project package. The `importlib.metadata` approach
works in a virtualenv, in which the Poetry root project is installed,
but is error prone in Docker, because the root project is usually not
installed. Parsing the TOML is a more durable alternative, because it
works even if the root project is not installed.

The `importlib.metadata` version calculation in `inboard/__init__.py`
was not necessary, so it will be removed.
  • Loading branch information
br3ndonland committed Oct 18, 2020
1 parent 0bf44e3 commit ff00f0d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
- Do not allow force pushes
- Require status checks to pass before merging (commits must have previously been pushed to `develop` and passed all checks)
- **To create a release:**
- Bump the version number in `pyproject.toml` with `poetry version`, and manually in `test_version.py`, and commit the changes to `develop`.
- Bump the version number in `pyproject.toml` with `poetry version` and commit the changes to `develop`.
- Push to `develop` and verify all CI checks pass.
- Fast-forward merge to `master`, push, and verify all CI checks pass.
- Create an [annotated and signed Git tag](https://www.git-scm.com/book/en/v2/Git-Basics-Tagging)
Expand Down
16 changes: 0 additions & 16 deletions inboard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
"""
inboard
---
"""
from importlib.metadata import version


def package_version(package: str = __package__) -> str:
"""Calculate version number based on pyproject.toml"""
try:
return version(package)
except Exception:
return "Package not found."


__version__ = package_version()
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[mypy]
disallow_untyped_defs = True
files = **/*.py
plugins = pydantic.mypy
78 changes: 67 additions & 11 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,74 @@
from inboard import __version__, package_version
from importlib.metadata import metadata, version
from pathlib import Path
from typing import Any, Dict, List

current_version = "0.5.7"
import toml
from pydantic import BaseSettings


def test_package_version() -> None:
"""Test package version calculation."""
assert package_version() == current_version
def load_pyproject(pyproject_path: Path = Path("pyproject.toml")) -> Dict[str, Any]:
"""Load pyproject.toml into a dictionary, with a default dict as a fallback."""
try:
return dict(toml.load(pyproject_path))
except Exception:
return {
"tool": {"poetry": {"name": "inboard", "description": "", "version": ""}}
}


class Settings(BaseSettings):
"""Instantiate a Pydantic Settings model."""

pyproject: Dict[str, Any] = load_pyproject(pyproject_path=Path("pyproject.toml"))
name: str = str(pyproject["tool"]["poetry"]["name"])
version: str = str(pyproject["tool"]["poetry"]["version"])
description: str = str(pyproject["tool"]["poetry"]["description"])
authors: List[str] = list(pyproject["tool"]["poetry"]["authors"])
license_name: str = str(pyproject["tool"]["poetry"]["license"])
homepage: str = str(pyproject["tool"]["poetry"]["homepage"])
readme: str = str(pyproject["tool"]["poetry"]["readme"])
include: List[str] = list(pyproject["tool"]["poetry"]["include"])
keywords: List[str] = list(pyproject["tool"]["poetry"]["keywords"])
classifiers: List[str] = list(pyproject["tool"]["poetry"]["classifiers"])


settings = Settings()

def test_package_version_not_found() -> None:
"""Test package version calculation when package is not installed."""
assert package_version(package="incorrect") == "Package not found."

def test_load_pyproject() -> None:
"""Assert that pyproject.toml is successfully loaded and parsed."""
pyproject = load_pyproject(pyproject_path=Path("pyproject.toml"))
assert pyproject == settings.pyproject
assert str(pyproject["tool"]["poetry"]["name"]) == settings.name == "inboard"
assert str(pyproject["tool"]["poetry"]["version"]) == settings.version
assert str(pyproject["tool"]["poetry"]["description"]) == settings.description
assert list(pyproject["tool"]["poetry"]["authors"]) == settings.authors
assert "Brendon Smith <br3ndonland@protonmail.com>" in settings.authors
assert str(pyproject["tool"]["poetry"]["license"]) == settings.license_name == "MIT"
assert str(pyproject["tool"]["poetry"]["homepage"]) == settings.homepage
assert str(pyproject["tool"]["poetry"]["readme"]) == settings.readme
assert list(pyproject["tool"]["poetry"]["include"]) == settings.include
assert "inboard/py.typed" in settings.include
assert list(pyproject["tool"]["poetry"]["keywords"]) == settings.keywords
assert "fastapi" in settings.keywords
assert list(pyproject["tool"]["poetry"]["classifiers"]) == settings.classifiers
assert "Topic :: Internet :: WWW/HTTP :: HTTP Servers" in settings.classifiers

def test_version() -> None:
"""Test package version number."""
assert __version__ == current_version

def test_load_pyproject_error() -> None:
"""Assert that default dict is loaded when pyproject.toml is not found."""
pyproject = load_pyproject(pyproject_path=Path("pyproject.toml"))
pyproject_default = load_pyproject(pyproject_path=Path("error"))
assert pyproject != pyproject_default
assert pyproject_default == {
"tool": {"poetry": {"name": "inboard", "description": "", "version": ""}}
}


def test_package_version() -> None:
"""Assert that version number parsed from pyproject.toml matches
version number of installed package obtained by importlib.metadata.
"""
assert version("inboard") == settings.version
assert metadata("inboard")["name"] == settings.name == "inboard"
assert metadata("inboard")["summary"] == settings.description

0 comments on commit ff00f0d

Please sign in to comment.