test_config.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. 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.assertEquals(ConfigFile(), ConfigFile())
  39. def test_from_file_empty(self):
  40. cf = self.from_file("")
  41. self.assertEquals(ConfigFile(), cf)
  42. def test_empty_line_before_section(self):
  43. cf = self.from_file("\n[section]\n")
  44. self.assertEquals(ConfigFile({("section", ): {}}), cf)
  45. def test_comment_before_section(self):
  46. cf = self.from_file("# foo\n[section]\n")
  47. self.assertEquals(ConfigFile({("section", ): {}}), cf)
  48. def test_comment_after_section(self):
  49. cf = self.from_file("[section] # foo\n")
  50. self.assertEquals(ConfigFile({("section", ): {}}), cf)
  51. def test_comment_after_variable(self):
  52. cf = self.from_file("[section]\nbar= foo # a comment\n")
  53. self.assertEquals(ConfigFile({("section", ): {"bar": "foo"}}), cf)
  54. def test_from_file_section(self):
  55. cf = self.from_file("[core]\nfoo = bar\n")
  56. self.assertEquals("bar", cf.get(("core", ), "foo"))
  57. self.assertEquals("bar", cf.get(("core", "foo"), "foo"))
  58. def test_from_file_with_mixed_quoted(self):
  59. cf = self.from_file("[core]\nfoo = \"bar\"la\n")
  60. self.assertEquals("barla", cf.get(("core", ), "foo"))
  61. def test_from_file_with_open_quoted(self):
  62. self.assertRaises(ValueError,
  63. self.from_file, "[core]\nfoo = \"bar\n")
  64. def test_from_file_with_quotes(self):
  65. cf = self.from_file(
  66. "[core]\n"
  67. 'foo = " bar"\n')
  68. self.assertEquals(" bar", cf.get(("core", ), "foo"))
  69. def test_from_file_with_interrupted_line(self):
  70. cf = self.from_file(
  71. "[core]\n"
  72. 'foo = bar\\\n'
  73. ' la\n')
  74. self.assertEquals("barla", cf.get(("core", ), "foo"))
  75. def test_from_file_with_boolean_setting(self):
  76. cf = self.from_file(
  77. "[core]\n"
  78. 'foo\n')
  79. self.assertEquals("true", cf.get(("core", ), "foo"))
  80. def test_from_file_subsection(self):
  81. cf = self.from_file("[branch \"foo\"]\nfoo = bar\n")
  82. self.assertEquals("bar", cf.get(("branch", "foo"), "foo"))
  83. def test_from_file_subsection_invalid(self):
  84. self.assertRaises(ValueError,
  85. self.from_file, "[branch \"foo]\nfoo = bar\n")
  86. def test_from_file_subsection_not_quoted(self):
  87. cf = self.from_file("[branch.foo]\nfoo = bar\n")
  88. self.assertEquals("bar", cf.get(("branch", "foo"), "foo"))
  89. def test_write_to_file_empty(self):
  90. c = ConfigFile()
  91. f = StringIO()
  92. c.write_to_file(f)
  93. self.assertEquals("", f.getvalue())
  94. def test_write_to_file_section(self):
  95. c = ConfigFile()
  96. c.set(("core", ), "foo", "bar")
  97. f = StringIO()
  98. c.write_to_file(f)
  99. self.assertEquals("[core]\nfoo = bar\n", f.getvalue())
  100. def test_write_to_file_subsection(self):
  101. c = ConfigFile()
  102. c.set(("branch", "blie"), "foo", "bar")
  103. f = StringIO()
  104. c.write_to_file(f)
  105. self.assertEquals("[branch \"blie\"]\nfoo = bar\n", f.getvalue())
  106. class ConfigDictTests(TestCase):
  107. def test_get_set(self):
  108. cd = ConfigDict()
  109. self.assertRaises(KeyError, cd.get, "foo", "core")
  110. cd.set(("core", ), "foo", "bla")
  111. self.assertEquals("bla", cd.get(("core", ), "foo"))
  112. cd.set(("core", ), "foo", "bloe")
  113. self.assertEquals("bloe", cd.get(("core", ), "foo"))
  114. class StackedConfigTests(TestCase):
  115. def test_default_backends(self):
  116. StackedConfig.default_backends()
  117. class UnescapeTests(TestCase):
  118. def test_nothing(self):
  119. self.assertEquals("", _unescape_value(""))
  120. def test_tab(self):
  121. self.assertEquals("\tbar\t", _unescape_value("\\tbar\\t"))
  122. def test_newline(self):
  123. self.assertEquals("\nbar\t", _unescape_value("\\nbar\\t"))
  124. def test_quote(self):
  125. self.assertEquals("\"foo\"", _unescape_value("\\\"foo\\\""))
  126. class EscapeValueTests(TestCase):
  127. def test_nothing(self):
  128. self.assertEquals("foo", _escape_value("foo"))
  129. def test_backslash(self):
  130. self.assertEquals("foo\\\\", _escape_value("foo\\"))
  131. def test_newline(self):
  132. self.assertEquals("foo\\n", _escape_value("foo\n"))
  133. class FormatStringTests(TestCase):
  134. def test_quoted(self):
  135. self.assertEquals('" foo"', _format_string(" foo"))
  136. self.assertEquals('"\\tfoo"', _format_string("\tfoo"))
  137. def test_not_quoted(self):
  138. self.assertEquals('foo', _format_string("foo"))
  139. self.assertEquals('foo bar', _format_string("foo bar"))
  140. class ParseStringTests(TestCase):
  141. def test_quoted(self):
  142. self.assertEquals(' foo', _parse_string('" foo"'))
  143. self.assertEquals('\tfoo', _parse_string('"\\tfoo"'))
  144. def test_not_quoted(self):
  145. self.assertEquals('foo', _parse_string("foo"))
  146. self.assertEquals('foo bar', _parse_string("foo bar"))
  147. class CheckVariableNameTests(TestCase):
  148. def test_invalid(self):
  149. self.assertFalse(_check_variable_name("foo "))
  150. self.assertFalse(_check_variable_name("bar,bar"))
  151. self.assertFalse(_check_variable_name("bar.bar"))
  152. def test_valid(self):
  153. self.assertTrue(_check_variable_name("FOO"))
  154. self.assertTrue(_check_variable_name("foo"))
  155. self.assertTrue(_check_variable_name("foo-bar"))
  156. class CheckSectionNameTests(TestCase):
  157. def test_invalid(self):
  158. self.assertFalse(_check_section_name("foo "))
  159. self.assertFalse(_check_section_name("bar,bar"))
  160. def test_valid(self):
  161. self.assertTrue(_check_section_name("FOO"))
  162. self.assertTrue(_check_section_name("foo"))
  163. self.assertTrue(_check_section_name("foo-bar"))
  164. self.assertTrue(_check_section_name("bar.bar"))