Skip to content

Commit

Permalink
Fix test suite (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat authored Oct 22, 2022
1 parent 73196a1 commit 59d909b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ repos:
- flake8-comprehensions==3.10
- flake8-pytest-style==1.6
- flake8-spellcheck==0.28
- flake8-unused-arguments==0.0.11
- flake8-unused-arguments==0.0.12
- flake8-noqa==1.2.9
- pep8-naming==0.13.2
11 changes: 6 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build-system]
build-backend = "hatchling.build"
requires = ["hatchling>=1.11", "hatch-vcs>=0.2"]
requires = ["hatchling>=1.11.1", "hatch-vcs>=0.2"]

[project]
name = "pytest-env"
Expand All @@ -13,7 +13,7 @@ urls.Source = "https://github.com/pytest-dev/pytest-env"
urls.Tracker = "https://github.com/pytest-dev/pytest-env/issues"
requires-python = ">=3.7"
dependencies = ["pytest>=7.1.3"]
optional-dependencies.test = ["coverage>=6.5", "pytest-mock>=3.10", "covdefaults>=2.2"]
optional-dependencies.test = ["coverage>=6.5", "pytest-mock>=3.10"]
keywords = ["pytest", "env"]
classifiers = [
"Development Status :: 5 - Production/Stable",
Expand All @@ -39,11 +39,12 @@ version.source = "vcs"
line-length = 120

[tool.coverage]
source = ["${_COVERAGE_SRC}", "${_COVERAGE_TEST}"]
run.source = ["pytest_env", "tests"]
run.dynamic_context = "test_function"
run.plugins = ["covdefaults"]
run.branch = true
run.parallel = true
report.fail_under = 100
report.fail_under = 92
report.show_missing = true
html.show_contexts = true
html.skip_covered = false
paths.source = [
Expand Down
17 changes: 7 additions & 10 deletions src/pytest_env/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@ def pytest_load_initial_conftests(
args: list[str], early_config: pytest.Config, parser: pytest.Parser # noqa: U100
) -> None:
"""Load environment variables from configuration files."""
for e in early_config.getini("env"):
part = e.partition("=")
for line in early_config.getini("env"):
part = line.partition("=")
key = part[0].strip()
value = part[2].strip()

# Replace environment variables in value. for instance:
# TEST_DIR={USER}/repo_test_dir.
# Replace environment variables in value. for instance TEST_DIR={USER}/repo_test_dir.
value = value.format(**os.environ)

# use D: as a way to designate a default value
# that will only override env variables if they
# do not exist already
dkey = key.split("D:")
# use D: as a way to designate a default value that will only override env variables if they do not exist
default_key = key.split("D:")
default_val = False

if len(dkey) == 2:
key = dkey[1]
if len(default_key) == 2:
key = default_key[1]
default_val = True

if not default_val or key not in os.environ:
Expand Down
2 changes: 1 addition & 1 deletion tests/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@


def test_works() -> None:
assert "MAGIC" not in os.environ
assert os.environ["MAGIC"] == os.environ["_PATCH"]
9 changes: 4 additions & 5 deletions tests/test_env.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from __future__ import annotations

from pathlib import Path
from shutil import copy2

import pytest

_EXAMPLE = Path(__file__).parent / "example.py"


@pytest.fixture()
def example(testdir: pytest.Testdir) -> pytest.Testdir:
src = Path(__file__).parent / "example.py"
dest = Path(str(testdir.tmpdir / "test_example.py"))
# dest.symlink_to(_EXAMPLE) # for local debugging use this
copy2(str(_EXAMPLE), str(dest))
dest.symlink_to(src)
return testdir


def test_simple(example: pytest.Testdir) -> None:
(example.tmpdir / "pytest.ini").write_text("[pytest]\nenv = MAGIC=alpha", encoding="utf-8")
example.monkeypatch.setenv("_PATCH", "alpha")
result = example.runpytest()
result.assert_outcomes(passed=1)
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ minversion = 3.21
description = run the tests with pytest
setenv =
COVERAGE_FILE = {env:COVERAGE_FILE:{toxworkdir}{/}.coverage.{envname}}
_COVERAGE_SRC = {envsitepackagesdir}{/}pytest_env
_COVERAGE_TEST = {toxinidir}{/}tests
extras =
test
commands =
Expand Down
3 changes: 1 addition & 2 deletions whitelist.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
addini
addoption
conftests
copy2
dkey
getini
hookimpl
repo
runpytest
setenv
testdir
tmpdir
tryfirst
Expand Down

0 comments on commit 59d909b

Please sign in to comment.