Skip to content

Commit a73ddd1

Browse files
authored
Remove -vv by default for pytest (#595)
Fixes #594.
2 parents c0410d1 + 2c99cc2 commit a73ddd1

14 files changed

Lines changed: 74 additions & 13 deletions

File tree

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ But you might still need to adapt your code:
2626

2727
### Cookiecutter template
2828

29-
<!-- Here new features for cookiecutter specifically -->
29+
- Generated `pyproject.toml` no longer sets `addopts = "-vv"` under `[tool.pytest.ini_options]` as this is too verbose for a default.
3030

3131
## Bug Fixes
3232

cookiecutter/migrate.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import hashlib
2727
import json
2828
import os
29+
import re
2930
import subprocess
3031
import sys
3132
import tempfile
@@ -39,6 +40,9 @@ def main() -> None:
3940
"""Run the migration steps."""
4041
# Add a separation line like this one after each migration step.
4142
print("=" * 72)
43+
print("Removing default `-vv` from pytest addopts...")
44+
migrate_pytest_addopts_default()
45+
print("=" * 72)
4246
print()
4347

4448
if _manual_steps:
@@ -308,6 +312,75 @@ def read_cookiecutter_str_var(name: str) -> str | None:
308312
return value
309313

310314

315+
def migrate_pytest_addopts_default() -> None:
316+
"""Remove the default ``-vv`` from pytest addopts in ``pyproject.toml``.
317+
318+
Earlier versions of the template set ``addopts = "-vv"`` under
319+
``[tool.pytest.ini_options]``. For projects with many tests this
320+
default produces a wall of output that makes it hard to see the
321+
results from other, previous, sessions. The template no longer ships
322+
this default; this step removes the matching line from existing
323+
projects.
324+
325+
The function is a no-op when ``pyproject.toml`` does not exist, when
326+
the ``[tool.pytest.ini_options]`` section is missing, or when the
327+
section has no ``addopts`` line (already migrated). A manual step is
328+
emitted when ``addopts`` is present but no longer matches the
329+
template default, so the maintainer can decide whether to drop
330+
``-vv`` from the customized value.
331+
"""
332+
pyproject = Path("pyproject.toml")
333+
if not pyproject.exists():
334+
print(f" Skipped {pyproject}: file not found")
335+
return
336+
337+
try:
338+
content = pyproject.read_text(encoding="utf-8")
339+
except OSError as exc:
340+
manual_step(
341+
f"Failed to read {pyproject}: {exc}. Please remove "
342+
'`addopts = "-vv"` from `[tool.pytest.ini_options]` manually.'
343+
)
344+
return
345+
346+
pytest_section_match = re.search(
347+
r"(?ms)^\[tool\.pytest\.ini_options\]\n.*?(?=^\[|\Z)",
348+
content,
349+
)
350+
if pytest_section_match is None:
351+
print(f" Skipped {pyproject}: no [tool.pytest.ini_options] section")
352+
return
353+
354+
pytest_section = pytest_section_match.group(0)
355+
addopts_match = re.search(r"^addopts\s*=.*$", pytest_section, flags=re.MULTILINE)
356+
if addopts_match is None:
357+
print(f" Skipped {pyproject}: no addopts in [tool.pytest.ini_options]")
358+
return
359+
360+
addopts_line = addopts_match.group(0)
361+
if addopts_line != 'addopts = "-vv"':
362+
manual_step(
363+
f"{pyproject} has a customized `{addopts_line}` line under "
364+
"[tool.pytest.ini_options]; please drop `-vv` from it manually "
365+
"if appropriate."
366+
)
367+
return
368+
369+
new_content = content.replace(f"{addopts_line}\n", "", 1)
370+
371+
try:
372+
replace_file_atomically(pyproject, new_content)
373+
print(
374+
f" Updated {pyproject}: removed default `-vv` from "
375+
"[tool.pytest.ini_options]"
376+
)
377+
except OSError as exc:
378+
manual_step(
379+
f"Failed to update {pyproject}: {exc}. Please remove "
380+
'`addopts = "-vv"` from `[tool.pytest.ini_options]` manually.'
381+
)
382+
383+
311384
def manual_step(message: str) -> None:
312385
"""Print a manual step message in yellow."""
313386
_manual_steps.append(message)

cookiecutter/{{cookiecutter.github_repo_name}}/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ disable = [
212212
]
213213

214214
[tool.pytest.ini_options]
215-
addopts = "-vv"
216215
filterwarnings = [
217216
"error",
218217
"once::DeprecationWarning",

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ module = [
212212
ignore_missing_imports = true
213213

214214
[tool.pytest.ini_options]
215-
addopts = "-vv"
216215
filterwarnings = [
217216
"error",
218217
"once::DeprecationWarning",

tests_golden/integration/test_cookiecutter_generation/actor-proprietary/frequenz-actor-test/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ disable = [
154154
]
155155

156156
[tool.pytest.ini_options]
157-
addopts = "-vv"
158157
filterwarnings = [
159158
"error",
160159
"once::DeprecationWarning",

tests_golden/integration/test_cookiecutter_generation/actor/frequenz-actor-test/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ disable = [
154154
]
155155

156156
[tool.pytest.ini_options]
157-
addopts = "-vv"
158157
filterwarnings = [
159158
"error",
160159
"once::DeprecationWarning",

tests_golden/integration/test_cookiecutter_generation/api-proprietary/frequenz-api-test/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ disable = [
166166
]
167167

168168
[tool.pytest.ini_options]
169-
addopts = "-vv"
170169
filterwarnings = [
171170
"error",
172171
"once::DeprecationWarning",

tests_golden/integration/test_cookiecutter_generation/api/frequenz-api-test/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ disable = [
166166
]
167167

168168
[tool.pytest.ini_options]
169-
addopts = "-vv"
170169
filterwarnings = [
171170
"error",
172171
"once::DeprecationWarning",

tests_golden/integration/test_cookiecutter_generation/app-proprietary/frequenz-app-test/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ disable = [
153153
]
154154

155155
[tool.pytest.ini_options]
156-
addopts = "-vv"
157156
filterwarnings = [
158157
"error",
159158
"once::DeprecationWarning",

tests_golden/integration/test_cookiecutter_generation/app/frequenz-app-test/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ disable = [
153153
]
154154

155155
[tool.pytest.ini_options]
156-
addopts = "-vv"
157156
filterwarnings = [
158157
"error",
159158
"once::DeprecationWarning",

0 commit comments

Comments
 (0)