|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import sys |
3 | 4 | from typing import TYPE_CHECKING
|
4 | 5 |
|
5 | 6 | import pytest
|
6 | 7 |
|
7 | 8 | from tox.config.cli.parse import get_options
|
8 |
| -from tox.session.env_select import CliEnv, EnvSelector |
| 9 | +from tox.session.env_select import _DYNAMIC_ENV_FACTORS, CliEnv, EnvSelector |
9 | 10 | from tox.session.state import State
|
10 | 11 |
|
11 | 12 | if TYPE_CHECKING:
|
@@ -150,17 +151,98 @@ def test_cli_env_can_be_specified_in_additional_environments(tox_project: ToxPro
|
150 | 151 | assert not outcome.err
|
151 | 152 |
|
152 | 153 |
|
153 |
| -def test_cli_env_not_in_tox_config_fails(tox_project: ToxProjectCreator) -> None: |
154 |
| - proj = tox_project({"tox.ini": ""}) |
155 |
| - outcome = proj.run("r", "-e", "does_not_exist") |
156 |
| - outcome.assert_failed(code=-2) |
157 |
| - assert "provided environments not found in configuration file: ['does_not_exist']" in outcome.out, outcome.out |
158 |
| - |
159 |
| - |
160 | 154 | @pytest.mark.parametrize("env_name", ["py", "py310", ".pkg"])
|
161 | 155 | def test_allowed_implicit_cli_envs(env_name: str, tox_project: ToxProjectCreator) -> None:
|
162 | 156 | proj = tox_project({"tox.ini": ""})
|
163 | 157 | outcome = proj.run("r", "-e", env_name)
|
164 | 158 | outcome.assert_success()
|
165 | 159 | assert env_name in outcome.out
|
166 | 160 | assert not outcome.err
|
| 161 | + |
| 162 | + |
| 163 | +@pytest.mark.parametrize("env_name", ["a", "b", "a-b", "b-a"]) |
| 164 | +def test_matches_hyphenated_env(env_name: str, tox_project: ToxProjectCreator) -> None: |
| 165 | + tox_ini = """ |
| 166 | + [tox] |
| 167 | + env_list=a-b |
| 168 | + [testenv] |
| 169 | + package=skip |
| 170 | + commands_pre = |
| 171 | + a: python -c 'print("a")' |
| 172 | + b: python -c 'print("b")' |
| 173 | + commands=python -c 'print("ok")' |
| 174 | + """ |
| 175 | + proj = tox_project({"tox.ini": tox_ini}) |
| 176 | + outcome = proj.run("r", "-e", env_name) |
| 177 | + outcome.assert_success() |
| 178 | + assert env_name in outcome.out |
| 179 | + assert not outcome.err |
| 180 | + |
| 181 | + |
| 182 | +_MINOR = sys.version_info.minor |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.parametrize( |
| 186 | + "env_name", |
| 187 | + [f"3.{_MINOR}", f"3.{_MINOR}-cov", "3-cov", "3", f"3.{_MINOR}", f"py3{_MINOR}-cov", f"py3.{_MINOR}-cov"], |
| 188 | +) |
| 189 | +def test_matches_combined_env(env_name: str, tox_project: ToxProjectCreator) -> None: |
| 190 | + tox_ini = """ |
| 191 | + [testenv] |
| 192 | + package=skip |
| 193 | + commands = |
| 194 | + !cov: python -c 'print("without cov")' |
| 195 | + cov: python -c 'print("with cov")' |
| 196 | + """ |
| 197 | + proj = tox_project({"tox.ini": tox_ini}) |
| 198 | + outcome = proj.run("r", "-e", env_name) |
| 199 | + outcome.assert_success() |
| 200 | + assert env_name in outcome.out |
| 201 | + assert not outcome.err |
| 202 | + |
| 203 | + |
| 204 | +@pytest.mark.parametrize( |
| 205 | + "env", |
| 206 | + [ |
| 207 | + "py", |
| 208 | + "pypy", |
| 209 | + "pypy3", |
| 210 | + "pypy3.12", |
| 211 | + "pypy312", |
| 212 | + "py3", |
| 213 | + "py3.12", |
| 214 | + "py312", |
| 215 | + "3", |
| 216 | + "3.12", |
| 217 | + "3.12.0", |
| 218 | + ], |
| 219 | +) |
| 220 | +def test_dynamic_env_factors_match(env: str) -> None: |
| 221 | + assert _DYNAMIC_ENV_FACTORS.fullmatch(env) |
| 222 | + |
| 223 | + |
| 224 | +@pytest.mark.parametrize( |
| 225 | + "env", |
| 226 | + [ |
| 227 | + "cy3", |
| 228 | + "cov", |
| 229 | + "py10.1", |
| 230 | + ], |
| 231 | +) |
| 232 | +def test_dynamic_env_factors_not_match(env: str) -> None: |
| 233 | + assert not _DYNAMIC_ENV_FACTORS.fullmatch(env) |
| 234 | + |
| 235 | + |
| 236 | +def test_suggest_env(tox_project: ToxProjectCreator) -> None: |
| 237 | + tox_ini = f"[testenv:release]\n[testenv:py3{_MINOR}]\n[testenv:alpha-py3{_MINOR}]\n" |
| 238 | + proj = tox_project({"tox.ini": tox_ini}) |
| 239 | + outcome = proj.run("r", "-e", f"releas,p3{_MINOR},magic,alph-p{_MINOR}") |
| 240 | + outcome.assert_failed(code=-2) |
| 241 | + |
| 242 | + assert not outcome.err |
| 243 | + msg = ( |
| 244 | + "ROOT: HandledError| provided environments not found in configuration file:\n" |
| 245 | + f"releas - did you mean release?\np3{_MINOR} - did you mean py3{_MINOR}?\nmagic\n" |
| 246 | + f"alph-p{_MINOR} - did you mean alpha-py3{_MINOR}?\n" |
| 247 | + ) |
| 248 | + assert outcome.out == msg |
0 commit comments