-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_tdir.py
238 lines (176 loc) Β· 5.75 KB
/
test_tdir.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
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
import os
import shutil
import sys
import unittest
from pathlib import Path
import pytest
import tdir
CWD = Path().absolute()
def test_simple_cwd():
with tdir('a', 'b', 'c') as td:
assert sorted(i.name for i in td.iterdir()) == ['a', 'b', 'c']
for i in 'abc':
assert Path(i).read_text() == i + '\n'
def test_simple():
with tdir('a', 'b', {'c': 'c'}, chdir=False) as td:
assert sorted(i.name for i in td.iterdir()) == ['a', 'b', 'c']
for i in 'abc':
assert (td / i).read_text() == i + '\n'
assert not Path(i).exists()
def test_dict():
with tdir(one='ONE', two='TWO') as td:
assert sorted(i.name for i in td.iterdir()) == ['one', 'two']
for i in ('one', 'two'):
assert (td / i).read_text() == i.upper() + '\n'
def test_binary():
with tdir(one=b'ONE', two=bytearray(b'TWO')) as td:
assert sorted(i.name for i in td.iterdir()) == ['one', 'two']
for i in ('one', 'two'):
assert (td / i).read_bytes() == i.upper().encode()
def test_list():
with tdir(sub=['one', 'two']) as td:
sub = td / 'sub'
assert sorted(i.name for i in sub.iterdir()) == ['one', 'two']
for i in ('one', 'two'):
assert (sub / i).read_text() == i + '\n'
def test_path1():
path = Path(__file__)
with tdir(path):
expected = path.read_text()
actual = Path(path.name).read_text()
assert expected == actual
def test_path2():
path = Path(__file__)
with tdir(foo=path):
expected = path.read_text()
actual = Path('foo').read_text()
assert expected == actual
def test_error():
with tdir():
with pytest.raises(TypeError) as m:
tdir.fill('.', foo=3)
assert m.value.args[0].startswith('Do not understand type')
with pytest.raises(TypeError) as m:
with tdir(None):
pass
assert m.value.args[0].startswith('Do not understand type')
@tdir
def test_usedir():
cwd = os.getcwd()
os.mkdir('one')
with tdir():
Path('one.txt').write_text('ONE')
assert os.getcwd() != os.path.join(cwd, 'one')
assert not Path('one/one.txt').exists()
with tdir(use_dir='one'):
Path('one.txt').write_text('ONE')
assert os.getcwd() == os.path.join(cwd, 'one')
assert Path('one/one.txt').read_text() == 'ONE'
def test_save():
with tdir(save=True) as td:
Path('one.txt').write_text('ONE')
assert Path(os.path.join(td, 'one.txt')).read_text() == 'ONE'
shutil.rmtree(td)
def test_copy_dir():
root = Path(__file__).parent
with tdir(root=root) as td:
copied = sorted(f.name for f in (td / 'root').iterdir())
original = sorted(f.name for f in root.iterdir())
assert copied == original
def test_big():
items = {
'foo': {
'__init__.py': 'TEST = 32\n',
'toast.py': 'from . import TEST as TOAST\n',
},
'bar': {
'__init__.py': 'import foo\nTEST = foo.TEST - 9\n',
'toast.py': 'from . import TEST as TOAST\n',
},
'bang.py': 'TEST = 5\n',
'data': ['a', 'b', 'c'],
}
with tdir(**items) as td:
sys_path = sys.path[:]
sys.path.insert(0, str(td))
try:
import bang
import bar.toast
import foo.toast
finally:
sys.path[:] = sys_path
assert bang.TEST == 5
assert foo.toast.TOAST == 32
assert bar.toast.TOAST == 23
for i in 'abc':
assert (td / 'data' / i).read_text() == i + '\n'
with tdir(items) as td:
sys_path = sys.path[:]
sys.path.insert(0, str(td))
try:
import bang
import bar.toast
import foo.toast
finally:
sys.path[:] = sys_path
assert bang.TEST == 5
assert foo.toast.TOAST == 32
assert bar.toast.TOAST == 23
for i in 'abc':
assert (td / 'data' / i).read_text() == i + '\n'
@tdir
class TestTdirClass1(unittest.TestCase):
def test_not_in_root(self):
cwd = str(Path().absolute())
assert cwd != CWD
@tdir()
class TestTdirClass2(unittest.TestCase):
def test_not_in_root(self):
cwd = str(Path().absolute())
assert cwd != CWD
@tdir('a', foo='bar')
class TestTdirClass3(unittest.TestCase):
test_variable = 3
def test_values(self):
assert Path('a').read_text() == 'a\n'
assert Path('foo').read_text() == 'bar\n'
class TestTdirClass4(unittest.TestCase):
@tdir
def test_not_in_root0(self):
cwd = str(Path().absolute())
assert cwd != CWD
def test_not_in_root1(self):
@tdir
def fn():
cwd = str(Path().absolute())
assert cwd != CWD
fn()
@tdir()
def test_not_in_root2(self):
cwd = str(Path().absolute())
assert cwd != CWD
@tdir('a', foo='bar')
def test_values(self):
assert Path('a').read_text() == 'a\n'
assert Path('foo').read_text() == 'bar\n'
@tdir('a', foo='bar', methods='test_keep')
class TestTdirClassMethods(unittest.TestCase):
def test_keep(self):
cwd = Path().absolute()
assert cwd != CWD
def test_not(self):
cwd = Path().absolute()
assert cwd == CWD
def test_recursive():
d1 = Path().absolute()
with tdir() as d2:
f2 = d2 / 'one.txt'
f2.write_text('one.txt')
with tdir() as d3:
f3 = d3 / 'two.txt'
f3.write_text('two.txt')
assert all(p.is_absolute() for p in (d1, d2, d3))
assert d1 != d2 != d3 != d1
assert f2.exists() and f3.exists()
assert f2.exists() and not f3.exists()
assert not f2.exists() and not f3.exists()