zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import os
import pathlib
 
import pytest
 
import trio
from trio._path import AsyncAutoWrapperType as Type
from trio._file_io import AsyncIOWrapper
 
 
@pytest.fixture
def path(tmpdir):
    p = str(tmpdir.join("test"))
    return trio.Path(p)
 
 
def method_pair(path, method_name):
    path = pathlib.Path(path)
    async_path = trio.Path(path)
    return getattr(path, method_name), getattr(async_path, method_name)
 
 
async def test_open_is_async_context_manager(path):
    async with await path.open("w") as f:
        assert isinstance(f, AsyncIOWrapper)
 
    assert f.closed
 
 
async def test_magic():
    path = trio.Path("test")
 
    assert str(path) == "test"
    assert bytes(path) == b"test"
 
 
cls_pairs = [
    (trio.Path, pathlib.Path),
    (pathlib.Path, trio.Path),
    (trio.Path, trio.Path),
]
 
 
@pytest.mark.parametrize("cls_a,cls_b", cls_pairs)
async def test_cmp_magic(cls_a, cls_b):
    a, b = cls_a(""), cls_b("")
    assert a == b
    assert not a != b
 
    a, b = cls_a("a"), cls_b("b")
    assert a < b
    assert b > a
 
    # this is intentionally testing equivalence with none, due to the
    # other=sentinel logic in _forward_magic
    assert not a == None  # noqa
    assert not b == None  # noqa
 
 
# upstream python3.8 bug: we should also test (pathlib.Path, trio.Path), but
# __*div__ does not properly raise NotImplementedError like the other comparison
# magic, so trio.Path's implementation does not get dispatched
cls_pairs = [
    (trio.Path, pathlib.Path),
    (trio.Path, trio.Path),
    (trio.Path, str),
    (str, trio.Path),
]
 
 
@pytest.mark.parametrize("cls_a,cls_b", cls_pairs)
async def test_div_magic(cls_a, cls_b):
    a, b = cls_a("a"), cls_b("b")
 
    result = a / b
    assert isinstance(result, trio.Path)
    assert str(result) == os.path.join("a", "b")
 
 
@pytest.mark.parametrize(
    "cls_a,cls_b", [(trio.Path, pathlib.Path), (trio.Path, trio.Path)]
)
@pytest.mark.parametrize("path", ["foo", "foo/bar/baz", "./foo"])
async def test_hash_magic(cls_a, cls_b, path):
    a, b = cls_a(path), cls_b(path)
    assert hash(a) == hash(b)
 
 
async def test_forwarded_properties(path):
    # use `name` as a representative of forwarded properties
 
    assert "name" in dir(path)
    assert path.name == "test"
 
 
async def test_async_method_signature(path):
    # use `resolve` as a representative of wrapped methods
 
    assert path.resolve.__name__ == "resolve"
    assert path.resolve.__qualname__ == "Path.resolve"
 
    assert "pathlib.Path.resolve" in path.resolve.__doc__
 
 
@pytest.mark.parametrize("method_name", ["is_dir", "is_file"])
async def test_compare_async_stat_methods(method_name):
 
    method, async_method = method_pair(".", method_name)
 
    result = method()
    async_result = await async_method()
 
    assert result == async_result
 
 
async def test_invalid_name_not_wrapped(path):
    with pytest.raises(AttributeError):
        getattr(path, "invalid_fake_attr")
 
 
@pytest.mark.parametrize("method_name", ["absolute", "resolve"])
async def test_async_methods_rewrap(method_name):
 
    method, async_method = method_pair(".", method_name)
 
    result = method()
    async_result = await async_method()
 
    assert isinstance(async_result, trio.Path)
    assert str(result) == str(async_result)
 
 
async def test_forward_methods_rewrap(path, tmpdir):
    with_name = path.with_name("foo")
    with_suffix = path.with_suffix(".py")
 
    assert isinstance(with_name, trio.Path)
    assert with_name == tmpdir.join("foo")
    assert isinstance(with_suffix, trio.Path)
    assert with_suffix == tmpdir.join("test.py")
 
 
async def test_forward_properties_rewrap(path):
    assert isinstance(path.parent, trio.Path)
 
 
async def test_forward_methods_without_rewrap(path, tmpdir):
    path = await path.parent.resolve()
 
    assert path.as_uri().startswith("file:///")
 
 
async def test_repr():
    path = trio.Path(".")
 
    assert repr(path) == "trio.Path('.')"
 
 
class MockWrapped:
    unsupported = "unsupported"
    _private = "private"
 
 
class MockWrapper:
    _forwards = MockWrapped
    _wraps = MockWrapped
 
 
async def test_type_forwards_unsupported():
    with pytest.raises(TypeError):
        Type.generate_forwards(MockWrapper, {})
 
 
async def test_type_wraps_unsupported():
    with pytest.raises(TypeError):
        Type.generate_wraps(MockWrapper, {})
 
 
async def test_type_forwards_private():
    Type.generate_forwards(MockWrapper, {"unsupported": None})
 
    assert not hasattr(MockWrapper, "_private")
 
 
async def test_type_wraps_private():
    Type.generate_wraps(MockWrapper, {"unsupported": None})
 
    assert not hasattr(MockWrapper, "_private")
 
 
@pytest.mark.parametrize("meth", [trio.Path.__init__, trio.Path.joinpath])
async def test_path_wraps_path(path, meth):
    wrapped = await path.absolute()
    result = meth(path, wrapped)
    if result is None:
        result = path
 
    assert wrapped == result
 
 
async def test_path_nonpath():
    with pytest.raises(TypeError):
        trio.Path(1)
 
 
async def test_open_file_can_open_path(path):
    async with await trio.open_file(path, "w") as f:
        assert f.name == os.fspath(path)
 
 
async def test_globmethods(path):
    # Populate a directory tree
    await path.mkdir()
    await (path / "foo").mkdir()
    await (path / "foo" / "_bar.txt").write_bytes(b"")
    await (path / "bar.txt").write_bytes(b"")
    await (path / "bar.dat").write_bytes(b"")
 
    # Path.glob
    for _pattern, _results in {
        "*.txt": {"bar.txt"},
        "**/*.txt": {"_bar.txt", "bar.txt"},
    }.items():
        entries = set()
        for entry in await path.glob(_pattern):
            assert isinstance(entry, trio.Path)
            entries.add(entry.name)
 
        assert entries == _results
 
    # Path.rglob
    entries = set()
    for entry in await path.rglob("*.txt"):
        assert isinstance(entry, trio.Path)
        entries.add(entry.name)
 
    assert entries == {"_bar.txt", "bar.txt"}
 
 
async def test_iterdir(path):
    # Populate a directory
    await path.mkdir()
    await (path / "foo").mkdir()
    await (path / "bar.txt").write_bytes(b"")
 
    entries = set()
    for entry in await path.iterdir():
        assert isinstance(entry, trio.Path)
        entries.add(entry.name)
 
    assert entries == {"bar.txt", "foo"}
 
 
async def test_classmethods():
    assert isinstance(await trio.Path.home(), trio.Path)
 
    # pathlib.Path has only two classmethods
    assert str(await trio.Path.home()) == os.path.expanduser("~")
    assert str(await trio.Path.cwd()) == os.getcwd()
 
    # Wrapped method has docstring
    assert trio.Path.home.__doc__