Skip to content

Commit

Permalink
Fix pytest when source coverage is not used
Browse files Browse the repository at this point in the history
The source coverage option may not be defined when running the tests
manually. The option existence should be checked before trying to get
it.

The `coverage` package dependency was also missing from the
`pyproject.toml`.
  • Loading branch information
grouigrokon committed May 14, 2024
1 parent 7f8d3a5 commit bce466b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ classifiers = [
]

dependencies = [
"coverage",
"colorama",
"pyyaml",
"python-dateutil",
Expand Down
17 changes: 12 additions & 5 deletions src/e3/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def env_protect(request: pytest.FixtureRequest) -> None:
The fixture is enabled for all tests and does the following:
* store/restore env between each tests
* store/restore env between tests
* create a temporary directory and do a cd to it before each
test. The directory is automatically removed when test ends
"""
Expand All @@ -98,9 +98,14 @@ def restore_env() -> None:


def pytest_configure(config: pytest.Config) -> None:
if config.getoption("e3") and config.getoption("cov_source"):
if (
hasattr(config.option, "e3")
and config.getoption("e3")
and hasattr(config.option, "cov_source")
and config.getoption("cov_source")
):
# When e3 plugin is activated, the report generated by pytest-cov
# should be deactivated to avoid duplicating the output. Also
# should be deactivated to avoid duplicating the output. Also,
# some options set by e3 change the coverage, pytest-cov report is not
# accurate in that case.
cov = config.pluginmanager.getplugin("_cov")
Expand All @@ -122,7 +127,9 @@ def pytest_sessionfinish(session: pytest.Session, exitstatus: int) -> None:
# test code.
session.exitstatus = 3

if session.config.getoption("cov_source"):
if hasattr(session.config.option, "cov_source") and session.config.getoption(
"cov_source"
):
cov_file = str(session.config.rootpath / ".coverage")
if session.config.getoption("e3_cov_rewrite"):
origin_dir, new_dir = session.config.getoption("e3_cov_rewrite")
Expand Down Expand Up @@ -181,7 +188,7 @@ def fix_coverage_paths(origin_dir: str, new_dir: str, cov_db: str) -> None:
.tox/py311-cov-xdist/lib/python3.11/site-packages/e3
:param new_dir: path to the dir that should be visible instead of origin_dir
e.g. src/
:param cov_db: path to the .coverage data base
:param cov_db: path to the .coverage database
"""
paths = PathAliases()
paths.add(origin_dir, new_dir)
Expand Down

0 comments on commit bce466b

Please sign in to comment.