forked from pallets/click
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_types.py
130 lines (108 loc) · 4.11 KB
/
test_types.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
import os.path
import pathlib
import pytest
from conftest import symlinks_supported
import click
@pytest.mark.parametrize(
("type", "value", "expect"),
[
(click.IntRange(0, 5), "3", 3),
(click.IntRange(5), "5", 5),
(click.IntRange(5), "100", 100),
(click.IntRange(max=5), "5", 5),
(click.IntRange(max=5), "-100", -100),
(click.IntRange(0, clamp=True), "-1", 0),
(click.IntRange(max=5, clamp=True), "6", 5),
(click.IntRange(0, min_open=True, clamp=True), "0", 1),
(click.IntRange(max=5, max_open=True, clamp=True), "5", 4),
(click.FloatRange(0.5, 1.5), "1.2", 1.2),
(click.FloatRange(0.5, min_open=True), "0.51", 0.51),
(click.FloatRange(max=1.5, max_open=True), "1.49", 1.49),
(click.FloatRange(0.5, clamp=True), "-0.0", 0.5),
(click.FloatRange(max=1.5, clamp=True), "inf", 1.5),
],
)
def test_range(type, value, expect):
assert type.convert(value, None, None) == expect
@pytest.mark.parametrize(
("type", "value", "expect"),
[
(click.IntRange(0, 5), "6", "6 is not in the range 0<=x<=5."),
(click.IntRange(5), "4", "4 is not in the range x>=5."),
(click.IntRange(max=5), "6", "6 is not in the range x<=5."),
(click.IntRange(0, 5, min_open=True), 0, "0<x<=5"),
(click.IntRange(0, 5, max_open=True), 5, "0<=x<5"),
(click.FloatRange(0.5, min_open=True), 0.5, "x>0.5"),
(click.FloatRange(max=1.5, max_open=True), 1.5, "x<1.5"),
],
)
def test_range_fail(type, value, expect):
with pytest.raises(click.BadParameter) as exc_info:
type.convert(value, None, None)
assert expect in exc_info.value.message
def test_float_range_no_clamp_open():
with pytest.raises(TypeError):
click.FloatRange(0, 1, max_open=True, clamp=True)
sneaky = click.FloatRange(0, 1, max_open=True)
sneaky.clamp = True
with pytest.raises(RuntimeError):
sneaky.convert("1.5", None, None)
@pytest.mark.parametrize(
("nargs", "multiple", "default", "expect"),
[
(2, False, None, None),
(2, False, (None, None), (None, None)),
(None, True, None, ()),
(None, True, (None, None), (None, None)),
(2, True, None, ()),
(2, True, [(None, None)], ((None, None),)),
(-1, None, None, ()),
],
)
def test_cast_multi_default(runner, nargs, multiple, default, expect):
if nargs == -1:
param = click.Argument(["a"], nargs=nargs, default=default)
else:
param = click.Option(["-a"], nargs=nargs, multiple=multiple, default=default)
cli = click.Command("cli", params=[param], callback=lambda a: a)
result = runner.invoke(cli, standalone_mode=False)
assert result.exception is None
assert result.return_value == expect
@pytest.mark.parametrize(
("cls", "expect"),
[
(None, "a/b/c.txt"),
(str, "a/b/c.txt"),
(bytes, b"a/b/c.txt"),
(pathlib.Path, pathlib.Path("a", "b", "c.txt")),
],
)
def test_path_type(runner, cls, expect):
cli = click.Command(
"cli",
params=[click.Argument(["p"], type=click.Path(path_type=cls))],
callback=lambda p: p,
)
result = runner.invoke(cli, ["a/b/c.txt"], standalone_mode=False)
assert result.exception is None
assert result.return_value == expect
@pytest.mark.skipif(
not symlinks_supported, reason="The current OS or FS doesn't support symlinks."
)
def test_path_resolve_symlink(tmp_path, runner):
test_file = tmp_path / "file"
test_file_str = os.fsdecode(test_file)
test_file.write_text("")
path_type = click.Path(resolve_path=True)
param = click.Argument(["a"], type=path_type)
ctx = click.Context(click.Command("cli", params=[param]))
test_dir = tmp_path / "dir"
test_dir.mkdir()
abs_link = test_dir / "abs"
abs_link.symlink_to(test_file)
abs_rv = path_type.convert(os.fsdecode(abs_link), param, ctx)
assert abs_rv == test_file_str
rel_link = test_dir / "rel"
rel_link.symlink_to(pathlib.Path("..") / "file")
rel_rv = path_type.convert(os.fsdecode(rel_link), param, ctx)
assert rel_rv == test_file_str