/
test_factor.py
229 lines (199 loc) · 6.3 KB
/
test_factor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from __future__ import annotations
from configparser import ConfigParser
from textwrap import dedent
from typing import Callable, List
import pytest
from tests.conftest import ToxIniCreator
from tox.config.loader.ini import IniLoader
from tox.config.loader.ini.factor import filter_for_env, find_envs
from tox.config.main import Config
from tox.config.source.ini_section import IniSection
def test_factor_env_discover_empty() -> None:
result = list(find_envs("\n\n"))
assert result == []
@pytest.fixture(scope="session")
def complex_example() -> str:
return dedent(
"""
default
lines
py: py only
!py: not py
{py,!pi}-{a,b}{,-dev},c: complex
py, d: space
extra: extra
more-default
no:space
trailingcolon:
tab:\ttab
""",
)
def test_factor_env_discover(complex_example: str) -> None:
result = list(find_envs(complex_example))
assert result == [
"py",
"py-a",
"py-a-dev",
"py-b",
"py-b-dev",
"pi-a",
"pi-a-dev",
"pi-b",
"pi-b-dev",
"c",
"d",
"extra",
"trailingcolon",
"tab",
]
@pytest.mark.parametrize(
"env",
[
"py",
"py-a",
"py-a-dev",
"py-b",
"py-b-dev",
"pi-a",
"pi-a-dev",
"pi-b",
"pi-b-dev",
"c",
"extra",
"trailingcolon",
"tab",
],
)
def test_factor_env_filter(env: str, complex_example: str) -> None:
result = filter_for_env(complex_example, name=env)
assert "default" in result
assert "lines" in result
assert "more-default" in result
assert "no:space" in result
if "py" in env:
assert "py only" in result
assert "not py" not in result
else:
assert "py only" not in result
assert "not py" in result
if "extra" == env:
assert "extra" in result
else:
assert "extra" not in result
if env in {"py-a", "py-a-dev", "py-b", "py-b-dev", "c"}:
assert "complex" in result
else:
assert "complex" not in result
def test_factor_env_list(tox_ini_conf: ToxIniCreator) -> None:
config = tox_ini_conf("[tox]\nenv_list = {py27,py36}-django{ 15, 16 }{,-dev}, docs, flake")
result = list(config)
assert result == [
"py27-django15",
"py27-django15-dev",
"py27-django16",
"py27-django16-dev",
"py36-django15",
"py36-django15-dev",
"py36-django16",
"py36-django16-dev",
"docs",
"flake",
]
def test_simple_env_list(tox_ini_conf: ToxIniCreator) -> None:
config = tox_ini_conf("[tox]\nenv_list = docs, flake8")
assert list(config) == ["docs", "flake8"]
def test_factor_config(tox_ini_conf: ToxIniCreator) -> None:
config = tox_ini_conf(
"""
[tox]
env_list = {py36,py37}-{django15,django16}
[testenv]
deps-x =
pytest
django15: Django>=1.5,<1.6
django16: Django>=1.6,<1.7
py36: unittest2
!py37,!django16: negation-or
!py37-!django16: negation-and
""",
)
assert list(config) == ["py36-django15", "py36-django16", "py37-django15", "py37-django16"]
for env in config.core["env_list"]:
env_config = config.get_env(env)
env_config.add_config(keys="deps-x", of_type=List[str], default=[], desc="deps")
deps = env_config["deps-x"]
assert "pytest" in deps
if "py36" in env:
assert "unittest2" in deps
assert "negation-or" in deps
if "django15" in env:
assert "Django>=1.5,<1.6" in deps
assert "negation-or" in deps
if "django16" in env:
assert "Django>=1.6,<1.7" in deps
if "py36-django15" == env_config.name:
assert "negation-and" in deps
def test_factor_config_do_not_replace_unescaped_comma(tox_ini_conf: ToxIniCreator) -> None:
config = tox_ini_conf("[tox]\nenv_list = py37-{base,i18n},b")
assert list(config) == ["py37-base", "py37-i18n", "b"]
def test_factor_config_no_env_list_creates_env(tox_ini_conf: ToxIniCreator) -> None:
"""If we have a factor that is not specified within the core env-list then that's also an environment"""
config = tox_ini_conf(
"""
[tox]
env_list = py37-{django15,django16}
[testenv]
deps =
pytest
django15: Django>=1.5,<1.6
django16: Django>=1.6,<1.7
py36: unittest2
""",
)
assert list(config) == ["py37-django15", "py37-django16", "py36"]
@pytest.mark.parametrize(
("env", "result"),
[
("py35", "python -m coverage html -d cov"),
("py36", "python -m coverage html -d cov\n--show-contexts"),
],
)
def test_ini_loader_raw_with_factors(
mk_ini_conf: Callable[[str], ConfigParser],
env: str,
result: str,
empty_config: Config,
) -> None:
commands = "python -m coverage html -d cov \n !py35: --show-contexts"
loader = IniLoader(
section=IniSection(None, "testenv"),
parser=mk_ini_conf(f"[tox]\nenvlist=py35,py36\n[testenv]\ncommands={commands}"),
overrides=[],
core_section=IniSection(None, "tox"),
)
outcome = loader.load_raw(key="commands", conf=empty_config, env_name=env)
assert outcome == result
def test_generative_section_name(tox_ini_conf: ToxIniCreator) -> None:
config = tox_ini_conf(
"""
[testenv:{py311,py310}-{black,lint}]
deps-x =
black: black
lint: flake8
""",
)
assert list(config) == ["py311-black", "py311-lint", "py310-black", "py310-lint"]
env_config = config.get_env("py311-black")
env_config.add_config(keys="deps-x", of_type=List[str], default=[], desc="deps")
deps = env_config["deps-x"]
assert deps == ["black"]
env_config = config.get_env("py311-lint")
env_config.add_config(keys="deps-x", of_type=List[str], default=[], desc="deps")
deps = env_config["deps-x"]
assert deps == ["flake8"]
def test_multiple_factor_match(tox_ini_conf: ToxIniCreator) -> None:
config = tox_ini_conf("[testenv]\nconf = a{,-b}: x")
env_config = config.get_env("a-b")
env_config.add_config(keys="conf", of_type=str, default="", desc="conf")
deps = env_config["conf"]
assert deps == "x"