-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_pathroot.py
More file actions
221 lines (160 loc) · 5.59 KB
/
test_pathroot.py
File metadata and controls
221 lines (160 loc) · 5.59 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
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
"""Unit tests for pathroot."""
import logging
from contextlib import contextmanager
from pathlib import Path
import pytest
import pathroot
LOG = logging.getLogger(__name__)
# Dictionary of test files to create in root_folder.
# Keys should be Path objects, and values should be None
# for directories, or a bytes object for contents.
TEST_FILES = {
Path("d1"): None,
Path("d1/f1.txt"): b"First file",
Path("d2"): None,
Path("d2/f2.txt"): b"Second file",
}
# region Fixtures
@pytest.fixture
def root_folder(tmp_path) -> Path: # type: ignore
"""Self cleaning test folder, populated by TEST_FILES."""
for p, c in TEST_FILES.items():
p = tmp_path / p
if c is None:
p.mkdir(exist_ok=True, parents=True)
LOG.info("** Created dir %s", p)
else:
p.parent.mkdir(exist_ok=True, parents=True)
p.write_bytes(c)
LOG.info("** Create file %s", p)
LOG.info("** Returning %s", tmp_path)
yield tmp_path
for p in sorted(tmp_path.rglob("*"), reverse=True):
if p.is_symlink() or p.is_file():
p.unlink()
LOG.info("** Unlinking %s", p)
elif p.is_dir():
p.rmdir()
LOG.info("** Removing dir %s", p)
@contextmanager
def fix_os_name(v: str):
"""Context manager for replacing pathroot.OS_NAME temporarily.
Args:
v: Value to set OS_NAME to.
"""
old_val = pathroot.OS_NAME
pathroot.OS_NAME = v
LOG.info("** Set OS_NAME to %r", v)
try:
yield
finally:
pathroot.OS_NAME = old_val
LOG.info("** Set OS_NAME back to %r", old_val)
@pytest.fixture
def _force_nt():
"""Force the OS name to nt (for Windows)."""
with fix_os_name("nt"):
yield
@pytest.fixture
def _force_posix():
"""Force the OS name to darwin (for POSIX)."""
with fix_os_name("darwin"):
yield
# endregion
# region Tests
@pytest.mark.usefixtures("_force_nt")
def test_new_windows(root_folder):
"""Test that PathRoot, on Windows, returns a WindowsPathRoot instance."""
# Act
r = pathroot.PathRoot(root_folder)
# Assert
assert type(r) is pathroot.WindowsPathRoot
@pytest.mark.usefixtures("_force_posix")
def test_new_posix(root_folder):
"""Test that PathRoot, on a POSIX OS, returns a PosixPathRoot instance."""
# Act
r = pathroot.PathRoot(root_folder)
# Assert
assert type(r) is pathroot.PosixPathRoot
def test_joinpath_works(root_folder):
"""Test that when we use joinpath with a path inside the the root, it works, and we get a PathRoot instance."""
# Arrange
r = pathroot.PathRoot(root_folder)
# Act
p1 = r.joinpath("foo/bar.txt")
# Assert
assert isinstance(p1, pathroot.PathRoot)
assert p1.safe_root is r.safe_root
def test_joinpath_errors(root_folder):
"""Test that when we use joinpath with a path outside the root, it raises a PathOutsideRootError."""
# Arrange
r = pathroot.PathRoot(root_folder)
# Act and Assert
with pytest.raises(pathroot.PathOutsideRootError):
r.joinpath("..", "..", "etc")
def test_divide_works(root_folder):
"""Test that when we use the divide operator inside the root, it works, and we get a PathRoot instance."""
# Arrange
r = pathroot.PathRoot(root_folder)
# Act
p1 = r / "foo" / "bar.txt"
# Assert
assert isinstance(p1, pathroot.PathRoot)
assert p1.safe_root is r.safe_root
def test_divide_errors(root_folder):
"""Test that when we use the divide operator outside the root, it raises a PathOutsideRootError."""
# Arrange
r = pathroot.PathRoot(root_folder)
# Act and Assert
with pytest.raises(pathroot.PathOutsideRootError):
r / ".." / ".." / "etc"
def test_with_segments_works(root_folder):
"""Test that with_segments with a path inside the root works, and we get a PathRoot instance."""
# Arrange
r = pathroot.PathRoot(root_folder)
# Act
p1 = r.with_segments(root_folder, "foo/bar.txt")
# Assert
assert isinstance(p1, pathroot.PathRoot)
assert p1.safe_root is r.safe_root
def test_with_segments_errors(root_folder):
"""Test that when we use with_segments with a path inside the the root, it works, and we get a PathRoot instance."""
# Arrange
r = pathroot.PathRoot(root_folder)
# Act and Assert
with pytest.raises(pathroot.PathOutsideRootError):
r.with_segments(root_folder, "..", "..", "etc")
def test_rename_works(root_folder):
"""Test that rename works when it should."""
# Arrange
p1 = pathroot.PathRoot(root_folder) / "d1"
# Act
p2 = p1.rename(root_folder / "d3")
# Assert
assert isinstance(p2, pathroot.PathRoot)
assert p2.safe_root is p1.safe_root
def test_rename_errors(root_folder):
"""Test that rename errors when the target path is outside of the root."""
# Arrange
r = pathroot.PathRoot(root_folder)
# Act and Assert
with pytest.raises(pathroot.PathOutsideRootError):
r.rename(root_folder / ".." / ".." / "etc")
def test_replace_works(root_folder):
"""Test that replae works."""
# Arrange
p1 = pathroot.PathRoot(root_folder) / "d1"
# Act
p2 = p1.replace(root_folder / "d3")
# Assert
assert isinstance(p2, pathroot.PathRoot)
assert p2.safe_root is p1.safe_root
def test_replace_errors(root_folder):
"""Test that replace errors when the target path is outside of the root."""
# Arrange
r = pathroot.PathRoot(root_folder)
# Act and Assert
with pytest.raises(pathroot.PathOutsideRootError):
r.replace(root_folder / ".." / ".." / "etc")
# TODO: Other corner cases?
# endregion