test_config.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. from dulwich.config import (
  23. ConfigDict,
  24. ConfigFile,
  25. StackedConfig,
  26. _check_section_name,
  27. _check_variable_name,
  28. _format_string,
  29. _escape_value,
  30. _parse_string,
  31. parse_submodules,
  32. )
  33. from dulwich.tests import (
  34. TestCase,
  35. )
  36. class ConfigFileTests(TestCase):
  37. def from_file(self, text):
  38. return ConfigFile.from_file(BytesIO(text))
  39. def test_empty(self):
  40. ConfigFile()
  41. def test_eq(self):
  42. self.assertEqual(ConfigFile(), ConfigFile())
  43. def test_default_config(self):
  44. cf = self.from_file(b"""[core]
  45. \trepositoryformatversion = 0
  46. \tfilemode = true
  47. \tbare = false
  48. \tlogallrefupdates = true
  49. """)
  50. self.assertEqual(ConfigFile({(b"core", ): {
  51. b"repositoryformatversion": b"0",
  52. b"filemode": b"true",
  53. b"bare": b"false",
  54. b"logallrefupdates": b"true"}}), cf)
  55. def test_from_file_empty(self):
  56. cf = self.from_file(b"")
  57. self.assertEqual(ConfigFile(), cf)
  58. def test_empty_line_before_section(self):
  59. cf = self.from_file(b"\n[section]\n")
  60. self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
  61. def test_comment_before_section(self):
  62. cf = self.from_file(b"# foo\n[section]\n")
  63. self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
  64. def test_comment_after_section(self):
  65. cf = self.from_file(b"[section] # foo\n")
  66. self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
  67. def test_comment_after_variable(self):
  68. cf = self.from_file(b"[section]\nbar= foo # a comment\n")
  69. self.assertEqual(ConfigFile({(b"section", ): {b"bar": b"foo"}}), cf)
  70. def test_comment_character_within_value_string(self):
  71. cf = self.from_file(b"[section]\nbar= \"foo#bar\"\n")
  72. self.assertEqual(
  73. ConfigFile({(b"section", ): {b"bar": b"foo#bar"}}), cf)
  74. def test_comment_character_within_section_string(self):
  75. cf = self.from_file(b"[branch \"foo#bar\"] # a comment\nbar= foo\n")
  76. self.assertEqual(
  77. ConfigFile({(b"branch", b"foo#bar"): {b"bar": b"foo"}}), cf)
  78. def test_from_file_section(self):
  79. cf = self.from_file(b"[core]\nfoo = bar\n")
  80. self.assertEqual(b"bar", cf.get((b"core", ), b"foo"))
  81. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  82. def test_from_file_section_case_insensitive_lower(self):
  83. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  84. self.assertEqual(b"bar", cf.get((b"core", ), b"foo"))
  85. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  86. def test_from_file_section_case_insensitive_mixed(self):
  87. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  88. self.assertEqual(b"bar", cf.get((b"core", ), b"fOo"))
  89. self.assertEqual(b"bar", cf.get((b"cOre", b"fOo"), b"fOo"))
  90. def test_from_file_with_mixed_quoted(self):
  91. cf = self.from_file(b"[core]\nfoo = \"bar\"la\n")
  92. self.assertEqual(b"barla", cf.get((b"core", ), b"foo"))
  93. def test_from_file_section_with_open_brackets(self):
  94. self.assertRaises(ValueError, self.from_file, b"[core\nfoo = bar\n")
  95. def test_from_file_value_with_open_quoted(self):
  96. self.assertRaises(ValueError, self.from_file, b"[core]\nfoo = \"bar\n")
  97. def test_from_file_with_quotes(self):
  98. cf = self.from_file(
  99. b"[core]\n"
  100. b'foo = " bar"\n')
  101. self.assertEqual(b" bar", cf.get((b"core", ), b"foo"))
  102. def test_from_file_with_interrupted_line(self):
  103. cf = self.from_file(
  104. b"[core]\n"
  105. b'foo = bar\\\n'
  106. b' la\n')
  107. self.assertEqual(b"barla", cf.get((b"core", ), b"foo"))
  108. def test_from_file_with_boolean_setting(self):
  109. cf = self.from_file(
  110. b"[core]\n"
  111. b'foo\n')
  112. self.assertEqual(b"true", cf.get((b"core", ), b"foo"))
  113. def test_from_file_subsection(self):
  114. cf = self.from_file(b"[branch \"foo\"]\nfoo = bar\n")
  115. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  116. def test_from_file_subsection_invalid(self):
  117. self.assertRaises(
  118. ValueError, self.from_file, b"[branch \"foo]\nfoo = bar\n")
  119. def test_from_file_subsection_not_quoted(self):
  120. cf = self.from_file(b"[branch.foo]\nfoo = bar\n")
  121. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  122. def test_write_to_file_empty(self):
  123. c = ConfigFile()
  124. f = BytesIO()
  125. c.write_to_file(f)
  126. self.assertEqual(b"", f.getvalue())
  127. def test_write_to_file_section(self):
  128. c = ConfigFile()
  129. c.set((b"core", ), b"foo", b"bar")
  130. f = BytesIO()
  131. c.write_to_file(f)
  132. self.assertEqual(b"[core]\n\tfoo = bar\n", f.getvalue())
  133. def test_write_to_file_subsection(self):
  134. c = ConfigFile()
  135. c.set((b"branch", b"blie"), b"foo", b"bar")
  136. f = BytesIO()
  137. c.write_to_file(f)
  138. self.assertEqual(b"[branch \"blie\"]\n\tfoo = bar\n", f.getvalue())
  139. def test_same_line(self):
  140. cf = self.from_file(b"[branch.foo] foo = bar\n")
  141. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  142. def test_quoted(self):
  143. cf = self.from_file(b"""[gui]
  144. \tfontdiff = -family \\\"Ubuntu Mono\\\" -size 11 -overstrike 0
  145. """)
  146. self.assertEqual(ConfigFile({(b'gui', ): {
  147. b'fontdiff': b'-family "Ubuntu Mono" -size 11 -overstrike 0',
  148. }}), cf)
  149. def test_quoted_multiline(self):
  150. cf = self.from_file(b"""[alias]
  151. who = \"!who() {\\
  152. git log --no-merges --pretty=format:'%an - %ae' $@ | uniq -c | sort -rn;\\
  153. };\\
  154. who\"
  155. """)
  156. self.assertEqual(ConfigFile({(b'alias', ): {
  157. b'who': (b"!who() {git log --no-merges --pretty=format:'%an - "
  158. b"%ae' $@ | uniq -c | sort -rn;};who")
  159. }}), cf)
  160. def test_set_hash_gets_quoted(self):
  161. c = ConfigFile()
  162. c.set(b"xandikos", b"color", b"#665544")
  163. f = BytesIO()
  164. c.write_to_file(f)
  165. self.assertEqual(b"[xandikos]\n\tcolor = \"#665544\"\n", f.getvalue())
  166. class ConfigDictTests(TestCase):
  167. def test_get_set(self):
  168. cd = ConfigDict()
  169. self.assertRaises(KeyError, cd.get, b"foo", b"core")
  170. cd.set((b"core", ), b"foo", b"bla")
  171. self.assertEqual(b"bla", cd.get((b"core", ), b"foo"))
  172. cd.set((b"core", ), b"foo", b"bloe")
  173. self.assertEqual(b"bloe", cd.get((b"core", ), b"foo"))
  174. def test_get_boolean(self):
  175. cd = ConfigDict()
  176. cd.set((b"core", ), b"foo", b"true")
  177. self.assertTrue(cd.get_boolean((b"core", ), b"foo"))
  178. cd.set((b"core", ), b"foo", b"false")
  179. self.assertFalse(cd.get_boolean((b"core", ), b"foo"))
  180. cd.set((b"core", ), b"foo", b"invalid")
  181. self.assertRaises(ValueError, cd.get_boolean, (b"core", ), b"foo")
  182. def test_dict(self):
  183. cd = ConfigDict()
  184. cd.set((b"core", ), b"foo", b"bla")
  185. cd.set((b"core2", ), b"foo", b"bloe")
  186. self.assertEqual([(b"core", ), (b"core2", )], list(cd.keys()))
  187. self.assertEqual(cd[(b"core", )], {b'foo': b'bla'})
  188. cd[b'a'] = b'b'
  189. self.assertEqual(cd[b'a'], b'b')
  190. def test_iteritems(self):
  191. cd = ConfigDict()
  192. cd.set((b"core", ), b"foo", b"bla")
  193. cd.set((b"core2", ), b"foo", b"bloe")
  194. self.assertEqual(
  195. [(b'foo', b'bla')],
  196. list(cd.iteritems((b"core", ))))
  197. def test_iteritems_nonexistant(self):
  198. cd = ConfigDict()
  199. cd.set((b"core2", ), b"foo", b"bloe")
  200. self.assertEqual([], list(cd.iteritems((b"core", ))))
  201. def test_itersections(self):
  202. cd = ConfigDict()
  203. cd.set((b"core2", ), b"foo", b"bloe")
  204. self.assertEqual([(b"core2", )], list(cd.itersections()))
  205. class StackedConfigTests(TestCase):
  206. def test_default_backends(self):
  207. StackedConfig.default_backends()
  208. class EscapeValueTests(TestCase):
  209. def test_nothing(self):
  210. self.assertEqual(b"foo", _escape_value(b"foo"))
  211. def test_backslash(self):
  212. self.assertEqual(b"foo\\\\", _escape_value(b"foo\\"))
  213. def test_newline(self):
  214. self.assertEqual(b"foo\\n", _escape_value(b"foo\n"))
  215. class FormatStringTests(TestCase):
  216. def test_quoted(self):
  217. self.assertEqual(b'" foo"', _format_string(b" foo"))
  218. self.assertEqual(b'"\\tfoo"', _format_string(b"\tfoo"))
  219. def test_not_quoted(self):
  220. self.assertEqual(b'foo', _format_string(b"foo"))
  221. self.assertEqual(b'foo bar', _format_string(b"foo bar"))
  222. class ParseStringTests(TestCase):
  223. def test_quoted(self):
  224. self.assertEqual(b' foo', _parse_string(b'" foo"'))
  225. self.assertEqual(b'\tfoo', _parse_string(b'"\\tfoo"'))
  226. def test_not_quoted(self):
  227. self.assertEqual(b'foo', _parse_string(b"foo"))
  228. self.assertEqual(b'foo bar', _parse_string(b"foo bar"))
  229. def test_nothing(self):
  230. self.assertEqual(b"", _parse_string(b''))
  231. def test_tab(self):
  232. self.assertEqual(b"\tbar\t", _parse_string(b"\\tbar\\t"))
  233. def test_newline(self):
  234. self.assertEqual(b"\nbar\t", _parse_string(b"\\nbar\\t\t"))
  235. def test_quote(self):
  236. self.assertEqual(b"\"foo\"", _parse_string(b"\\\"foo\\\""))
  237. class CheckVariableNameTests(TestCase):
  238. def test_invalid(self):
  239. self.assertFalse(_check_variable_name(b"foo "))
  240. self.assertFalse(_check_variable_name(b"bar,bar"))
  241. self.assertFalse(_check_variable_name(b"bar.bar"))
  242. def test_valid(self):
  243. self.assertTrue(_check_variable_name(b"FOO"))
  244. self.assertTrue(_check_variable_name(b"foo"))
  245. self.assertTrue(_check_variable_name(b"foo-bar"))
  246. class CheckSectionNameTests(TestCase):
  247. def test_invalid(self):
  248. self.assertFalse(_check_section_name(b"foo "))
  249. self.assertFalse(_check_section_name(b"bar,bar"))
  250. def test_valid(self):
  251. self.assertTrue(_check_section_name(b"FOO"))
  252. self.assertTrue(_check_section_name(b"foo"))
  253. self.assertTrue(_check_section_name(b"foo-bar"))
  254. self.assertTrue(_check_section_name(b"bar.bar"))
  255. class SubmodulesTests(TestCase):
  256. def testSubmodules(self):
  257. cf = ConfigFile.from_file(BytesIO(b"""\
  258. [submodule "core/lib"]
  259. \tpath = core/lib
  260. \turl = https://github.com/phhusson/QuasselC.git
  261. """))
  262. got = list(parse_submodules(cf))
  263. self.assertEqual([
  264. (b'core/lib', b'https://github.com/phhusson/QuasselC.git',
  265. b'core/lib')], got)