test_config.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # test_config.py -- Tests for reading and writing configuration files
  2. # Copyright (C) 2011 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Tests for reading and writing configuration files."""
  19. from cStringIO import StringIO
  20. from dulwich.config import (
  21. ConfigDict,
  22. ConfigFile,
  23. StackedConfig,
  24. _check_section_name,
  25. _check_variable_name,
  26. _format_string,
  27. _escape_value,
  28. _parse_string,
  29. _unescape_value,
  30. )
  31. from dulwich.tests import TestCase
  32. class ConfigFileTests(TestCase):
  33. def from_file(self, text):
  34. return ConfigFile.from_file(StringIO(text))
  35. def test_empty(self):
  36. ConfigFile()
  37. def test_eq(self):
  38. self.assertEqual(ConfigFile(), ConfigFile())
  39. def test_default_config(self):
  40. cf = self.from_file("""[core]
  41. repositoryformatversion = 0
  42. filemode = true
  43. bare = false
  44. logallrefupdates = true
  45. """)
  46. self.assertEqual(ConfigFile({("core", ): {
  47. "repositoryformatversion": "0",
  48. "filemode": "true",
  49. "bare": "false",
  50. "logallrefupdates": "true"}}), cf)
  51. def test_from_file_empty(self):
  52. cf = self.from_file("")
  53. self.assertEqual(ConfigFile(), cf)
  54. def test_empty_line_before_section(self):
  55. cf = self.from_file("\n[section]\n")
  56. self.assertEqual(ConfigFile({("section", ): {}}), cf)
  57. def test_comment_before_section(self):
  58. cf = self.from_file("# foo\n[section]\n")
  59. self.assertEqual(ConfigFile({("section", ): {}}), cf)
  60. def test_comment_after_section(self):
  61. cf = self.from_file("[section] # foo\n")
  62. self.assertEqual(ConfigFile({("section", ): {}}), cf)
  63. def test_comment_after_variable(self):
  64. cf = self.from_file("[section]\nbar= foo # a comment\n")
  65. self.assertEqual(ConfigFile({("section", ): {"bar": "foo"}}), cf)
  66. def test_from_file_section(self):
  67. cf = self.from_file("[core]\nfoo = bar\n")
  68. self.assertEqual("bar", cf.get(("core", ), "foo"))
  69. self.assertEqual("bar", cf.get(("core", "foo"), "foo"))
  70. def test_from_file_section_case_insensitive(self):
  71. cf = self.from_file("[cOre]\nfOo = bar\n")
  72. self.assertEqual("bar", cf.get(("core", ), "foo"))
  73. self.assertEqual("bar", cf.get(("core", "foo"), "foo"))
  74. def test_from_file_with_mixed_quoted(self):
  75. cf = self.from_file("[core]\nfoo = \"bar\"la\n")
  76. self.assertEqual("barla", cf.get(("core", ), "foo"))
  77. def test_from_file_with_open_quoted(self):
  78. self.assertRaises(ValueError,
  79. self.from_file, "[core]\nfoo = \"bar\n")
  80. def test_from_file_with_quotes(self):
  81. cf = self.from_file(
  82. "[core]\n"
  83. 'foo = " bar"\n')
  84. self.assertEqual(" bar", cf.get(("core", ), "foo"))
  85. def test_from_file_with_interrupted_line(self):
  86. cf = self.from_file(
  87. "[core]\n"
  88. 'foo = bar\\\n'
  89. ' la\n')
  90. self.assertEqual("barla", cf.get(("core", ), "foo"))
  91. def test_from_file_with_boolean_setting(self):
  92. cf = self.from_file(
  93. "[core]\n"
  94. 'foo\n')
  95. self.assertEqual("true", cf.get(("core", ), "foo"))
  96. def test_from_file_subsection(self):
  97. cf = self.from_file("[branch \"foo\"]\nfoo = bar\n")
  98. self.assertEqual("bar", cf.get(("branch", "foo"), "foo"))
  99. def test_from_file_subsection_invalid(self):
  100. self.assertRaises(ValueError,
  101. self.from_file, "[branch \"foo]\nfoo = bar\n")
  102. def test_from_file_subsection_not_quoted(self):
  103. cf = self.from_file("[branch.foo]\nfoo = bar\n")
  104. self.assertEqual("bar", cf.get(("branch", "foo"), "foo"))
  105. def test_write_to_file_empty(self):
  106. c = ConfigFile()
  107. f = StringIO()
  108. c.write_to_file(f)
  109. self.assertEqual("", f.getvalue())
  110. def test_write_to_file_section(self):
  111. c = ConfigFile()
  112. c.set(("core", ), "foo", "bar")
  113. f = StringIO()
  114. c.write_to_file(f)
  115. self.assertEqual("[core]\nfoo = bar\n", f.getvalue())
  116. def test_write_to_file_subsection(self):
  117. c = ConfigFile()
  118. c.set(("branch", "blie"), "foo", "bar")
  119. f = StringIO()
  120. c.write_to_file(f)
  121. self.assertEqual("[branch \"blie\"]\nfoo = bar\n", f.getvalue())
  122. def test_same_line(self):
  123. cf = self.from_file("[branch.foo] foo = bar\n")
  124. self.assertEqual("bar", cf.get(("branch", "foo"), "foo"))
  125. class ConfigDictTests(TestCase):
  126. def test_get_set(self):
  127. cd = ConfigDict()
  128. self.assertRaises(KeyError, cd.get, "foo", "core")
  129. cd.set(("core", ), "foo", "bla")
  130. self.assertEqual("bla", cd.get(("core", ), "foo"))
  131. cd.set(("core", ), "foo", "bloe")
  132. self.assertEqual("bloe", cd.get(("core", ), "foo"))
  133. def test_get_boolean(self):
  134. cd = ConfigDict()
  135. cd.set(("core", ), "foo", "true")
  136. self.assertTrue(cd.get_boolean(("core", ), "foo"))
  137. cd.set(("core", ), "foo", "false")
  138. self.assertFalse(cd.get_boolean(("core", ), "foo"))
  139. cd.set(("core", ), "foo", "invalid")
  140. self.assertRaises(ValueError, cd.get_boolean, ("core", ), "foo")
  141. def test_dict(self):
  142. cd = ConfigDict()
  143. cd.set(("core", ), "foo", "bla")
  144. cd.set(("core2", ), "foo", "bloe")
  145. self.assertEqual([("core2", ), ("core", )], cd.keys())
  146. self.assertEqual(cd[("core", )], {'foo': 'bla'})
  147. cd['a'] = 'b'
  148. self.assertEqual(cd['a'], 'b')
  149. class StackedConfigTests(TestCase):
  150. def test_default_backends(self):
  151. StackedConfig.default_backends()
  152. class UnescapeTests(TestCase):
  153. def test_nothing(self):
  154. self.assertEqual("", _unescape_value(""))
  155. def test_tab(self):
  156. self.assertEqual("\tbar\t", _unescape_value("\\tbar\\t"))
  157. def test_newline(self):
  158. self.assertEqual("\nbar\t", _unescape_value("\\nbar\\t"))
  159. def test_quote(self):
  160. self.assertEqual("\"foo\"", _unescape_value("\\\"foo\\\""))
  161. class EscapeValueTests(TestCase):
  162. def test_nothing(self):
  163. self.assertEqual("foo", _escape_value("foo"))
  164. def test_backslash(self):
  165. self.assertEqual("foo\\\\", _escape_value("foo\\"))
  166. def test_newline(self):
  167. self.assertEqual("foo\\n", _escape_value("foo\n"))
  168. class FormatStringTests(TestCase):
  169. def test_quoted(self):
  170. self.assertEqual('" foo"', _format_string(" foo"))
  171. self.assertEqual('"\\tfoo"', _format_string("\tfoo"))
  172. def test_not_quoted(self):
  173. self.assertEqual('foo', _format_string("foo"))
  174. self.assertEqual('foo bar', _format_string("foo bar"))
  175. class ParseStringTests(TestCase):
  176. def test_quoted(self):
  177. self.assertEqual(' foo', _parse_string('" foo"'))
  178. self.assertEqual('\tfoo', _parse_string('"\\tfoo"'))
  179. def test_not_quoted(self):
  180. self.assertEqual('foo', _parse_string("foo"))
  181. self.assertEqual('foo bar', _parse_string("foo bar"))
  182. class CheckVariableNameTests(TestCase):
  183. def test_invalid(self):
  184. self.assertFalse(_check_variable_name("foo "))
  185. self.assertFalse(_check_variable_name("bar,bar"))
  186. self.assertFalse(_check_variable_name("bar.bar"))
  187. def test_valid(self):
  188. self.assertTrue(_check_variable_name("FOO"))
  189. self.assertTrue(_check_variable_name("foo"))
  190. self.assertTrue(_check_variable_name("foo-bar"))
  191. class CheckSectionNameTests(TestCase):
  192. def test_invalid(self):
  193. self.assertFalse(_check_section_name("foo "))
  194. self.assertFalse(_check_section_name("bar,bar"))
  195. def test_valid(self):
  196. self.assertTrue(_check_section_name("FOO"))
  197. self.assertTrue(_check_section_name("foo"))
  198. self.assertTrue(_check_section_name("foo-bar"))
  199. self.assertTrue(_check_section_name("bar.bar"))