Skip to content

Commit 62f2cb9

Browse files
committed
add tests
1 parent 29a52f5 commit 62f2cb9

File tree

6 files changed

+124
-14
lines changed

6 files changed

+124
-14
lines changed

.pre-commit-config.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ repos:
99
hooks:
1010
- id: check-github-workflows
1111
args: ["--verbose"]
12-
# - repo: https://github.com/codespell-project/codespell
13-
# rev: v2.4.0
14-
# hooks:
15-
# - id: codespell
16-
# additional_dependencies: ["tomli>=2.0.1"]
1712
- repo: https://github.com/tox-dev/tox-ini-fmt
1813
rev: "1.5.0"
1914
hooks:

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ max_supported_python = "3.13"
114114
addopts = "-ra -q"
115115
testpaths = [
116116
"tests",
117-
"integration",
118117
]
118+
#echo_envs = ["ENV1", "ENV2"]
119+
#echo_attributes = []
120+
#echo_versions = ["pytest"]
119121

120122
[tool.coverage]
121123
run.source = [

src/pytest_echo/plugin.py

+56-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
import fnmatch
66
import importlib.metadata as meta
77
import os
8+
from dataclasses import dataclass
89
from pprint import pformat
9-
from typing import Any
10+
from typing import Any, Iterator
1011

1112
import pytest
1213

1314
UNKNOWN_TYPE = "unknown type"
1415
UNKNOWN_ATTR = "unknown attribute"
1516

1617

18+
@dataclass
19+
class Entry:
20+
"""Configuration entries."""
21+
22+
key: str
23+
type: str
24+
25+
1726
def get_installed_distributions() -> list[tuple[str, str]]:
1827
"""Return a list of installed Distribution objects."""
1928
try:
@@ -26,7 +35,6 @@ def get_attr(obj: Any, attr: str, default: str | None = "NOT FOUND") -> Any:
2635
"""Recursive get object's attribute. May use dot notation."""
2736
if "." not in attr:
2837
try:
29-
ret = default
3038
if hasattr(obj, attr):
3139
ret = getattr(obj, attr, default)
3240
elif isinstance(obj, (list, tuple)):
@@ -147,6 +155,25 @@ def pytest_report_header(config: pytest.Config) -> str | None:
147155
@pytest.hookimpl
148156
def pytest_addoption(parser: pytest.Parser) -> None:
149157
"""Add section to configuration files."""
158+
parser.addini(
159+
"echo_envs",
160+
type="linelist",
161+
default=[],
162+
help="environment to print",
163+
)
164+
parser.addini(
165+
"echo_attributes",
166+
type="linelist",
167+
default=[],
168+
help="attribute to print (full path)",
169+
)
170+
parser.addini(
171+
"echo_versions",
172+
type="linelist",
173+
default=[],
174+
help="package version to print",
175+
)
176+
150177
group = parser.getgroup("general")
151178
group.addoption(
152179
"--echo-env",
@@ -169,3 +196,30 @@ def pytest_addoption(parser: pytest.Parser) -> None:
169196
default=[],
170197
help="attribute to print (full path)",
171198
)
199+
200+
201+
@pytest.hookimpl(tryfirst=True)
202+
def pytest_load_initial_conftests(
203+
args: list[str], # noqa: ARG001
204+
early_config: pytest.Config,
205+
parser: pytest.Parser, # noqa: ARG001
206+
) -> None:
207+
"""Load environment variables from configuration files."""
208+
for entry in _load_values(early_config):
209+
if entry.type in {"env", "envs", "echo_envs"}:
210+
early_config.option.echo_envs.append(entry.key)
211+
if entry.type in ["attr", "attribute", "echo_attribute"]:
212+
early_config.option.echo_attributes.append(entry.key)
213+
if entry.type in ["version", "echo_version"]:
214+
early_config.option.echo_versions.append(entry.key)
215+
216+
217+
def _load_values(early_config: pytest.Config) -> Iterator[Entry]:
218+
has_toml_conf = False
219+
if not has_toml_conf:
220+
for var in early_config.getini("echo_envs"):
221+
yield Entry(var, "env")
222+
for var in early_config.getini("echo_attributes"):
223+
yield Entry(var, "attr")
224+
for var in early_config.getini("echo_versions"):
225+
yield Entry(var, "version")

tests/test_config.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from __future__ import annotations
2+
3+
import os
4+
from unittest import mock
5+
6+
import pytest
7+
8+
9+
@pytest.mark.parametrize(
10+
("ini", "env"),
11+
[
12+
pytest.param(
13+
"[pytest]\necho_envs = ENV1\n ENV2\n ENV3",
14+
{"ENV1": "1", "ENV2": "2", "ENV3": "3"},
15+
id="echo environment variables",
16+
),
17+
],
18+
)
19+
def test_ini_config(
20+
testdir: pytest.Testdir,
21+
ini: str,
22+
env: dict[str, str],
23+
) -> None:
24+
new_env = {
25+
**env,
26+
"PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1",
27+
"PYTEST_PLUGINS": "pytest_echo.plugin",
28+
}
29+
testdir.makeini(ini)
30+
31+
with mock.patch.dict(os.environ, new_env, clear=True):
32+
result = testdir.runpytest(testdir)
33+
result.stdout.fnmatch_lines([" ENV1: 1"])
34+
35+
36+
@pytest.mark.parametrize(
37+
("toml", "env"),
38+
[
39+
pytest.param(
40+
'[tool.pytest.ini_options]\necho_envs = ["ENV1", "ENV2", "ENV3"]',
41+
{"ENV1": "1", "ENV2": "2", "ENV3": "3"},
42+
id="echo environment variables",
43+
),
44+
],
45+
)
46+
def test_toml_config(
47+
testdir: pytest.Testdir,
48+
toml: str,
49+
env: dict[str, str],
50+
) -> None:
51+
new_env = {
52+
**env,
53+
"PYTEST_DISABLE_PLUGIN_AUTOLOAD": "1",
54+
"PYTEST_PLUGINS": "pytest_echo.plugin",
55+
}
56+
with mock.patch.dict(os.environ, new_env, clear=True):
57+
testdir.makepyfile("""def test_pass(request): pass""")
58+
testdir.makepyprojecttoml(toml)
59+
result = testdir.runpytest()
60+
result.stdout.fnmatch_lines([" ENV2: 2"])
61+
result.assert_outcomes(passed=1)

tests/test_echo.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import pytest
99

10-
import pytest_echo
11-
1210
ATTR_INT = 111
1311
ATTR_DICT = {"key": "value"}
1412
ATTR_LIST = [11, 12, 13, (21, 22)]
@@ -40,7 +38,7 @@ def test_echo_env_glob(testdir: pytest.Testdir) -> None:
4038

4139
def test_echo_version(testdir: pytest.Testdir) -> None:
4240
result = testdir.runpytest("--echo-version=pytest-echo")
43-
result.stdout.fnmatch_lines([f"*echo: {pytest_echo.__version__}"])
41+
result.stdout.fnmatch_lines(["*echo: *"])
4442

4543

4644
def test_echo_version_error(testdir: pytest.Testdir) -> None:
@@ -63,14 +61,14 @@ def test_echo_version_non_standard(testdir: pytest.Testdir, module: str) -> None
6361

6462
def test_echo_version_glob(testdir: pytest.Testdir) -> None:
6563
result = testdir.runpytest("--echo-version=pytest*")
66-
result.stdout.fnmatch_lines([f"*echo-{pytest_echo.__version__}*"])
64+
result.stdout.fnmatch_lines(["*echo-*"])
6765

6866

6967
def test_echo_all(testdir: pytest.Testdir) -> None:
7068
os.environ["PYTESTECHO"] = "123"
7169
result = testdir.runpytest("--echo-version=pytest_echo", "--echo-env=PYTESTECHO")
7270
result.stdout.fnmatch_lines([" PYTESTECHO: 123"])
73-
result.stdout.fnmatch_lines([f" pytest_echo: {pytest_echo.__version__}"])
71+
result.stdout.fnmatch_lines([" pytest_echo: *"])
7472

7573

7674
def test_echo_attr(testdir: pytest.Testdir) -> None:

uv.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)