test_config.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. _format_string,
  25. _escape_value,
  26. _parse_string,
  27. _unescape_value,
  28. )
  29. from dulwich.tests import TestCase
  30. class ConfigFileTests(TestCase):
  31. def from_file(self, text):
  32. return ConfigFile.from_file(StringIO(text))
  33. def test_empty(self):
  34. ConfigFile()
  35. def test_eq(self):
  36. self.assertEquals(ConfigFile(), ConfigFile())
  37. def test_from_file_empty(self):
  38. cf = self.from_file("")
  39. self.assertEquals(ConfigFile(), cf)
  40. def test_from_file_section(self):
  41. cf = self.from_file("[core]\nfoo = bar\n")
  42. self.assertEquals("bar", cf.get("core.foo"))
  43. self.assertEquals("bar", cf.get("core.foo.foo"))
  44. def test_from_file_with_quotes(self):
  45. cf = self.from_file(
  46. "[core]\n"
  47. 'foo = " bar"\n')
  48. self.assertEquals(" bar", cf.get("core.foo"))
  49. def test_from_file_with_interrupted_line(self):
  50. cf = self.from_file(
  51. "[core]\n"
  52. 'foo = bar\\\n'
  53. ' la\n')
  54. self.assertEquals("barla", cf.get("core.foo"))
  55. def test_from_file_subsection(self):
  56. cf = self.from_file("[branch \"foo\"]\nfoo = bar\n")
  57. self.assertEquals("bar", cf.get("branch.foo.foo"))
  58. def test_write_to_file_empty(self):
  59. c = ConfigFile()
  60. f = StringIO()
  61. c.write_to_file(f)
  62. self.assertEquals("", f.getvalue())
  63. def test_write_to_file_section(self):
  64. c = ConfigFile()
  65. c.set("core.foo", "bar")
  66. f = StringIO()
  67. c.write_to_file(f)
  68. self.assertEquals("[core]\nfoo = bar\n", f.getvalue())
  69. def test_write_to_file_subsection(self):
  70. c = ConfigFile()
  71. c.set("branch.blie.foo", "bar")
  72. f = StringIO()
  73. c.write_to_file(f)
  74. self.assertEquals("[branch \"blie\"]\nfoo = bar\n", f.getvalue())
  75. class ConfigDictTests(TestCase):
  76. def test_get_set(self):
  77. cd = ConfigDict()
  78. self.assertRaises(KeyError, cd.get, "core.foo")
  79. cd.set("core.foo", "bla")
  80. self.assertEquals("bla", cd.get("core.foo"))
  81. cd.set("core.foo", "bloe")
  82. self.assertEquals("bloe", cd.get("core.foo"))
  83. class StackedConfigTests(TestCase):
  84. def test_default_backends(self):
  85. StackedConfig.default_backends()
  86. class UnescapeTests(TestCase):
  87. def test_nothing(self):
  88. self.assertEquals("", _unescape_value(""))
  89. def test_tab(self):
  90. self.assertEquals("\tbar\t", _unescape_value("\\tbar\\t"))
  91. def test_newline(self):
  92. self.assertEquals("\nbar\t", _unescape_value("\\nbar\\t"))
  93. def test_quote(self):
  94. self.assertEquals("\"foo\"", _unescape_value("\\\"foo\\\""))
  95. class EscapeValueTests(TestCase):
  96. def test_nothing(self):
  97. self.assertEquals("foo", _escape_value("foo"))
  98. def test_backslash(self):
  99. self.assertEquals("foo\\\\", _escape_value("foo\\"))
  100. def test_newline(self):
  101. self.assertEquals("foo\\n", _escape_value("foo\n"))
  102. class FormatStringTests(TestCase):
  103. def test_quoted(self):
  104. self.assertEquals('" foo"', _format_string(" foo"))
  105. self.assertEquals('"\\tfoo"', _format_string("\tfoo"))
  106. def test_not_quoted(self):
  107. self.assertEquals('foo', _format_string("foo"))
  108. self.assertEquals('foo bar', _format_string("foo bar"))
  109. class ParseStringTests(TestCase):
  110. def test_quoted(self):
  111. self.assertEquals(' foo', _parse_string('" foo"'))
  112. self.assertEquals('\tfoo', _parse_string('"\\tfoo"'))
  113. def test_not_quoted(self):
  114. self.assertEquals('foo', _parse_string("foo"))
  115. self.assertEquals('foo bar', _parse_string("foo bar"))