test_config.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 io import BytesIO
  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 (
  32. TestCase,
  33. )
  34. class ConfigFileTests(TestCase):
  35. def from_file(self, text):
  36. return ConfigFile.from_file(BytesIO(text))
  37. def test_empty(self):
  38. ConfigFile()
  39. def test_eq(self):
  40. self.assertEqual(ConfigFile(), ConfigFile())
  41. def test_default_config(self):
  42. cf = self.from_file(b"""[core]
  43. repositoryformatversion = 0
  44. filemode = true
  45. bare = false
  46. logallrefupdates = true
  47. """)
  48. self.assertEqual(ConfigFile({(b"core", ): {
  49. b"repositoryformatversion": b"0",
  50. b"filemode": b"true",
  51. b"bare": b"false",
  52. b"logallrefupdates": b"true"}}), cf)
  53. def test_from_file_empty(self):
  54. cf = self.from_file(b"")
  55. self.assertEqual(ConfigFile(), cf)
  56. def test_empty_line_before_section(self):
  57. cf = self.from_file(b"\n[section]\n")
  58. self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
  59. def test_comment_before_section(self):
  60. cf = self.from_file(b"# foo\n[section]\n")
  61. self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
  62. def test_comment_after_section(self):
  63. cf = self.from_file(b"[section] # foo\n")
  64. self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
  65. def test_comment_after_variable(self):
  66. cf = self.from_file(b"[section]\nbar= foo # a comment\n")
  67. self.assertEqual(ConfigFile({(b"section", ): {b"bar": b"foo"}}), cf)
  68. def test_from_file_section(self):
  69. cf = self.from_file(b"[core]\nfoo = bar\n")
  70. self.assertEqual(b"bar", cf.get((b"core", ), b"foo"))
  71. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  72. def test_from_file_section_case_insensitive(self):
  73. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  74. self.assertEqual(b"bar", cf.get((b"core", ), b"foo"))
  75. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  76. def test_from_file_with_mixed_quoted(self):
  77. cf = self.from_file(b"[core]\nfoo = \"bar\"la\n")
  78. self.assertEqual(b"barla", cf.get((b"core", ), b"foo"))
  79. def test_from_file_with_open_quoted(self):
  80. self.assertRaises(ValueError,
  81. self.from_file, b"[core]\nfoo = \"bar\n")
  82. def test_from_file_with_quotes(self):
  83. cf = self.from_file(
  84. b"[core]\n"
  85. b'foo = " bar"\n')
  86. self.assertEqual(b" bar", cf.get((b"core", ), b"foo"))
  87. def test_from_file_with_interrupted_line(self):
  88. cf = self.from_file(
  89. b"[core]\n"
  90. b'foo = bar\\\n'
  91. b' la\n')
  92. self.assertEqual(b"barla", cf.get((b"core", ), b"foo"))
  93. def test_from_file_with_boolean_setting(self):
  94. cf = self.from_file(
  95. b"[core]\n"
  96. b'foo\n')
  97. self.assertEqual(b"true", cf.get((b"core", ), b"foo"))
  98. def test_from_file_subsection(self):
  99. cf = self.from_file(b"[branch \"foo\"]\nfoo = bar\n")
  100. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  101. def test_from_file_subsection_invalid(self):
  102. self.assertRaises(ValueError,
  103. self.from_file, b"[branch \"foo]\nfoo = bar\n")
  104. def test_from_file_subsection_not_quoted(self):
  105. cf = self.from_file(b"[branch.foo]\nfoo = bar\n")
  106. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  107. def test_write_to_file_empty(self):
  108. c = ConfigFile()
  109. f = BytesIO()
  110. c.write_to_file(f)
  111. self.assertEqual(b"", f.getvalue())
  112. def test_write_to_file_section(self):
  113. c = ConfigFile()
  114. c.set((b"core", ), b"foo", b"bar")
  115. f = BytesIO()
  116. c.write_to_file(f)
  117. self.assertEqual(b"[core]\n\tfoo = bar\n", f.getvalue())
  118. def test_write_to_file_subsection(self):
  119. c = ConfigFile()
  120. c.set((b"branch", b"blie"), b"foo", b"bar")
  121. f = BytesIO()
  122. c.write_to_file(f)
  123. self.assertEqual(b"[branch \"blie\"]\n\tfoo = bar\n", f.getvalue())
  124. def test_same_line(self):
  125. cf = self.from_file(b"[branch.foo] foo = bar\n")
  126. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  127. #@expectedFailure
  128. def test_quoted(self):
  129. cf = self.from_file(b"""[gui]
  130. fontdiff = -family \\\"Ubuntu Mono\\\" -size 11 -weight normal -slant roman -underline 0 -overstrike 0
  131. """)
  132. self.assertEqual(ConfigFile({(b'gui', ): {
  133. b'fontdiff': b'-family "Ubuntu Mono" -size 11 -weight normal -slant roman -underline 0 -overstrike 0',
  134. }}), cf)
  135. class ConfigDictTests(TestCase):
  136. def test_get_set(self):
  137. cd = ConfigDict()
  138. self.assertRaises(KeyError, cd.get, b"foo", b"core")
  139. cd.set((b"core", ), b"foo", b"bla")
  140. self.assertEqual(b"bla", cd.get((b"core", ), b"foo"))
  141. cd.set((b"core", ), b"foo", b"bloe")
  142. self.assertEqual(b"bloe", cd.get((b"core", ), b"foo"))
  143. def test_get_boolean(self):
  144. cd = ConfigDict()
  145. cd.set((b"core", ), b"foo", b"true")
  146. self.assertTrue(cd.get_boolean((b"core", ), b"foo"))
  147. cd.set((b"core", ), b"foo", b"false")
  148. self.assertFalse(cd.get_boolean((b"core", ), b"foo"))
  149. cd.set((b"core", ), b"foo", b"invalid")
  150. self.assertRaises(ValueError, cd.get_boolean, (b"core", ), b"foo")
  151. def test_dict(self):
  152. cd = ConfigDict()
  153. cd.set((b"core", ), b"foo", b"bla")
  154. cd.set((b"core2", ), b"foo", b"bloe")
  155. self.assertEqual([(b"core", ), (b"core2", )], list(cd.keys()))
  156. self.assertEqual(cd[(b"core", )], {b'foo': b'bla'})
  157. cd[b'a'] = b'b'
  158. self.assertEqual(cd[b'a'], b'b')
  159. def test_iteritems(self):
  160. cd = ConfigDict()
  161. cd.set((b"core", ), b"foo", b"bla")
  162. cd.set((b"core2", ), b"foo", b"bloe")
  163. self.assertEqual(
  164. [(b'foo', b'bla')],
  165. list(cd.iteritems((b"core", ))))
  166. def test_iteritems_nonexistant(self):
  167. cd = ConfigDict()
  168. cd.set((b"core2", ), b"foo", b"bloe")
  169. self.assertEqual([],
  170. list(cd.iteritems((b"core", ))))
  171. def test_itersections(self):
  172. cd = ConfigDict()
  173. cd.set((b"core2", ), b"foo", b"bloe")
  174. self.assertEqual([(b"core2", )],
  175. list(cd.itersections()))
  176. class StackedConfigTests(TestCase):
  177. def test_default_backends(self):
  178. StackedConfig.default_backends()
  179. class EscapeValueTests(TestCase):
  180. def test_nothing(self):
  181. self.assertEqual(b"foo", _escape_value(b"foo"))
  182. def test_backslash(self):
  183. self.assertEqual(b"foo\\\\", _escape_value(b"foo\\"))
  184. def test_newline(self):
  185. self.assertEqual(b"foo\\n", _escape_value(b"foo\n"))
  186. class FormatStringTests(TestCase):
  187. def test_quoted(self):
  188. self.assertEqual(b'" foo"', _format_string(b" foo"))
  189. self.assertEqual(b'"\\tfoo"', _format_string(b"\tfoo"))
  190. def test_not_quoted(self):
  191. self.assertEqual(b'foo', _format_string(b"foo"))
  192. self.assertEqual(b'foo bar', _format_string(b"foo bar"))
  193. class ParseStringTests(TestCase):
  194. def test_quoted(self):
  195. self.assertEqual(b' foo', _parse_string(b'" foo"'))
  196. self.assertEqual(b'\tfoo', _parse_string(b'"\\tfoo"'))
  197. def test_not_quoted(self):
  198. self.assertEqual(b'foo', _parse_string(b"foo"))
  199. self.assertEqual(b'foo bar', _parse_string(b"foo bar"))
  200. def test_nothing(self):
  201. self.assertEqual(b"", _parse_string(b''))
  202. def test_tab(self):
  203. self.assertEqual(b"\tbar\t", _parse_string(b"\\tbar\\t"))
  204. def test_newline(self):
  205. self.assertEqual(b"\nbar\t", _parse_string(b"\\nbar\\t\t"))
  206. def test_quote(self):
  207. self.assertEqual(b"\"foo\"", _parse_string(b"\\\"foo\\\""))
  208. class CheckVariableNameTests(TestCase):
  209. def test_invalid(self):
  210. self.assertFalse(_check_variable_name(b"foo "))
  211. self.assertFalse(_check_variable_name(b"bar,bar"))
  212. self.assertFalse(_check_variable_name(b"bar.bar"))
  213. def test_valid(self):
  214. self.assertTrue(_check_variable_name(b"FOO"))
  215. self.assertTrue(_check_variable_name(b"foo"))
  216. self.assertTrue(_check_variable_name(b"foo-bar"))
  217. class CheckSectionNameTests(TestCase):
  218. def test_invalid(self):
  219. self.assertFalse(_check_section_name(b"foo "))
  220. self.assertFalse(_check_section_name(b"bar,bar"))
  221. def test_valid(self):
  222. self.assertTrue(_check_section_name(b"FOO"))
  223. self.assertTrue(_check_section_name(b"foo"))
  224. self.assertTrue(_check_section_name(b"foo-bar"))
  225. self.assertTrue(_check_section_name(b"bar.bar"))