-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
93 lines (69 loc) · 3.16 KB
/
Copy pathtest_cli.py
File metadata and controls
93 lines (69 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""End-to-end tests for the CLI (calling main() directly, not via subprocess)."""
from __future__ import annotations
from pytest_starter_config.cli import main
def _run(argv, tmp_path, extra=()):
return main(["--target", str(tmp_path), "--yes", *extra, *argv])
def test_full_preset_writes_expected_files(tmp_path):
code = _run(["--preset", "full"], tmp_path)
assert code == 0
assert (tmp_path / "pyproject.toml").exists()
assert (tmp_path / "conftest.py").exists()
assert (tmp_path / "tests" / "test_starter.py").exists()
def test_dry_run_writes_nothing_but_prints_a_diff(tmp_path, capsys):
code = _run(["--preset", "full", "--dry-run"], tmp_path)
assert code == 0
out = capsys.readouterr().out
# A real unified diff header is printed...
assert "+++ b/pyproject.toml" in out
assert "[tool.pytest.ini_options]" in out
# ...but nothing is written to disk.
assert list(tmp_path.iterdir()) == []
def test_without_excludes_a_feature(tmp_path):
_run(["--preset", "full", "--without", "coverage"], tmp_path)
text = (tmp_path / "pyproject.toml").read_text(encoding="utf-8")
assert "--cov" not in text
assert "tool.coverage" not in text
def test_existing_file_is_not_clobbered_without_force(tmp_path):
conftest = tmp_path / "conftest.py"
conftest.write_text("# my own conftest\n", encoding="utf-8")
_run(["--preset", "standard"], tmp_path)
assert conftest.read_text(encoding="utf-8") == "# my own conftest\n"
def test_force_overwrites_existing_file(tmp_path):
conftest = tmp_path / "conftest.py"
conftest.write_text("# my own conftest\n", encoding="utf-8")
_run(["--preset", "standard", "--force"], tmp_path)
assert "tmp_workspace" in conftest.read_text(encoding="utf-8")
def test_running_twice_is_idempotent(tmp_path):
_run(["--preset", "full"], tmp_path)
before = {
p.name: p.read_text(encoding="utf-8")
for p in tmp_path.rglob("*")
if p.is_file()
}
_run(["--preset", "full"], tmp_path)
after = {
p.name: p.read_text(encoding="utf-8")
for p in tmp_path.rglob("*")
if p.is_file()
}
assert before == after
def test_ini_format_writes_pytest_ini(tmp_path):
_run(["--preset", "full", "--format", "ini"], tmp_path)
assert (tmp_path / "pytest.ini").exists()
assert (tmp_path / ".coveragerc").exists()
assert not (tmp_path / "pyproject.toml").exists()
def test_unknown_feature_returns_error_code(tmp_path):
code = _run(["--with", "bogus"], tmp_path)
assert code == 2
def test_merge_falls_back_to_ini_on_unparseable_pyproject(tmp_path, capsys):
# A pyproject.toml with NUL bytes cannot be merged; the CLI must fall back.
(tmp_path / "pyproject.toml").write_text("junk\x00junk\n", encoding="utf-8")
code = _run(["--preset", "standard"], tmp_path)
assert code == 0
assert (tmp_path / "pytest.ini").exists()
assert "falling back" in capsys.readouterr().err
def test_package_flag_sets_coverage_source(tmp_path):
_run(["--preset", "full", "--package", "acme"], tmp_path)
text = (tmp_path / "pyproject.toml").read_text(encoding="utf-8")
assert "--cov=acme" in text
assert '"acme"' in text