From 95827f3009b2bc09d7b2e482748abc41f6ee904c Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Thu, 11 Jan 2024 18:42:14 +0100 Subject: [PATCH] FEAT: add `--color=yes` to `pytest` config (#267) --- .cspell.json | 1 + src/repoma/check_dev_files/pytest.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/.cspell.json b/.cspell.json index f93b4f6c..f26e6fdb 100644 --- a/.cspell.json +++ b/.cspell.json @@ -57,6 +57,7 @@ "ignoreWords": [ "MAINT", "PyPI", + "addopts", "argparse", "automethod", "autonumbering", diff --git a/src/repoma/check_dev_files/pytest.py b/src/repoma/check_dev_files/pytest.py index 32eb3a2e..1cf67e02 100644 --- a/src/repoma/check_dev_files/pytest.py +++ b/src/repoma/check_dev_files/pytest.py @@ -6,6 +6,7 @@ import tomlkit from ini2toml.api import Translator +from tomlkit import string from repoma.errors import PrecommitError from repoma.utilities import CONFIG_PATH @@ -21,6 +22,7 @@ def main() -> None: executor(_merge_coverage_into_pyproject) executor(_merge_pytest_into_pyproject) executor(_remove_pytest_ini) + executor(_update_settings) executor.finalize() @@ -67,3 +69,25 @@ def _remove_pytest_ini() -> None: os.remove(__PYTEST_INI_PATH) msg = f"Removed {__PYTEST_INI_PATH}" raise PrecommitError(msg) + + +def _update_settings() -> None: + pyproject = load_pyproject() + if pyproject.get("tool", {}).get("pytest", {}).get("ini_options") is None: + return + config = get_sub_table(pyproject, "tool.pytest.ini_options") + addopts: str = config.get("addopts", "") + options = {opt.strip() for opt in addopts.split()} + options = {opt for opt in options if opt and not opt.startswith("--color=")} + options.add("--color=yes") + if len(options) == 1: + expected = "".join(options) + else: + expected = string("\n" + "\n".join(sorted(options)) + "\n", multiline=True) + if "\n" in addopts and not addopts.startswith("\n"): + addopts = "\n" + addopts + if expected != addopts: + config["addopts"] = expected + write_pyproject(pyproject) + msg = f"Updated tool.pytest.ini_options.addopts under {CONFIG_PATH.pyproject}" + raise PrecommitError(msg)