Skip to content

Commit a683cfd

Browse files
committed
PR feedback
Signed-off-by: Bernát Gábor <[email protected]>
1 parent 63e5726 commit a683cfd

File tree

3 files changed

+49
-51
lines changed

3 files changed

+49
-51
lines changed

src/tox/config/loader/toml/__init__.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,23 @@ def build( # noqa: PLR0913
6565
raw: TomlTypes,
6666
args: ConfigLoadArgs,
6767
) -> _T:
68-
delay_replace = inspect.isclass(of_type) and issubclass(of_type, SetEnv)
69-
70-
def replacer(raw_: str, args_: ConfigLoadArgs) -> str:
71-
reference_replacer = Unroll(conf, self, args)
72-
try:
73-
replaced = str(reference_replacer(raw_)) # do replacements
74-
except Exception as exception:
75-
if isinstance(exception, HandledError):
76-
raise
77-
msg = f"replace failed in {args_.env_name}.{key} with {exception!r}"
78-
raise HandledError(msg) from exception
79-
return replaced
80-
8168
exploded = Unroll(conf=conf, loader=self, args=args)(raw)
82-
refactoried = self.to(exploded, of_type, factory)
83-
if delay_replace:
84-
refactoried.use_replacer(replacer, args=args) # type: ignore[attr-defined] # issubclass(to_type, SetEnv)
85-
return refactoried
69+
result = self.to(exploded, of_type, factory)
70+
if inspect.isclass(of_type) and issubclass(of_type, SetEnv):
71+
72+
def replacer(raw_: str, args_: ConfigLoadArgs) -> str:
73+
reference_replacer = Unroll(conf, self, args)
74+
try:
75+
replaced = str(reference_replacer(raw_)) # do replacements
76+
except Exception as exception:
77+
if isinstance(exception, HandledError):
78+
raise
79+
msg = f"replace failed in {args_.env_name}.{key} with {exception!r}"
80+
raise HandledError(msg) from exception
81+
return replaced
82+
83+
result.use_replacer(replacer, args=args) # type: ignore[attr-defined] # issubclass(to_type, SetEnv)
84+
return result
8685

8786
def found_keys(self) -> set[str]:
8887
return set(self.content.keys()) - self._unused_exclude

src/tox/config/set_env.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,17 @@ def __init__( # noqa: C901, PLR0912
2424
from .loader.replacer import MatchExpression, find_replace_expr # noqa: PLC0415
2525

2626
if isinstance(raw, dict):
27-
# TOML 'file' attribute is to be handled separately later
28-
self._raw = dict(raw)
29-
if "file" in raw:
27+
self._raw = raw
28+
if "file" in raw: # environment files to be handled later
3029
self._env_files.append(raw["file"])
3130
self._raw.pop("file")
32-
3331
return
34-
3532
if isinstance(raw, list):
3633
self._raw = reduce(lambda a, b: {**a, **b}, raw)
3734
return
38-
3935
for line in raw.splitlines(): # noqa: PLR1702
4036
if line.strip():
41-
# INI 'file|' attribute is to be handled separately later
42-
if line.startswith("file|"):
37+
if line.startswith("file|"): # environment files to be handled later
4338
self._env_files.append(line[len("file|") :])
4439
else:
4540
try:

tests/config/test_set_env.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ def test_set_env_bad_line() -> None:
5151
SetEnv("A", "py", "py", Path())
5252

5353

54-
_ConfType = Literal["ini", "toml"]
54+
ConfigFileFormat = Literal["ini", "toml"]
5555

5656

5757
class EvalSetEnv(Protocol):
5858
def __call__(
5959
self,
6060
config: str,
61-
conf_type: _ConfType = "ini",
61+
*,
62+
of_type: ConfigFileFormat = "ini",
6263
extra_files: dict[str, Any] | None = ...,
6364
from_cwd: Path | None = ...,
6465
) -> SetEnv: ...
@@ -68,14 +69,12 @@ def __call__(
6869
def eval_set_env(tox_project: ToxProjectCreator) -> EvalSetEnv:
6970
def func(
7071
config: str,
71-
conf_type: _ConfType = "ini",
72+
*,
73+
of_type: ConfigFileFormat = "ini",
7274
extra_files: dict[str, Any] | None = None,
7375
from_cwd: Path | None = None,
7476
) -> SetEnv:
75-
if conf_type == "ini":
76-
prj = tox_project({"tox.ini": config, **(extra_files or {})})
77-
else:
78-
prj = tox_project({"tox.toml": config, **(extra_files or {})})
77+
prj = tox_project({f"tox.{of_type}": config, **(extra_files or {})})
7978
result = prj.run("c", "-k", "set_env", "-e", "py", from_cwd=None if from_cwd is None else prj.path / from_cwd)
8079
result.assert_success()
8180
set_env: SetEnv = result.env_conf("py")["set_env"]
@@ -162,17 +161,18 @@ def test_set_env_honor_override(eval_set_env: EvalSetEnv) -> None:
162161

163162

164163
@pytest.mark.parametrize(
165-
("conf_type", "config"),
164+
("of_type", "config"),
166165
[
167-
("ini", "[testenv]\npackage=skip\nset_env=file|A{/}a.txt\nchange_dir=C"),
168-
("toml", '[env_run_base]\npackage="skip"\nset_env={file="A{/}a.txt"}\nchange_dir="C"'),
169-
# Using monkeypatched env setting as a reference
170-
("ini", "[testenv]\npackage=skip\nset_env=file|{env:myenvfile}\nchange_dir=C"),
171-
("toml", '[env_run_base]\npackage="skip"\nset_env={file="{env:myenvfile}"}\nchange_dir="C"'),
166+
pytest.param("ini", "[testenv]\npackage=skip\nset_env=file|A{/}a.txt\nchange_dir=C", id="ini"),
167+
pytest.param("toml", '[env_run_base]\npackage="skip"\nset_env={file="A{/}a.txt"}\nchange_dir="C"', id="toml"),
168+
pytest.param("ini", "[testenv]\npackage=skip\nset_env=file|{env:env_file}\nchange_dir=C", id="ini-env"),
169+
pytest.param(
170+
"toml", '[env_run_base]\npackage="skip"\nset_env={file="{env:env_file}"}\nchange_dir="C"', id="toml-env"
171+
),
172172
],
173173
)
174174
def test_set_env_environment_file(
175-
conf_type: _ConfType, config: str, eval_set_env: EvalSetEnv, monkeypatch: MonkeyPatch
175+
of_type: ConfigFileFormat, config: str, eval_set_env: EvalSetEnv, monkeypatch: MonkeyPatch
176176
) -> None:
177177
env_file = """
178178
A=1
@@ -182,11 +182,10 @@ def test_set_env_environment_file(
182182
E = "1"
183183
F =
184184
"""
185-
# Monkeypatch only used for some of the parameters
186-
monkeypatch.setenv("myenvfile", "A{/}a.txt")
185+
monkeypatch.setenv("env_file", "A{/}a.txt")
187186

188187
extra = {"A": {"a.txt": env_file}, "B": None, "C": None}
189-
set_env = eval_set_env(config, conf_type=conf_type, extra_files=extra, from_cwd=Path("B"))
188+
set_env = eval_set_env(config, of_type=of_type, extra_files=extra, from_cwd=Path("B"))
190189
content = {k: set_env.load(k) for k in set_env}
191190
assert content == {
192191
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
@@ -201,26 +200,31 @@ def test_set_env_environment_file(
201200

202201

203202
@pytest.mark.parametrize(
204-
("conf_type", "config"),
203+
("of_type", "config"),
205204
[
206-
("ini", "[testenv]\npackage=skip\nset_env=file|A{/}a.txt\n X=y\nchange_dir=C"),
207-
("toml", '[env_run_base]\npackage="skip"\nset_env={file="A{/}a.txt", X="y"}\nchange_dir="C"'),
208-
# Using monkeypatched env setting as a reference
209-
("ini", "[testenv]\npackage=skip\nset_env=file|{env:myenvfile}\n X=y\nchange_dir=C"),
210-
("toml", '[env_run_base]\npackage="skip"\nset_env={file="{env:myenvfile}", X="y"}\nchange_dir="C"'),
205+
pytest.param("ini", "[testenv]\npackage=skip\nset_env=file|A{/}a.txt\n X=y\nchange_dir=C", id="ini"),
206+
pytest.param(
207+
"toml", '[env_run_base]\npackage="skip"\nset_env={file="A{/}a.txt", X="y"}\nchange_dir="C"', id="toml"
208+
),
209+
pytest.param("ini", "[testenv]\npackage=skip\nset_env=file|{env:env_file}\n X=y\nchange_dir=C", id="ini-env"),
210+
pytest.param(
211+
"toml",
212+
'[env_run_base]\npackage="skip"\nset_env={file="{env:env_file}", X="y"}\nchange_dir="C"',
213+
id="toml-env",
214+
),
211215
],
212216
)
213217
def test_set_env_environment_file_combined_with_normal_setting(
214-
conf_type: _ConfType, config: str, eval_set_env: EvalSetEnv, monkeypatch: MonkeyPatch
218+
of_type: ConfigFileFormat, config: str, eval_set_env: EvalSetEnv, monkeypatch: MonkeyPatch
215219
) -> None:
216220
env_file = """
217221
A=1
218222
"""
219223
# Monkeypatch only used for some of the parameters
220-
monkeypatch.setenv("myenvfile", "A{/}a.txt")
224+
monkeypatch.setenv("env_file", "A{/}a.txt")
221225

222226
extra = {"A": {"a.txt": env_file}, "B": None, "C": None}
223-
set_env = eval_set_env(config, conf_type=conf_type, extra_files=extra, from_cwd=Path("B"))
227+
set_env = eval_set_env(config, of_type=of_type, extra_files=extra, from_cwd=Path("B"))
224228
content = {k: set_env.load(k) for k in set_env}
225229
assert content == {
226230
"PIP_DISABLE_PIP_VERSION_CHECK": "1",

0 commit comments

Comments
 (0)