PageRenderTime 67ms CodeModel.GetById 30ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/wconfig_test.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 377 lines | 325 code | 50 blank | 2 comment | 21 complexity | 2b4160894db2c0dd69f0ca45ba3c12b3 MD5 | raw file
  1import os, tempfile, shutil
  2from nose.tools import *
  3from nose.plugins.skip import SkipTest
  4from StringIO import StringIO
  5from mercurial import config
  6from tortoisehg.util import wconfig
  7
  8def setup():
  9    global _tempdir
 10    _tempdir = tempfile.mkdtemp()
 11
 12def teardown():
 13    shutil.rmtree(_tempdir)
 14
 15
 16def newrconfig(vals={}):
 17    c = config.config()
 18    for k, v in isinstance(vals, dict) and vals.iteritems() or vals:
 19        sec, it = k.split('.', 1)
 20        c.set(sec, it, v)
 21    return c
 22
 23def newwconfig(vals={}):
 24    return wconfig.config(newrconfig(vals))
 25
 26def written(c):
 27    dest = StringIO()
 28    c.write(dest)
 29    return dest.getvalue()
 30
 31def writetempfile(s):
 32    fd, path = tempfile.mkstemp(dir=_tempdir)
 33    os.write(fd, s)
 34    os.close(fd)
 35    return path
 36
 37class _Collector(list):
 38    def __call__(self, func):
 39        self.append(func)
 40        return func
 41
 42with_rconfig = _Collector()
 43with_wconfig = _Collector()
 44with_both = _Collector()
 45
 46
 47def test_both():
 48    for e in with_wconfig + with_both:
 49        if wconfig._hasiniparse:
 50            yield e
 51        else:
 52            def skipped():
 53                raise SkipTest
 54            yield skipped
 55
 56    orighasiniparse = wconfig._hasiniparse
 57    wconfig._hasiniparse = False
 58    try:
 59        for e in with_rconfig + with_both:
 60            yield e
 61    finally:
 62        wconfig._hasiniparse = orighasiniparse
 63
 64
 65@with_both
 66def check_copy():
 67    c = newwconfig({'foo.bar': 'baz'})
 68    assert_equals(c.__class__, c.copy().__class__)
 69    assert_equals('baz', c.copy().get('foo', 'bar'))
 70
 71@with_both
 72def check_contains():
 73    c = newwconfig({'foo.bar': 'baz'})
 74    assert 'foo' in c
 75    assert 'bar' not in c
 76
 77@with_both
 78def check_getitem():
 79    c = newwconfig({'foo.bar': 'x', 'foo.baz': 'y'})
 80    assert_equals({'bar': 'x', 'baz': 'y'}, dict(c['foo']))
 81    assert_equals({}, dict(c['unknown']))
 82
 83@with_both
 84def check_getitem_empty_then_set_no_effect():
 85    c = newwconfig()
 86    c['unknown']['bar'] = 'baz'
 87    assert not c.get('unknown', 'bar')
 88
 89@with_both
 90def check_set_followed_by_getitem_empty():
 91    c = newwconfig()
 92    c['unknown']
 93    c.set('unknown', 'foo', 'bar')
 94    assert_equals('bar', c.get('unknown', 'foo'))
 95    assert_equals('bar', c['unknown']['foo'])
 96
 97@with_both
 98def check_dict_contains():
 99    c = newwconfig({'foo.bar': 'x'})
100    assert 'bar' in c['foo']
101    assert 'baz' not in c['foo']
102
103@with_both
104def check_dict_getitem():
105    c = newwconfig({'foo.bar': 'x'})
106    assert_equals('x', c['foo']['bar'])
107    assert_raises(KeyError, lambda: c['foo']['baz'])
108
109@with_both
110def check_dict_setitem():
111    c = newwconfig({'foo.bar': 'x'})
112    c['foo']['bar'] = 'y'
113    c['foo']['baz'] = 'z'
114    assert_equals('y', c['foo']['bar'])
115    assert_equals('z', c['foo']['baz'])
116
117@with_wconfig  # original config doesn't preserve the order
118def check_dict_setitem_preserve_order():
119    c = newwconfig([('foo.bar', 'x'), ('foo.baz', 'y')])
120    assert_equals(['bar', 'baz'], list(c['foo']))
121    c['foo']['bar'] = 'z'
122    assert_equals(['bar', 'baz'], list(c['foo']))
123
124@with_both
125def check_dict_iter():
126    c = newwconfig({'foo.bar': 'x', 'foo.baz': 'y'})
127    assert_equals(set(['bar', 'baz']), set(c['foo']))
128
129@with_both
130def check_dict_len():
131    c = newwconfig({'foo.bar': 'x'})
132    assert_equals(1, len(c['foo']))
133
134@with_both
135def check_dict_update():
136    c = newwconfig({'foo.bar': 'x', 'foo.baz': 'y'})
137    c['foo'].update(newwconfig({'foo.bar': 'z', 'foo.baz': 'w'})['foo'])
138    assert_equals('z', c['foo']['bar'])
139    assert_equals('w', c['foo']['baz'])
140
141@with_both
142def check_dict_delitem():
143    c = newwconfig({'foo.bar': 'x'})
144    del c['foo']['bar']
145    assert 'bar' not in c['foo']
146
147@with_both
148def check_iter():
149    c = newwconfig({'foo.bar': 'x', 'baz.bax': 'y'})
150    assert_equals(set(['foo', 'baz']), set(c))
151
152@with_both
153def check_update():
154    c0 = newwconfig({'foo.bar': 'x', 'foo.blah': 'w'})
155    c1 = newwconfig({'foo.bar': 'y', 'baz.bax': 'z'})
156    c0.update(c1)
157    assert_equals('y', c0.get('foo', 'bar'))
158    assert_equals('z', c0.get('baz', 'bax'))
159    assert_equals('w', c0.get('foo', 'blah'))
160
161@with_both
162def check_get():
163    c = newwconfig({'foo.bar': 'baz'})
164    assert_equals('baz', c.get('foo', 'bar'))
165    assert_equals(None, c.get('foo', 'baz'))
166    assert_equals('x', c.get('foo', 'baz', 'x'))
167
168@with_both
169def check_source():
170    c = newwconfig()
171    c.set('foo', 'bar', 'baz', source='blah')
172    assert_equals('blah', c.source('foo', 'bar'))
173
174@with_both
175def check_sections():
176    c = newwconfig({'foo.bar': 'x', 'baz.bax': 'y'})
177    assert_equals(['baz', 'foo'], c.sections())
178
179@with_both
180def check_items():
181    c = newwconfig({'foo.bar': 'x', 'foo.baz': 'y'})
182    assert_equals({'bar': 'x', 'baz': 'y'}, dict(c.items('foo')))
183
184@with_both
185def check_set():
186    c = newwconfig({'foo.bar': 'x'})
187    c.set('foo', 'baz', 'y')
188    c.set('foo', 'bar', 'w')
189    c.set('newsection', 'bax', 'z')
190    assert_equals('y', c.get('foo', 'baz'))
191    assert_equals('w', c.get('foo', 'bar'))
192    assert_equals('z', c.get('newsection', 'bax'))
193
194@with_wconfig  # original config doesn't preserve the order
195def check_set_preserve_order():
196    c = newwconfig([('foo.bar', 'x'), ('foo.baz', 'y')])
197    assert_equals(['bar', 'baz'], list(c['foo']))
198    c.set('foo', 'bar', 'z')
199    assert_equals(['bar', 'baz'], list(c['foo']))
200
201# TODO: test_parse
202# TODO: test_read
203
204@with_wconfig
205def check_write_after_set():
206    c = newwconfig()
207    c.set('foo', 'bar', 'baz')
208    assert_equals('[foo]\nbar = baz', written(c).rstrip())
209
210@with_wconfig
211def check_write_empty():
212    c = newwconfig()
213    assert_equals('', written(c).rstrip())
214
215@with_wconfig
216def check_write_after_update():
217    c = newwconfig()
218    c.update(newwconfig({'foo.bar': 'baz'}))
219    assert_equals('[foo]\nbar = baz', written(c).rstrip())
220
221@with_wconfig
222def check_read_write():
223    c = newwconfig()
224    s = '[foo]\nbar = baz'
225    c.read(path='foo', fp=StringIO(s))
226    assert_equals(s, written(c).rstrip())
227
228@with_wconfig
229def check_write_after_dict_setitem():
230    c = newwconfig({'foo.bar': 'x'})
231    c['foo']['bar'] = 'y'
232    assert_equals('[foo]\nbar = y', written(c).rstrip())
233
234@with_wconfig
235def check_write_after_dict_update():
236    c = newwconfig({'foo.bar': 'x'})
237    c['foo'].update({'bar': 'y'})
238    assert_equals('[foo]\nbar = y', written(c).rstrip())
239
240@with_wconfig
241def check_write_after_dict_delitem():
242    c = newwconfig({'foo.bar': 'x', 'foo.baz': 'y'})
243    del c['foo']['bar']
244    assert_equals('[foo]\nbaz = y', written(c).rstrip())
245
246@with_wconfig
247def check_read_write_rem():
248    c = newwconfig()
249    s = '[foo]\nrem = x'
250    c.read(path='foo', fp=StringIO(s))
251    c.set('foo', 'rem', 'y')
252    assert_equals('[foo]\nrem = y', written(c).rstrip())
253
254
255@with_wconfig
256def check_write_conflict_set_set():
257    fname = writetempfile('[foo]\nbar = x')
258    c0 = wconfig.readfile(fname)
259    c1 = wconfig.readfile(fname)
260    c1.set('foo', 'bar', 'y')
261    wconfig.writefile(c1, fname)
262    c0.set('foo', 'bar', 'z')
263    wconfig.writefile(c0, fname)
264
265    cr = wconfig.readfile(fname)
266    assert_equals('z', cr.get('foo', 'bar'))
267
268@with_wconfig
269def check_write_conflict_del_set():
270    fname = writetempfile('[foo]\nbar = x')
271    c0 = wconfig.readfile(fname)
272    c1 = wconfig.readfile(fname)
273    del c1['foo']['bar']
274    wconfig.writefile(c1, fname)
275    c0.set('foo', 'bar', 'z')
276    wconfig.writefile(c0, fname)
277
278    cr = wconfig.readfile(fname)
279    assert_equals('z', cr.get('foo', 'bar'))
280
281@with_wconfig
282def check_write_conflict_set_del():
283    fname = writetempfile('[foo]\nbar = x')
284    c0 = wconfig.readfile(fname)
285    c1 = wconfig.readfile(fname)
286    c1.set('foo', 'bar', 'y')
287    wconfig.writefile(c1, fname)
288    del c0['foo']['bar']
289    wconfig.writefile(c0, fname)
290
291    cr = wconfig.readfile(fname)
292    assert not cr.get('foo', 'bar')
293
294@with_wconfig
295def check_write_conflict_del_del():
296    fname = writetempfile('[foo]\nbar = x')
297    c0 = wconfig.readfile(fname)
298    c1 = wconfig.readfile(fname)
299    del c1['foo']['bar']
300    wconfig.writefile(c1, fname)
301    del c0['foo']['bar']
302    wconfig.writefile(c0, fname)  # shouldn't raise KeyError
303
304    cr = wconfig.readfile(fname)
305    assert not cr.get('foo', 'bar')
306
307@with_wconfig
308def check_write_noconflict_set_set():
309    fname = writetempfile('[foo]\nbar = x')
310    c0 = wconfig.readfile(fname)
311    c1 = wconfig.readfile(fname)
312    c1.set('foo', 'baz', 'y')
313    wconfig.writefile(c1, fname)
314    c0.set('foo', 'bar', 'z')
315    wconfig.writefile(c0, fname)  # should not override foo.baz = y
316
317    cr = wconfig.readfile(fname)
318    assert_equals('z', cr.get('foo', 'bar'))
319    assert_equals('y', cr.get('foo', 'baz'))
320    assert not c0.get('foo', 'baz')  # don't reload c1's change implicitly
321
322@with_wconfig
323def check_write_noconflict_del():
324    fname = writetempfile('[foo]\nbar = x')
325    c0 = wconfig.readfile(fname)
326    c1 = wconfig.readfile(fname)
327    del c1['foo']['bar']
328    wconfig.writefile(c1, fname)
329    wconfig.writefile(c0, fname)  # shouldn't override del foo.bar
330
331    cr = wconfig.readfile(fname)
332    assert not cr.get('foo', 'bar')
333    assert c0.get('foo', 'bar')  # don't reload c1's change implicitly
334
335
336@with_wconfig
337def check_write_copied():
338    fname = writetempfile('[foo]\nbar = x')
339    c0 = wconfig.readfile(fname)
340    c1 = c0.copy()
341    c1.set('foo', 'baz', 'y')
342    wconfig.writefile(c1, fname)
343
344    cr = wconfig.readfile(fname)
345    assert_equals('x', cr.get('foo', 'bar'))
346    assert_equals('y', cr.get('foo', 'baz'))
347
348@with_wconfig
349def check_write_copied_conflict():
350    fname = writetempfile('[foo]\nbar = x')
351    c0 = wconfig.readfile(fname)
352    c1 = c0.copy()
353    c0.set('foo', 'bar', 'y')
354    wconfig.writefile(c0, fname)
355    wconfig.writefile(c1, fname)  # shouldn't override foo.bar = y
356
357    cr = wconfig.readfile(fname)
358    assert_equals('y', cr.get('foo', 'bar'))
359
360@with_wconfig
361def test_write_copied_rconfig():
362    c0 = newrconfig({'foo.bar': 'x'})
363    c1 = wconfig.config(c0)
364    assert_equals('[foo]\nbar = x', written(c1).rstrip())
365
366@with_both
367def check_readfile():
368    fname = writetempfile('[foo]\nbar = baz')
369    c = wconfig.readfile(fname)
370    assert_equals('baz', c.get('foo', 'bar'))
371
372@with_wconfig
373def check_writefile():
374    c = newwconfig({'foo.bar': 'baz'})
375    fname = writetempfile('')
376    wconfig.writefile(c, fname)
377    assert_equals('[foo]\nbar = baz', open(fname).read().rstrip())