Skip to content

Commit

Permalink
Extend support for additional keys (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat authored Mar 30, 2023
1 parent 209d4a6 commit 4ecfcec
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

All notable changes to this project will be documented in this file.

## 0.7.0 (2023-03-30)
## 1.0.0 (2023-03-30)

- Extend support for additional keys, support tox v4 aliases, and move pass_env/set_env just before commands.
- Use hatchling instead of setuptools.

## 0.6.0 (2023-01-05)
Expand Down
7 changes: 4 additions & 3 deletions src/tox_ini_fmt/formatter/section_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def order_sections(parser: ConfigParser, pin_toxenvs: list[str]) -> None:
def load_and_order_env_list(parser: ConfigParser, pin_toxenvs: list[str]) -> list[str]:
if not parser.has_section("tox"):
return []
if "envlist" not in parser["tox"]:
return []
result = explode_env_list(parser["tox"]["envlist"])
result: list[str] = next(
(explode_env_list(parser["tox"][i]) for i in ("envlist", "env_list") if i in parser["tox"]),
[],
)
missing = [e for e in pin_toxenvs if e not in result]
if missing:
raise RuntimeError(f"missing tox environment(s) to pin {', '.join(missing)}")
Expand Down
38 changes: 34 additions & 4 deletions src/tox_ini_fmt/formatter/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,47 @@

def format_test_env(parser: ConfigParser, name: str) -> None:
tox_section_cfg: Mapping[str, Callable[[str], str]] = {
"runner": str,
"description": str,
"passenv": to_pass_env,
"setenv": to_set_env,
"base_python": str,
"basepython": str,
"system_site_packages": to_boolean,
"sitepackages": to_boolean,
"always_copy": to_boolean,
"alwayscopy": to_boolean,
"download": to_boolean,
"package": str,
"package_env": str,
"wheel_build_env": str,
"package_tox_env_type": str,
"package_root": str,
"skip_install": to_boolean,
"use_develop": to_boolean,
"usedevelop": to_boolean,
"meta_dir": str,
"pkg_dir": str,
"pip_pre": to_boolean,
"deps": to_deps,
"extras": to_extras,
"extras": to_ordered_list,
"recreate": to_boolean,
"parallel_show_output": to_boolean,
"pass_env": to_pass_env,
"passenv": to_pass_env,
"set_env": to_set_env,
"setenv": to_set_env,
"change_dir": str,
"changedir": str,
"args_are_paths": to_boolean,
"ignore_errors": to_boolean,
"ignore_outcome": to_boolean,
"commands_pre": to_commands,
"commands": to_commands,
"commands_post": to_commands,
"allowlist_externals": to_ordered_list,
"suicide_timeout": str,
"interrupt_timeout": str,
"terminate_timeout": str,
"depends": to_ordered_list,
}
fix_and_reorder(parser, name, tox_section_cfg)

Expand Down Expand Up @@ -70,7 +100,7 @@ def fmt_list(values: list[str], substitute: list[str]) -> str:
return "\n".join([""] + substitute + values)


def to_extras(value: str) -> str:
def to_ordered_list(value: str) -> str:
"""Must be a line separated list - fix comma separated format"""
extras, substitute = collect_multi_line(value)
return fmt_list(sorted(extras), substitute)
Expand Down
18 changes: 17 additions & 1 deletion src/tox_ini_fmt/formatter/tox_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,38 @@
from functools import partial
from typing import Callable, Mapping

from .requires import requires
from .test_env import collect_multi_line, fmt_list
from .util import fix_and_reorder, to_boolean


def format_tox_section(parser: ConfigParser, pin_toxenvs: list[str]) -> None:
if not parser.has_section("tox"):
return
tox_section_cfg: Mapping[str, Callable[[str], str]] = {
"minversion": str,
"min_version": str,
"requires": to_requires,
"provision_tox_env": str,
"env_list": partial(to_list_of_env_values, pin_toxenvs),
"envlist": partial(to_list_of_env_values, pin_toxenvs),
"isolated_build": to_boolean,
"package_env": str,
"isolated_build_env": str,
"no_package": to_boolean,
"skipsdist": to_boolean,
"skip_missing_interpreters": to_boolean,
"minversion": str,
"ignore_base_python_conflict": to_boolean,
"ignore_basepython_conflict": to_boolean,
}
fix_and_reorder(parser, "tox", tox_section_cfg)


def to_requires(value: str) -> str:
raw_deps, substitute = collect_multi_line(value, line_split=None)
return fmt_list(requires(raw_deps), substitute)


def to_list_of_env_values(pin_toxenvs: list[str], payload: str) -> str:
"""
Example:
Expand Down
20 changes: 10 additions & 10 deletions tests/formatter/test_test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from tox_ini_fmt.formatter import format_tox_ini
from tox_ini_fmt.formatter.test_env import to_deps, to_extras
from tox_ini_fmt.formatter.test_env import to_deps, to_ordered_list


def test_no_tox_section(tox_ini: Path) -> None:
Expand Down Expand Up @@ -42,14 +42,6 @@ def test_format_test_env(tox_ini: Path) -> None:
"""
[testenv]
description = desc
passenv =
x
y
z
setenv =
A = B
C = D
E = F
basepython = python3.8
skip_install = false
usedevelop = true
Expand All @@ -60,6 +52,14 @@ def test_format_test_env(tox_ini: Path) -> None:
c
d
parallel_show_output = false
passenv =
x
y
z
setenv =
A = B
C = D
E = F
commands =
e
f \\
Expand All @@ -84,7 +84,7 @@ def test_format_test_env(tox_ini: Path) -> None:
],
)
def test_extras(arg: str, output: str) -> None:
result = to_extras(arg)
result = to_ordered_list(arg)
assert result == output


Expand Down
34 changes: 27 additions & 7 deletions tests/formatter/test_tox_section.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from pathlib import Path
from textwrap import dedent

import pytest

Expand Down Expand Up @@ -34,14 +35,33 @@ def test_format_envlist_generator(tmp_path: Path) -> None:


def test_tox_section_order(tox_ini: Path) -> None:
tox_ini.write_text(
"[tox]\nskip_missing_interpreters=true\nisolated_build=true\nminversion=3.14\nskipsdist=false\nenvlist=py37",
)
text = """
[tox]
skip_missing_interpreters=true
isolated_build=true
requires=
tox-magic >= 0.2
tox-abc >= 0.1
minversion=3.14
skipsdist=false
envlist=py37
"""
tox_ini.write_text(dedent(text))
outcome = format_tox_ini(tox_ini)
assert (
outcome == "[tox]\nenvlist =\n py37\nisolated_build = true\nskipsdist = false\n"
"skip_missing_interpreters = true\nminversion = 3.14\n"
)
result = """\
[tox]
minversion = 3.14
requires =
tox-abc>=0.1
tox-magic>=0.2
envlist =
py37
isolated_build = true
skipsdist = false
skip_missing_interpreters = true
"""
result = dedent(result)
assert outcome == result


@pytest.mark.parametrize(
Expand Down
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[tox]
skip_missing_interpreters = true
env_list =
fix
py311
py310
py39
py38
py37
fix
type
readme
requires = tox>=4.2
requires =
tox>=4.2

[testenv]
description = run the unit tests with pytest under {base_python}
Expand Down

0 comments on commit 4ecfcec

Please sign in to comment.