/tests/wconfig_test.py

https://bitbucket.org/tortoisehg/hgtk/ · Python · 377 lines · 311 code · 64 blank · 2 comment · 9 complexity · 2b4160894db2c0dd69f0ca45ba3c12b3 MD5 · raw file

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