test_config.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. # test_config.py -- Tests for reading and writing configuration files
  2. # Copyright (C) 2011 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for reading and writing configuration files."""
  21. from io import BytesIO
  22. import os
  23. from dulwich.config import (
  24. ConfigDict,
  25. ConfigFile,
  26. StackedConfig,
  27. _check_section_name,
  28. _check_variable_name,
  29. _format_string,
  30. _escape_value,
  31. _parse_string,
  32. parse_submodules,
  33. )
  34. from dulwich.tests import (
  35. TestCase,
  36. )
  37. class ConfigFileTests(TestCase):
  38. def from_file(self, text):
  39. return ConfigFile.from_file(BytesIO(text))
  40. def test_empty(self):
  41. ConfigFile()
  42. def test_eq(self):
  43. self.assertEqual(ConfigFile(), ConfigFile())
  44. def test_default_config(self):
  45. cf = self.from_file(b"""[core]
  46. repositoryformatversion = 0
  47. filemode = true
  48. bare = false
  49. logallrefupdates = true
  50. """)
  51. self.assertEqual(ConfigFile({(b"core", ): {
  52. b"repositoryformatversion": b"0",
  53. b"filemode": b"true",
  54. b"bare": b"false",
  55. b"logallrefupdates": b"true"}}), cf)
  56. def test_from_file_empty(self):
  57. cf = self.from_file(b"")
  58. self.assertEqual(ConfigFile(), cf)
  59. def test_empty_line_before_section(self):
  60. cf = self.from_file(b"\n[section]\n")
  61. self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
  62. def test_comment_before_section(self):
  63. cf = self.from_file(b"# foo\n[section]\n")
  64. self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
  65. def test_comment_after_section(self):
  66. cf = self.from_file(b"[section] # foo\n")
  67. self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
  68. def test_comment_after_variable(self):
  69. cf = self.from_file(b"[section]\nbar= foo # a comment\n")
  70. self.assertEqual(ConfigFile({(b"section", ): {b"bar": b"foo"}}), cf)
  71. def test_from_file_section(self):
  72. cf = self.from_file(b"[core]\nfoo = bar\n")
  73. self.assertEqual(b"bar", cf.get((b"core", ), b"foo"))
  74. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  75. def test_from_file_section_case_insensitive(self):
  76. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  77. self.assertEqual(b"bar", cf.get((b"core", ), b"foo"))
  78. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  79. def test_from_file_with_mixed_quoted(self):
  80. cf = self.from_file(b"[core]\nfoo = \"bar\"la\n")
  81. self.assertEqual(b"barla", cf.get((b"core", ), b"foo"))
  82. def test_from_file_with_open_quoted(self):
  83. self.assertRaises(ValueError,
  84. self.from_file, b"[core]\nfoo = \"bar\n")
  85. def test_from_file_with_quotes(self):
  86. cf = self.from_file(
  87. b"[core]\n"
  88. b'foo = " bar"\n')
  89. self.assertEqual(b" bar", cf.get((b"core", ), b"foo"))
  90. def test_from_file_with_interrupted_line(self):
  91. cf = self.from_file(
  92. b"[core]\n"
  93. b'foo = bar\\\n'
  94. b' la\n')
  95. self.assertEqual(b"barla", cf.get((b"core", ), b"foo"))
  96. def test_from_file_with_boolean_setting(self):
  97. cf = self.from_file(
  98. b"[core]\n"
  99. b'foo\n')
  100. self.assertEqual(b"true", cf.get((b"core", ), b"foo"))
  101. def test_from_file_subsection(self):
  102. cf = self.from_file(b"[branch \"foo\"]\nfoo = bar\n")
  103. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  104. def test_from_file_subsection_invalid(self):
  105. self.assertRaises(ValueError,
  106. self.from_file, b"[branch \"foo]\nfoo = bar\n")
  107. def test_from_file_subsection_not_quoted(self):
  108. cf = self.from_file(b"[branch.foo]\nfoo = bar\n")
  109. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  110. def test_write_to_file_empty(self):
  111. c = ConfigFile()
  112. f = BytesIO()
  113. c.write_to_file(f)
  114. self.assertEqual(b"", f.getvalue())
  115. def test_write_to_file_section(self):
  116. c = ConfigFile()
  117. c.set((b"core", ), b"foo", b"bar")
  118. f = BytesIO()
  119. c.write_to_file(f)
  120. self.assertEqual(b"[core]\n\tfoo = bar\n", f.getvalue())
  121. def test_write_to_file_subsection(self):
  122. c = ConfigFile()
  123. c.set((b"branch", b"blie"), b"foo", b"bar")
  124. f = BytesIO()
  125. c.write_to_file(f)
  126. self.assertEqual(b"[branch \"blie\"]\n\tfoo = bar\n", f.getvalue())
  127. def test_same_line(self):
  128. cf = self.from_file(b"[branch.foo] foo = bar\n")
  129. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  130. def test_quoted(self):
  131. cf = self.from_file(b"""[gui]
  132. fontdiff = -family \\\"Ubuntu Mono\\\" -size 11 -weight normal -slant roman -underline 0 -overstrike 0
  133. """)
  134. self.assertEqual(ConfigFile({(b'gui', ): {
  135. b'fontdiff': b'-family "Ubuntu Mono" -size 11 -weight normal -slant roman -underline 0 -overstrike 0',
  136. }}), cf)
  137. def test_quoted_multiline(self):
  138. cf = self.from_file(b"""[alias]
  139. who = \"!who() {\\
  140. git log --no-merges --pretty=format:'%an - %ae' $@ | sort | uniq -c | sort -rn;\\
  141. };\\
  142. who\"
  143. """)
  144. self.assertEqual(ConfigFile({(b'alias', ): {
  145. b'who': b"!who() {git log --no-merges --pretty=format:'%an - %ae' $@ | sort | uniq -c | sort -rn;};who"}}), cf)
  146. def test_set_hash_gets_quoted(self):
  147. c = ConfigFile()
  148. c.set(b"xandikos", b"color", b"#665544")
  149. f = BytesIO()
  150. c.write_to_file(f)
  151. self.assertEqual(b"[xandikos]\n\tcolor = \"#665544\"\n", f.getvalue())
  152. class ConfigDictTests(TestCase):
  153. def test_get_set(self):
  154. cd = ConfigDict()
  155. self.assertRaises(KeyError, cd.get, b"foo", b"core")
  156. cd.set((b"core", ), b"foo", b"bla")
  157. self.assertEqual(b"bla", cd.get((b"core", ), b"foo"))
  158. cd.set((b"core", ), b"foo", b"bloe")
  159. self.assertEqual(b"bloe", cd.get((b"core", ), b"foo"))
  160. def test_get_boolean(self):
  161. cd = ConfigDict()
  162. cd.set((b"core", ), b"foo", b"true")
  163. self.assertTrue(cd.get_boolean((b"core", ), b"foo"))
  164. cd.set((b"core", ), b"foo", b"false")
  165. self.assertFalse(cd.get_boolean((b"core", ), b"foo"))
  166. cd.set((b"core", ), b"foo", b"invalid")
  167. self.assertRaises(ValueError, cd.get_boolean, (b"core", ), b"foo")
  168. def test_dict(self):
  169. cd = ConfigDict()
  170. cd.set((b"core", ), b"foo", b"bla")
  171. cd.set((b"core2", ), b"foo", b"bloe")
  172. self.assertEqual([(b"core", ), (b"core2", )], list(cd.keys()))
  173. self.assertEqual(cd[(b"core", )], {b'foo': b'bla'})
  174. cd[b'a'] = b'b'
  175. self.assertEqual(cd[b'a'], b'b')
  176. def test_iteritems(self):
  177. cd = ConfigDict()
  178. cd.set((b"core", ), b"foo", b"bla")
  179. cd.set((b"core2", ), b"foo", b"bloe")
  180. self.assertEqual(
  181. [(b'foo', b'bla')],
  182. list(cd.iteritems((b"core", ))))
  183. def test_iteritems_nonexistant(self):
  184. cd = ConfigDict()
  185. cd.set((b"core2", ), b"foo", b"bloe")
  186. self.assertEqual([],
  187. list(cd.iteritems((b"core", ))))
  188. def test_itersections(self):
  189. cd = ConfigDict()
  190. cd.set((b"core2", ), b"foo", b"bloe")
  191. self.assertEqual([(b"core2", )],
  192. list(cd.itersections()))
  193. class StackedConfigTests(TestCase):
  194. def test_default_backends(self):
  195. StackedConfig.default_backends()
  196. class EscapeValueTests(TestCase):
  197. def test_nothing(self):
  198. self.assertEqual(b"foo", _escape_value(b"foo"))
  199. def test_backslash(self):
  200. self.assertEqual(b"foo\\\\", _escape_value(b"foo\\"))
  201. def test_newline(self):
  202. self.assertEqual(b"foo\\n", _escape_value(b"foo\n"))
  203. class FormatStringTests(TestCase):
  204. def test_quoted(self):
  205. self.assertEqual(b'" foo"', _format_string(b" foo"))
  206. self.assertEqual(b'"\\tfoo"', _format_string(b"\tfoo"))
  207. def test_not_quoted(self):
  208. self.assertEqual(b'foo', _format_string(b"foo"))
  209. self.assertEqual(b'foo bar', _format_string(b"foo bar"))
  210. class ParseStringTests(TestCase):
  211. def test_quoted(self):
  212. self.assertEqual(b' foo', _parse_string(b'" foo"'))
  213. self.assertEqual(b'\tfoo', _parse_string(b'"\\tfoo"'))
  214. def test_not_quoted(self):
  215. self.assertEqual(b'foo', _parse_string(b"foo"))
  216. self.assertEqual(b'foo bar', _parse_string(b"foo bar"))
  217. def test_nothing(self):
  218. self.assertEqual(b"", _parse_string(b''))
  219. def test_tab(self):
  220. self.assertEqual(b"\tbar\t", _parse_string(b"\\tbar\\t"))
  221. def test_newline(self):
  222. self.assertEqual(b"\nbar\t", _parse_string(b"\\nbar\\t\t"))
  223. def test_quote(self):
  224. self.assertEqual(b"\"foo\"", _parse_string(b"\\\"foo\\\""))
  225. class CheckVariableNameTests(TestCase):
  226. def test_invalid(self):
  227. self.assertFalse(_check_variable_name(b"foo "))
  228. self.assertFalse(_check_variable_name(b"bar,bar"))
  229. self.assertFalse(_check_variable_name(b"bar.bar"))
  230. def test_valid(self):
  231. self.assertTrue(_check_variable_name(b"FOO"))
  232. self.assertTrue(_check_variable_name(b"foo"))
  233. self.assertTrue(_check_variable_name(b"foo-bar"))
  234. class CheckSectionNameTests(TestCase):
  235. def test_invalid(self):
  236. self.assertFalse(_check_section_name(b"foo "))
  237. self.assertFalse(_check_section_name(b"bar,bar"))
  238. def test_valid(self):
  239. self.assertTrue(_check_section_name(b"FOO"))
  240. self.assertTrue(_check_section_name(b"foo"))
  241. self.assertTrue(_check_section_name(b"foo-bar"))
  242. self.assertTrue(_check_section_name(b"bar.bar"))
  243. class SubmodulesTests(TestCase):
  244. def testSubmodules(self):
  245. cf = ConfigFile.from_file(BytesIO(b"""\
  246. [submodule "core/lib"]
  247. path = core/lib
  248. url = https://github.com/phhusson/QuasselC.git
  249. """))
  250. got = list(parse_submodules(cf))
  251. self.assertEqual([
  252. (b'core/lib', b'https://github.com/phhusson/QuasselC.git', b'core/lib')], got)