Debian Codegraph @

tests/test_directory_actions.py

raw symbols SCIP
1 import pathlib
2
3 import pytest
4
5 import aioftp
6
7
8 @pytest.mark.asyncio
9 async def test_create_and_remove_directory(pair_factory):
10 async with pair_factory() as pair:
11 await pair.client.make_directory("bar")
12 (path, stat), *_ = files = await pair.client.list()
13 assert len(files) == 1
14 assert path == pathlib.PurePosixPath("bar")
15 assert stat["type"] == "dir"
16
17 await pair.client.remove_directory("bar")
18 assert await pair.server_paths_exists("bar") is False
19
20
21 @pytest.mark.asyncio
22 async def test_create_and_remove_directory_long(pair_factory):
23 async with pair_factory() as pair:
24 await pair.client.make_directory("bar/baz")
25 assert await pair.server_paths_exists("bar", "bar/baz")
26 await pair.client.remove_directory("bar/baz")
27 await pair.client.remove_directory("bar")
28 assert await pair.server_paths_exists("bar") is False
29
30
31 @pytest.mark.asyncio
32 async def test_create_directory_long_no_parents(pair_factory):
33 async with pair_factory() as pair:
34 await pair.client.make_directory("bar/baz", parents=False)
35 await pair.client.remove_directory("bar/baz")
36 await pair.client.remove_directory("bar")
37
38
39 @pytest.mark.asyncio
40 async def test_change_directory(pair_factory):
41 async with pair_factory() as pair:
42 await pair.client.make_directory("bar")
43 await pair.client.change_directory("bar")
44 cwd = await pair.client.get_current_directory()
45 assert cwd == pathlib.PurePosixPath("/bar")
46 await pair.client.change_directory()
47 cwd = await pair.client.get_current_directory()
48 assert cwd == pathlib.PurePosixPath("/")
49
50
51 @pytest.mark.asyncio
52 async def test_change_directory_not_exist(
53 pair_factory,
54 expect_codes_in_exception,
55 ):
56 async with pair_factory() as pair:
57 with expect_codes_in_exception("550"):
58 await pair.client.change_directory("bar")
59
60
61 @pytest.mark.asyncio
62 async def test_rename_empty_directory(pair_factory):
63 async with pair_factory() as pair:
64 await pair.client.make_directory("bar")
65 assert await pair.server_paths_exists("bar")
66 assert await pair.server_paths_exists("baz") is False
67 await pair.client.rename("bar", "baz")
68 assert await pair.server_paths_exists("bar") is False
69 assert await pair.server_paths_exists("baz")
70
71
72 @pytest.mark.asyncio
73 async def test_rename_non_empty_directory(pair_factory):
74 async with pair_factory() as pair:
75 await pair.make_server_files("bar/foo.txt")
76 assert await pair.server_paths_exists("bar/foo.txt", "bar")
77 await pair.client.make_directory("hurr")
78 await pair.client.rename("bar", "hurr/baz")
79 assert await pair.server_paths_exists("hurr/baz/foo.txt")
80 assert await pair.server_paths_exists("bar") is False
81
82
83 class FakeErrorPathIO(aioftp.MemoryPathIO):
84 def list(self, path):
85 class Lister(aioftp.AbstractAsyncLister):
86 @aioftp.pathio.universal_exception
87 async def __anext__(self):
88 raise Exception("KERNEL PANIC")
89
90 return Lister(timeout=self.timeout)
91
92
93 @pytest.mark.asyncio
94 async def test_exception_in_list(
95 pair_factory,
96 Server,
97 expect_codes_in_exception,
98 ):
99 s = Server(path_io_factory=FakeErrorPathIO)
100 async with pair_factory(None, s) as pair:
101 with expect_codes_in_exception("451"):
102 await pair.client.list()
103
104
105 @pytest.mark.asyncio
106 async def test_list_recursive(pair_factory):
107 async with pair_factory() as pair:
108 await pair.make_server_files("foo/bar", "foo/baz/baz")
109 files = await pair.client.list(recursive=True)
110 assert len(files) == 4