test_config.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 configuraiton 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. import os
  33. class ConfigFileTests(TestCase):
  34. def from_file(self, text):
  35. return ConfigFile.from_file(StringIO(text))
  36. def test_empty(self):
  37. ConfigFile()
  38. def test_eq(self):
  39. self.assertEquals(ConfigFile(), ConfigFile())
  40. def test_default_config(self):
  41. cf = self.from_file("""[core]
  42. repositoryformatversion = 0
  43. filemode = true
  44. bare = false
  45. logallrefupdates = true
  46. """)
  47. self.assertEquals(ConfigFile({("core", ): {
  48. "repositoryformatversion": "0",
  49. "filemode": "true",
  50. "bare": "false",
  51. "logallrefupdates": "true"}}), cf)
  52. def test_from_file_empty(self):
  53. cf = self.from_file("")
  54. self.assertEquals(ConfigFile(), cf)
  55. def test_empty_line_before_section(self):
  56. cf = self.from_file("\n[section]\n")
  57. self.assertEquals(ConfigFile({("section", ): {}}), cf)
  58. def test_comment_before_section(self):
  59. cf = self.from_file("# foo\n[section]\n")
  60. self.assertEquals(ConfigFile({("section", ): {}}), cf)
  61. def test_comment_after_section(self):
  62. cf = self.from_file("[section] # foo\n")
  63. self.assertEquals(ConfigFile({("section", ): {}}), cf)
  64. def test_comment_after_variable(self):
  65. cf = self.from_file("[section]\nbar= foo # a comment\n")
  66. self.assertEquals(ConfigFile({("section", ): {"bar": "foo"}}), cf)
  67. def test_from_file_section(self):
  68. cf = self.from_file("[core]\nfoo = bar\n")
  69. self.assertEquals("bar", cf.get(("core", ), "foo"))
  70. self.assertEquals("bar", cf.get(("core", "foo"), "foo"))
  71. def test_from_file_section_case_insensitive(self):
  72. cf = self.from_file("[cOre]\nfOo = bar\n")
  73. self.assertEquals("bar", cf.get(("core", ), "foo"))
  74. self.assertEquals("bar", cf.get(("core", "foo"), "foo"))
  75. def test_from_file_with_mixed_quoted(self):
  76. cf = self.from_file("[core]\nfoo = \"bar\"la\n")
  77. self.assertEquals("barla", cf.get(("core", ), "foo"))
  78. def test_from_file_with_open_quoted(self):
  79. self.assertRaises(ValueError,
  80. self.from_file, "[core]\nfoo = \"bar\n")
  81. def test_from_file_with_quotes(self):
  82. cf = self.from_file(
  83. "[core]\n"
  84. 'foo = " bar"\n')
  85. self.assertEquals(" bar", cf.get(("core", ), "foo"))
  86. def test_from_file_with_interrupted_line(self):
  87. cf = self.from_file(
  88. "[core]\n"
  89. 'foo = bar\\\n'
  90. ' la\n')
  91. self.assertEquals("barla", cf.get(("core", ), "foo"))
  92. def test_from_file_with_boolean_setting(self):
  93. cf = self.from_file(
  94. "[core]\n"
  95. 'foo\n')
  96. self.assertEquals("true", cf.get(("core", ), "foo"))
  97. def test_from_file_subsection(self):
  98. cf = self.from_file("[branch \"foo\"]\nfoo = bar\n")
  99. self.assertEquals("bar", cf.get(("branch", "foo"), "foo"))
  100. def test_from_file_subsection_invalid(self):
  101. self.assertRaises(ValueError,
  102. self.from_file, "[branch \"foo]\nfoo = bar\n")
  103. def test_from_file_subsection_not_quoted(self):
  104. cf = self.from_file("[branch.foo]\nfoo = bar\n")
  105. self.assertEquals("bar", cf.get(("branch", "foo"), "foo"))
  106. def test_write_to_file_empty(self):
  107. c = ConfigFile()
  108. f = StringIO()
  109. c.write_to_file(f)
  110. self.assertEquals("", f.getvalue())
  111. def test_write_to_file_section(self):
  112. c = ConfigFile()
  113. c.set(("core", ), "foo", "bar")
  114. f = StringIO()
  115. c.write_to_file(f)
  116. self.assertEquals("[core]\nfoo = bar\n", f.getvalue())
  117. def test_write_to_file_subsection(self):
  118. c = ConfigFile()
  119. c.set(("branch", "blie"), "foo", "bar")
  120. f = StringIO()
  121. c.write_to_file(f)
  122. self.assertEquals("[branch \"blie\"]\nfoo = bar\n", f.getvalue())
  123. def test_same_line(self):
  124. cf = self.from_file("[branch.foo] foo = bar\n")
  125. self.assertEquals("bar", cf.get(("branch", "foo"), "foo"))
  126. class ConfigDictTests(TestCase):
  127. def test_get_set(self):
  128. cd = ConfigDict()
  129. self.assertRaises(KeyError, cd.get, "foo", "core")
  130. cd.set(("core", ), "foo", "bla")
  131. self.assertEquals("bla", cd.get(("core", ), "foo"))
  132. cd.set(("core", ), "foo", "bloe")
  133. self.assertEquals("bloe", cd.get(("core", ), "foo"))
  134. def test_get_boolean(self):
  135. cd = ConfigDict()
  136. cd.set(("core", ), "foo", "true")
  137. self.assertTrue(cd.get_boolean(("core", ), "foo"))
  138. cd.set(("core", ), "foo", "false")
  139. self.assertFalse(cd.get_boolean(("core", ), "foo"))
  140. cd.set(("core", ), "foo", "invalid")
  141. self.assertRaises(ValueError, cd.get_boolean, ("core", ), "foo")
  142. class StackedConfigTests(TestCase):
  143. def test_default_backends(self):
  144. self.addCleanup(os.environ.__setitem__, "HOME", os.environ["HOME"])
  145. os.environ["HOME"] = "/nonexistant"
  146. StackedConfig.default_backends()
  147. class UnescapeTests(TestCase):
  148. def test_nothing(self):
  149. self.assertEquals("", _unescape_value(""))
  150. def test_tab(self):
  151. self.assertEquals("\tbar\t", _unescape_value("\\tbar\\t"))
  152. def test_newline(self):
  153. self.assertEquals("\nbar\t", _unescape_value("\\nbar\\t"))
  154. def test_quote(self):
  155. self.assertEquals("\"foo\"", _unescape_value("\\\"foo\\\""))
  156. class EscapeValueTests(TestCase):
  157. def test_nothing(self):
  158. self.assertEquals("foo", _escape_value("foo"))
  159. def test_backslash(self):
  160. self.assertEquals("foo\\\\", _escape_value("foo\\"))
  161. def test_newline(self):
  162. self.assertEquals("foo\\n", _escape_value("foo\n"))
  163. class FormatStringTests(TestCase):
  164. def test_quoted(self):
  165. self.assertEquals('" foo"', _format_string(" foo"))
  166. self.assertEquals('"\\tfoo"', _format_string("\tfoo"))
  167. def test_not_quoted(self):
  168. self.assertEquals('foo', _format_string("foo"))
  169. self.assertEquals('foo bar', _format_string("foo bar"))
  170. class ParseStringTests(TestCase):
  171. def test_quoted(self):
  172. self.assertEquals(' foo', _parse_string('" foo"'))
  173. self.assertEquals('\tfoo', _parse_string('"\\tfoo"'))
  174. def test_not_quoted(self):
  175. self.assertEquals('foo', _parse_string("foo"))
  176. self.assertEquals('foo bar', _parse_string("foo bar"))
  177. class CheckVariableNameTests(TestCase):
  178. def test_invalid(self):
  179. self.assertFalse(_check_variable_name("foo "))
  180. self.assertFalse(_check_variable_name("bar,bar"))
  181. self.assertFalse(_check_variable_name("bar.bar"))
  182. def test_valid(self):
  183. self.assertTrue(_check_variable_name("FOO"))
  184. self.assertTrue(_check_variable_name("foo"))
  185. self.assertTrue(_check_variable_name("foo-bar"))
  186. class CheckSectionNameTests(TestCase):
  187. def test_invalid(self):
  188. self.assertFalse(_check_section_name("foo "))
  189. self.assertFalse(_check_section_name("bar,bar"))
  190. def test_valid(self):
  191. self.assertTrue(_check_section_name("FOO"))
  192. self.assertTrue(_check_section_name("foo"))
  193. self.assertTrue(_check_section_name("foo-bar"))
  194. self.assertTrue(_check_section_name("bar.bar"))