From 762435061e682089784ac15aa9e2292e87112310 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Thu, 11 Jan 2024 19:11:46 +0100 Subject: [PATCH] FIX: support newlines in `pytest` options (#268) --- src/repoma/check_dev_files/pytest.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/repoma/check_dev_files/pytest.py b/src/repoma/check_dev_files/pytest.py index 1cf67e02..a218f609 100644 --- a/src/repoma/check_dev_files/pytest.py +++ b/src/repoma/check_dev_files/pytest.py @@ -77,7 +77,7 @@ def _update_settings() -> 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.strip() for opt in __split_options(addopts)} options = {opt for opt in options if opt and not opt.startswith("--color=")} options.add("--color=yes") if len(options) == 1: @@ -91,3 +91,19 @@ def _update_settings() -> None: write_pyproject(pyproject) msg = f"Updated tool.pytest.ini_options.addopts under {CONFIG_PATH.pyproject}" raise PrecommitError(msg) + + +def __split_options(string: str) -> list[str]: + """Split a string of options into a list of options. + + >>> __split_options('-abc def -ghi "j k l" -mno pqr') + ['-abc def', '-ghi "j k l"', '-mno pqr'] + """ + elements = string.split() + options: list[str] = [] + for i in range(len(elements)): + if i > 0 and not elements[i].startswith("-"): + options[-1] += f" {elements[i]}" + else: + options.append(elements[i]) + return options