-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
265 lines (215 loc) · 8.36 KB
/
tests.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from __future__ import absolute_import, division, print_function, unicode_literals
import os
from os.path import isdir
import pytest
from filesystem_tree import FilesystemTree
@pytest.yield_fixture
def fs():
fs = FilesystemTree()
yield fs
fs.remove()
def test_it_can_be_instantiated():
assert FilesystemTree().__class__.__name__ == 'FilesystemTree'
def test_args_go_to_mk_not_root():
fs = FilesystemTree('foo', 'bar')
assert fs.root != 'foo'
def test_it_makes_a_directory(fs):
assert isdir(fs.root)
def test_resolve_resolves(fs):
path = fs.resolve('some/dir')
assert path == os.path.realpath(os.sep.join([fs.root, 'some', 'dir']))
def test_mk_makes_a_dir(fs):
fs.mk('some/dir')
assert isdir(fs.resolve('some/dir'))
def test_mk_makes_a_dir_with_a_unicode_path(fs):
fs.mk('some/\u2603')
assert isdir(fs.resolve('some/\u2603'))
def test_mk_makes_a_dir_is_like_dash_p(fs):
fs.mk('some/dir', 'some/dir')
assert isdir(fs.resolve('some/dir'))
def test_mk_makes_a_file(fs):
fs.mk(('some/dir/file.txt', 'Greetings, program!'))
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == 'Greetings, program!'
def test_mk_makes_a_file_with_unicode_content(fs):
fs.mk(('some/dir/file.txt', '\u2603'))
contents = open(fs.resolve('some/dir/file.txt'), 'rb').read().decode('UTF-8')
assert contents == '\u2603'
def test_mk_doesnt_choke_on_existing_dir(fs):
fs.mk('some/dir', ('some/dir/file.txt', 'Greetings, program!'))
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == 'Greetings, program!'
def test_remove_removes(fs):
assert isdir(fs.root)
fs.remove()
assert not isdir(fs.root)
def test_resolve_with_no_arg_is_equivalent_to_root(fs):
assert fs.resolve() == fs.root
# should_dedent - sd
def test_sd_defaults_to_true(fs):
fs.mk(('some/dir/file.txt', ' Greetings, program!'))
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == 'Greetings, program!'
def test_sd_false_via_tuple(fs):
fs.mk(('some/dir/file.txt', ' Greetings, program!', 0), should_dedent=1)
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == ' Greetings, program!'
def test_sd_true_via_tuple(fs):
fs.mk(('some/dir/file.txt', ' Greetings, program!', 1), should_dedent=0)
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == 'Greetings, program!'
def test_sd_false_via_mk(fs):
fs.mk(('some/dir/file.txt', ' Greetings, program!'), should_dedent=False)
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == ' Greetings, program!'
def test_sd_true_via_mk(fs):
fs.mk(('some/dir/file.txt', ' Greetings, program!'), should_dedent=1)
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == 'Greetings, program!'
def test_sd_false_via_constructor():
try:
fs = FilesystemTree(should_dedent=False)
fs.mk(('some/dir/file.txt', ' Greetings, program!'))
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == ' Greetings, program!'
finally:
fs.remove()
def test_sd_true_via_constructor():
_reset = FilesystemTree.should_dedent
try:
FilesystemTree.should_dedent = False
fs = FilesystemTree(should_dedent=True)
fs.mk(('some/dir/file.txt', ' Greetings, program!'))
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == 'Greetings, program!'
finally:
FilesystemTree.should_dedent = _reset
fs.remove()
def test_sd_false_via_instance_attribute():
try:
fs = FilesystemTree()
fs.should_dedent = False
fs.mk(('some/dir/file.txt', ' Greetings, program!'))
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == ' Greetings, program!'
finally:
fs.remove()
def test_sd_true_via_instance_attribute():
try:
fs = FilesystemTree(should_dedent=False)
fs.should_dedent = True
fs.mk(('some/dir/file.txt', ' Greetings, program!'))
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == 'Greetings, program!'
finally:
fs.remove()
def test_sd_false_via_class_attribute():
_reset = FilesystemTree.should_dedent
try:
FilesystemTree.should_dedent = False
fs = FilesystemTree()
fs.mk(('some/dir/file.txt', ' Greetings, program!'))
contents = open(fs.resolve('some/dir/file.txt')).read()
assert contents == ' Greetings, program!'
finally:
FilesystemTree.should_dedent = _reset
fs.remove()
# encoding - enc
# To test this, we need to find a Unicode code point and an encoding that
# results in a different bytestring for that code point than the same code
# point encoded with UTF-8. I wrote a little script using the encodings module,
# and discovered that UTF-8 and cp1140 generate different bytestrings for the
# input value '\x04'. The following tests utilize this discrepancy to determine
# which of the two encodings was used when writing the file.
A_UNICODE = '\x04'
AS_UTF8 = b'\x04'
AS_CP1140 = b'7'
def test_encoding_defaults_to_utf8(fs):
fs.mk(('file.txt', A_UNICODE))
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_UTF8
def test_encoding_cp1140_via_tuple(fs):
fs.mk(('file.txt', '\x04', 0, 'cp1140'), encoding='utf8')
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_CP1140
def test_encoding_utf8_via_tuple(fs):
fs.mk(('file.txt', A_UNICODE, 0, 'utf8'), encoding='cp1140')
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_UTF8
def test_encoding_cp1140_via_mk(fs):
fs.mk(('file.txt', A_UNICODE), encoding='cp1140')
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_CP1140
def test_encoding_utf8_via_mk(fs):
fs.encoding = 'cp1140'
fs.mk(('file.txt', '\x04'), encoding='utf8')
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_UTF8
def test_encoding_cp1140_via_constructor():
try:
fs = FilesystemTree(encoding='cp1140')
fs.mk(('file.txt', A_UNICODE))
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_CP1140
finally:
fs.remove()
def test_encoding_utf8_via_constructor():
_reset = FilesystemTree.encoding
try:
FilesystemTree.encoding = 'cp1140'
fs = FilesystemTree(encoding='utf8')
fs.mk(('file.txt', A_UNICODE))
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_UTF8
finally:
FilesystemTree.encoding = _reset
fs.remove()
def test_encoding_cp1140_via_instance_attribute():
try:
fs = FilesystemTree()
fs.encoding = 'cp1140'
fs.mk(('file.txt', A_UNICODE))
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_CP1140
finally:
fs.remove()
def test_encoding_utf8_via_instance_attribute():
try:
fs = FilesystemTree(encoding='cp1140')
fs.encoding = 'utf8'
fs.mk(('file.txt', A_UNICODE))
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_UTF8
finally:
fs.remove()
def test_encoding_cp1140_via_class_attribute():
_reset = FilesystemTree.encoding
try:
FilesystemTree.encoding = 'cp1140'
fs = FilesystemTree()
fs.mk(('file.txt', A_UNICODE))
contents = open(fs.resolve('file.txt'), 'rb').read()
assert contents == AS_CP1140
finally:
FilesystemTree.encoding = _reset
fs.remove()
# test use as a context manager
def test_context_manager_removes_on_clean_context_exit():
ftname = None
with FilesystemTree() as ft:
ft.mk(('file.txt', 'some text'))
ftname = ft.resolve('file.txt')
assert os.path.exists(ftname)
assert not os.path.exists(ftname)
def test_context_manager_doesnt_remove_on_exception_context_exit():
excepted = False
try:
with FilesystemTree() as ft:
ft.mk(('file.txt', 'some text'))
ftname = ft.resolve('file.txt')
assert os.path.exists(ftname)
raise ValueError
except ValueError:
excepted = True
assert excepted
assert os.path.exists(ftname)