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

FIX: support newlines in pytest options #268

Merged
merged 1 commit into from
Jan 11, 2024
Merged
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
18 changes: 17 additions & 1 deletion src/repoma/check_dev_files/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
executor(_merge_coverage_into_pyproject)
executor(_merge_pytest_into_pyproject)
executor(_remove_pytest_ini)
executor(_update_settings)

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

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L25

Added line #L25 was not covered by tests
executor.finalize()


Expand Down Expand Up @@ -72,22 +72,38 @@


def _update_settings() -> None:
pyproject = load_pyproject()

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

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L75

Added line #L75 was not covered by tests
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", "")

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

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L77-L79

Added lines #L77 - L79 were not covered by tests
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")

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

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L82

Added line #L82 was not covered by tests
if len(options) == 1:
expected = "".join(options)

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

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L84

Added line #L84 was not covered by tests
else:
expected = string("\n" + "\n".join(sorted(options)) + "\n", multiline=True)

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

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L86

Added line #L86 was not covered by tests
if "\n" in addopts and not addopts.startswith("\n"):
addopts = "\n" + addopts

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#L88

Added line #L88 was not covered by tests
if expected != addopts:
config["addopts"] = expected
write_pyproject(pyproject)
msg = f"Updated tool.pytest.ini_options.addopts under {CONFIG_PATH.pyproject}"
raise PrecommitError(msg)

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

View check run for this annotation

Codecov / codecov/patch

src/repoma/check_dev_files/pytest.py#L90-L93

Added lines #L90 - L93 were not covered by tests


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
Loading