test_config.py 6.2 KB

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