Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DX: define addopts as array #269

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ reportUnusedVariable = true
typeCheckingMode = "strict"

[tool.pytest.ini_options]
addopts = """
--color=yes
--doctest-continue-on-failure
--doctest-modules
--durations=3
"""
addopts = [
"--color=yes",
"--doctest-continue-on-failure",
"--doctest-modules",
"--durations=3",
]
filterwarnings = [
"error",
"ignore: Importing ErrorTree directly from the jsonschema package is deprecated.*",
Expand Down
40 changes: 25 additions & 15 deletions src/repoma/check_dev_files/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING

import tomlkit
from ini2toml.api import Translator
from tomlkit import string

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH
from repoma.utilities.cfg import open_config
from repoma.utilities.executor import Executor
from repoma.utilities.pyproject import get_sub_table, load_pyproject, write_pyproject
from repoma.utilities.pyproject import (
get_sub_table,
load_pyproject,
to_toml_array,
write_pyproject,
)

if TYPE_CHECKING:
from tomlkit.items import Array

Check warning on line 23 in src/repoma/check_dev_files/pytest.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L23

Added line #L23 was not covered by tests

__PYTEST_INI_PATH = "pytest.ini"

Expand Down Expand Up @@ -76,30 +84,32 @@
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 __split_options(addopts)}
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:
existing = config.get("addopts", "")
expected = __get_expected_addopts(existing)

Check warning on line 88 in src/repoma/check_dev_files/pytest.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L87-L88

Added lines #L87 - L88 were not covered by tests
if isinstance(existing, str) or sorted(existing) != sorted(expected):
config["addopts"] = expected
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]:
def __get_expected_addopts(existing: str | Array) -> Array:
if isinstance(existing, str):
options = {opt.strip() for opt in __split_options(existing)}
else:
options = set(existing)

Check warning on line 100 in src/repoma/check_dev_files/pytest.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L100

Added line #L100 was not covered by tests
options = {opt for opt in options if opt and not opt.startswith("--color=")}
options.add("--color=yes")
return to_toml_array(sorted(options))

Check warning on line 103 in src/repoma/check_dev_files/pytest.py

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L102-L103

Added lines #L102 - L103 were not covered by tests


def __split_options(arg: 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()
elements = arg.split()
options: list[str] = []
for i in range(len(elements)):
if i > 0 and not elements[i].startswith("-"):
Expand Down
Loading