forked from benoitc/couchapp
-
Notifications
You must be signed in to change notification settings - Fork 101
/
test_util.py
278 lines (212 loc) · 7.23 KB
/
test_util.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
import shutil
import tempfile
import os
from couchapp.errors import AppError
from couchapp.util import discover_apps, iscouchapp, rcpath, split_path
from couchapp.util import sh_open, remove_comments, is_empty_dir, setup_dir
from couchapp.util import setup_dirs
from mock import patch
from nose.tools import raises
@patch('couchapp.util.user_rcpath')
@patch('couchapp.util._rcpath')
def test_rcpath_cached(_rcpath, user_rcpath):
'''
Global ``_rcpath`` is not None already
'''
assert _rcpath == rcpath()
assert not user_rcpath.called
@patch.dict('couchapp.util.os.environ', clear=True)
@patch('couchapp.util.user_rcpath', return_value=['/mock/couchapp.conf'])
def test_rcpath_empty_env(user_rcpath):
'''
Empty ``COUCHAPPCONF_PATH``
'''
import couchapp.util as util
util._rcpath = None
assert rcpath() == ['/mock/couchapp.conf'], rcpath()
assert user_rcpath.called
@patch.dict('couchapp.util.os.environ',
{'COUCHAPPCONF_PATH': '/mock:/tmp/fake.conf'})
@patch('couchapp.util.os.listdir')
@patch('couchapp.util.os.path.isdir')
@patch('couchapp.util.user_rcpath')
def test_rcpath_env(user_rcpath, isdir, listdir):
'''
With ``COUCHAPPCONF_PATH`` set
'''
import couchapp.util as util
util._rcpath = None
def _isdir(path):
return True if path == '/mock' else False
def _listdir(path):
return ['foo', 'bar', 'couchapp.conf'] if path == '/mock' else []
isdir.side_effect = _isdir
listdir.side_effect = _listdir
assert rcpath() == ['/mock/couchapp.conf', '/tmp/fake.conf'], rcpath()
assert not user_rcpath.called
@patch('couchapp.util.os.path.isfile', return_value=True)
def test_iscouchapp(isfile):
assert iscouchapp('/mock_dir') == True
isfile.assert_called_with('/mock_dir/.couchapprc')
@patch('couchapp.util.os.listdir', return_value=['foo'])
@patch('couchapp.util.os.path.isdir', return_value=True)
@patch('couchapp.util.iscouchapp', return_value=True)
def test_discover_apps(iscouchapp_, isdir, listdir):
assert discover_apps('/mock_dir') == ['/mock_dir/foo']
isdir.assert_called_with('/mock_dir/foo')
listdir.assert_called_with('/mock_dir')
@patch('couchapp.util.os.listdir', return_value=['foo'])
@patch('couchapp.util.os.path.isdir', return_value=True)
@patch('couchapp.util.iscouchapp', return_value=True)
def test_discover_apps_relative_path(iscouchapp_, isdir, listdir):
assert discover_apps('mock_dir') == ['mock_dir/foo']
isdir.assert_called_with('mock_dir/foo')
listdir.assert_called_with('mock_dir')
@patch('couchapp.util.os.listdir', return_value=['.git', 'foo'])
@patch('couchapp.util.os.path.isdir', return_value=True)
@patch('couchapp.util.iscouchapp', return_value=True)
def test_discover_apps_hidden_file(iscouchapp_, isdir, listdir):
'''
Test case for a dir including hidden file
'''
assert discover_apps('mock_dir') == ['mock_dir/foo']
isdir.assert_called_with('mock_dir/foo')
listdir.assert_called_with('mock_dir')
def test_split_path_rel():
'''
Test case for util.split_path with relative path
'''
assert split_path('foo/bar') == ['foo', 'bar']
def test_split_path_abs():
'''
Test case for util.split_path with abs path
'''
path = os.path.realpath('/foo/bar')
assert split_path(path) == [os.path.realpath('/foo'), 'bar']
def test_sh_open():
'''
Test case for ``util.sh_open``
'''
out, err = sh_open('echo mock')
assert out.startswith('mock'), out
assert not err, err
def test_remove_comments():
text = '''{
"mock": 42 // truth
}'''
expect = '{\n "mock": 42 \n}'
ret = remove_comments(text)
assert ret == expect
# testing for multiline comments
text = '''{
"mock": 42, // truth
/* foo
bar
*/
"fake": true}'''
ret = remove_comments(text)
expect = '{\n "mock": 42, \n \n "fake": true}'
assert ret == expect
class test_is_empty_dir_true():
def setup(self):
self.tmpdir = tempfile.mkdtemp()
def test(self):
assert is_empty_dir(self.tmpdir) is True
def teardown(self):
shutil.rmtree(self.tmpdir)
class test_is_empty_dir_false():
def setup(self):
self.tmpdir = tempfile.mkdtemp()
self.tmpfile = tempfile.NamedTemporaryFile(dir=self.tmpdir,
delete=False)
self.tmpfile.close()
def test(self):
assert is_empty_dir(self.tmpdir) is False
def teardown(self):
shutil.rmtree(self.tmpdir)
@patch('couchapp.util.os.mkdir')
def test_setup_dir(mkdir):
setup_dir('/mock')
assert mkdir.called
class test_setup_dir_exists():
def setup(self):
'''
/tmpdir/
*empty*
'''
self.tmpdir = tempfile.mkdtemp()
@patch('couchapp.util.os.mkdir')
def test(self, mkdir):
setup_dir(self.tmpdir, require_empty=True)
assert not mkdir.called
def teardown(self):
shutil.rmtree(self.tmpdir)
class test_setup_dir_exists_not_empty():
def setup(self):
'''
/tmpdir/
tmpfile
'''
self.tmpdir = tempfile.mkdtemp()
self.tmpfile = tempfile.NamedTemporaryFile(dir=self.tmpdir,
delete=False)
self.tmpfile.close()
@raises(AppError)
def main(self):
setup_dir(self.tmpdir, require_empty=True)
@patch('couchapp.util.os.mkdir')
def test(self, mkdir):
self.main()
assert not mkdir.called
def teardown(self):
shutil.rmtree(self.tmpdir)
class test_setup_dir_exists_empty_not_required():
def setup(self):
'''
/tmpdir/
tmpfile
'''
self.tmpdir = tempfile.mkdtemp()
self.tmpfile = tempfile.NamedTemporaryFile(dir=self.tmpdir,
delete=False)
self.tmpfile.close()
@patch('couchapp.util.os.mkdir')
def test(self, mkdir):
setup_dir(self.tmpdir, require_empty=False)
assert not mkdir.called
def teardown(self):
shutil.rmtree(self.tmpdir)
class test_setup_dir_exists_not_dir():
def setup(self):
'''
/strangefile
'''
self.tmpfile = tempfile.NamedTemporaryFile(delete=True)
@raises(AppError)
def main(self):
setup_dir(self.tmpfile.name)
@patch('couchapp.util.os.mkdir')
def test(self, mkdir):
self.main()
assert not mkdir.called
def teardown(self):
del self.tmpfile
@patch('couchapp.util.setup_dir')
def test_setup_dirs(setup_dir):
plist = ['/mock', '/fake', '/mock/app', '/42']
setup_dirs(plist)
assert setup_dir.called
setup_dir.assert_any_call('/mock')
setup_dir.assert_any_call('/fake')
setup_dir.assert_any_call('/mock/app')
setup_dir.assert_any_call('/42')
@patch('couchapp.util.setup_dir')
def test_setup_dirs_args(setup_dir):
plist = ['/mock', '/fake', '/mock/app', '/42']
setup_dirs(plist, require_empty=True)
assert setup_dir.called
setup_dir.assert_any_call('/mock', require_empty=True)
setup_dir.assert_any_call('/fake', require_empty=True)
setup_dir.assert_any_call('/mock/app', require_empty=True)
setup_dir.assert_any_call('/42', require_empty=True)