-
Notifications
You must be signed in to change notification settings - Fork 26
/
test_cli.py
132 lines (108 loc) · 3.76 KB
/
test_cli.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from __future__ import annotations
import os
import sys
from importlib.metadata import version
from stat import S_IREAD, S_IWRITE
from typing import TYPE_CHECKING
import pytest
from pyproject_fmt.cli import cli_args
if TYPE_CHECKING:
from pathlib import Path
def test_cli_version(capsys: pytest.CaptureFixture[str]) -> None:
with pytest.raises(SystemExit) as context:
cli_args(["--version"])
assert context.value.code == 0
out, _err = capsys.readouterr()
assert out == f"pyproject-fmt ({version('pyproject-fmt')})\n"
def test_cli_invalid_version(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None:
path = tmp_path / "pyproject.toml"
path.write_text("")
with pytest.raises(SystemExit) as context:
cli_args([str(path), "--max-supported-python", "3"])
assert context.value.code == 2
out, err = capsys.readouterr()
assert not out
assert "error: argument --max-supported-python: invalid version: 3, must be e.g. 3.12\n" in err
def test_cli_invalid_version_value(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None:
path = tmp_path / "pyproject.toml"
path.write_text("")
with pytest.raises(SystemExit) as context:
cli_args([str(path), "--max-supported-python", "a.1"])
assert context.value.code == 2
out, err = capsys.readouterr()
assert not out
assert (
"error: argument --max-supported-python: invalid version: a.1 due "
'ValueError("invalid literal for int() with base 10:'
) in err
def test_cli_pyproject_toml_ok(tmp_path: Path) -> None:
path = tmp_path / "tox.ini"
path.write_text("")
result = cli_args([str(path)])
assert len(result) == 1
assert result[0]
def test_cli_inputs_ok(tmp_path: Path) -> None:
paths = []
for filename in ("tox.ini", "tox2.ini", "tox3.ini"):
path = tmp_path / filename
path.write_text("")
paths.append(path)
result = cli_args([*map(str, paths)])
assert len(result) == 3
def test_cli_pyproject_toml_not_exists(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
with pytest.raises(SystemExit) as context:
cli_args([str(tmp_path / "tox.ini")])
assert context.value.code != 0
out, err = capsys.readouterr()
assert not out
assert "argument inputs: path does not exist" in err
def test_cli_pyproject_toml_not_file(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
path = tmp_path / "temp"
os.mkfifo(path)
with pytest.raises(SystemExit) as context:
cli_args([str(path)])
assert context.value.code != 0
out, err = capsys.readouterr()
assert not out
assert "argument inputs: path is not a file" in err
@pytest.mark.parametrize(("flag", "error"), [(S_IREAD, "write"), (S_IWRITE, "read")])
@pytest.mark.skipif(
sys.platform == "win32",
reason="On Windows files cannot be read only, only folders",
)
def test_cli_pyproject_toml_permission_fail(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
flag: int,
error: str,
) -> None:
path = tmp_path / "tox.ini"
path.write_text("")
path.chmod(flag)
try:
with pytest.raises(SystemExit) as context:
cli_args([str(path)])
finally:
path.chmod(S_IWRITE | S_IREAD)
assert context.value.code != 0
out, err = capsys.readouterr()
assert not out
assert f"argument inputs: cannot {error} path" in err
def test_pyproject_toml_resolved(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.chdir(tmp_path)
path = tmp_path / "tox.ini"
path.write_text("")
result = cli_args(["tox.ini"])
assert len(result) == 1
def test_pyproject_toml_dir(tmp_path: Path) -> None:
(tmp_path / "pyproject.toml").write_text("")
cli_args([str(tmp_path)])