test_config.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. # test_config.py -- Tests for reading and writing configuration files
  2. # Copyright (C) 2011 Jelmer Vernooij <jelmer@jelmer.uk>
  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. import os
  22. import sys
  23. from io import BytesIO
  24. from unittest import skipIf
  25. from unittest.mock import patch
  26. from dulwich.config import (
  27. ConfigDict,
  28. ConfigFile,
  29. StackedConfig,
  30. _check_section_name,
  31. _check_variable_name,
  32. _format_string,
  33. _escape_value,
  34. _parse_string,
  35. parse_submodules,
  36. )
  37. from dulwich.tests import (
  38. TestCase,
  39. )
  40. class ConfigFileTests(TestCase):
  41. def from_file(self, text):
  42. return ConfigFile.from_file(BytesIO(text))
  43. def test_empty(self):
  44. ConfigFile()
  45. def test_eq(self):
  46. self.assertEqual(ConfigFile(), ConfigFile())
  47. def test_default_config(self):
  48. cf = self.from_file(
  49. b"""[core]
  50. \trepositoryformatversion = 0
  51. \tfilemode = true
  52. \tbare = false
  53. \tlogallrefupdates = true
  54. """
  55. )
  56. self.assertEqual(
  57. ConfigFile(
  58. {
  59. (b"core",): {
  60. b"repositoryformatversion": b"0",
  61. b"filemode": b"true",
  62. b"bare": b"false",
  63. b"logallrefupdates": b"true",
  64. }
  65. }
  66. ),
  67. cf,
  68. )
  69. def test_from_file_empty(self):
  70. cf = self.from_file(b"")
  71. self.assertEqual(ConfigFile(), cf)
  72. def test_empty_line_before_section(self):
  73. cf = self.from_file(b"\n[section]\n")
  74. self.assertEqual(ConfigFile({(b"section",): {}}), cf)
  75. def test_comment_before_section(self):
  76. cf = self.from_file(b"# foo\n[section]\n")
  77. self.assertEqual(ConfigFile({(b"section",): {}}), cf)
  78. def test_comment_after_section(self):
  79. cf = self.from_file(b"[section] # foo\n")
  80. self.assertEqual(ConfigFile({(b"section",): {}}), cf)
  81. def test_comment_after_variable(self):
  82. cf = self.from_file(b"[section]\nbar= foo # a comment\n")
  83. self.assertEqual(ConfigFile({(b"section",): {b"bar": b"foo"}}), cf)
  84. def test_comment_character_within_value_string(self):
  85. cf = self.from_file(b'[section]\nbar= "foo#bar"\n')
  86. self.assertEqual(ConfigFile({(b"section",): {b"bar": b"foo#bar"}}), cf)
  87. def test_comment_character_within_section_string(self):
  88. cf = self.from_file(b'[branch "foo#bar"] # a comment\nbar= foo\n')
  89. self.assertEqual(ConfigFile({(b"branch", b"foo#bar"): {b"bar": b"foo"}}), cf)
  90. def test_from_file_section(self):
  91. cf = self.from_file(b"[core]\nfoo = bar\n")
  92. self.assertEqual(b"bar", cf.get((b"core",), b"foo"))
  93. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  94. def test_from_file_section_case_insensitive_lower(self):
  95. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  96. self.assertEqual(b"bar", cf.get((b"core",), b"foo"))
  97. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  98. def test_from_file_section_case_insensitive_mixed(self):
  99. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  100. self.assertEqual(b"bar", cf.get((b"core",), b"fOo"))
  101. self.assertEqual(b"bar", cf.get((b"cOre", b"fOo"), b"fOo"))
  102. def test_from_file_with_mixed_quoted(self):
  103. cf = self.from_file(b'[core]\nfoo = "bar"la\n')
  104. self.assertEqual(b"barla", cf.get((b"core",), b"foo"))
  105. def test_from_file_section_with_open_brackets(self):
  106. self.assertRaises(ValueError, self.from_file, b"[core\nfoo = bar\n")
  107. def test_from_file_value_with_open_quoted(self):
  108. self.assertRaises(ValueError, self.from_file, b'[core]\nfoo = "bar\n')
  109. def test_from_file_with_quotes(self):
  110. cf = self.from_file(b"[core]\n" b'foo = " bar"\n')
  111. self.assertEqual(b" bar", cf.get((b"core",), b"foo"))
  112. def test_from_file_with_interrupted_line(self):
  113. cf = self.from_file(b"[core]\n" b"foo = bar\\\n" b" la\n")
  114. self.assertEqual(b"barla", cf.get((b"core",), b"foo"))
  115. def test_from_file_with_boolean_setting(self):
  116. cf = self.from_file(b"[core]\n" b"foo\n")
  117. self.assertEqual(b"true", cf.get((b"core",), b"foo"))
  118. def test_from_file_subsection(self):
  119. cf = self.from_file(b'[branch "foo"]\nfoo = bar\n')
  120. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  121. def test_from_file_subsection_invalid(self):
  122. self.assertRaises(ValueError, self.from_file, b'[branch "foo]\nfoo = bar\n')
  123. def test_from_file_subsection_not_quoted(self):
  124. cf = self.from_file(b"[branch.foo]\nfoo = bar\n")
  125. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  126. def test_write_to_file_empty(self):
  127. c = ConfigFile()
  128. f = BytesIO()
  129. c.write_to_file(f)
  130. self.assertEqual(b"", f.getvalue())
  131. def test_write_to_file_section(self):
  132. c = ConfigFile()
  133. c.set((b"core",), b"foo", b"bar")
  134. f = BytesIO()
  135. c.write_to_file(f)
  136. self.assertEqual(b"[core]\n\tfoo = bar\n", f.getvalue())
  137. def test_write_to_file_subsection(self):
  138. c = ConfigFile()
  139. c.set((b"branch", b"blie"), b"foo", b"bar")
  140. f = BytesIO()
  141. c.write_to_file(f)
  142. self.assertEqual(b'[branch "blie"]\n\tfoo = bar\n', f.getvalue())
  143. def test_same_line(self):
  144. cf = self.from_file(b"[branch.foo] foo = bar\n")
  145. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  146. def test_quoted(self):
  147. cf = self.from_file(
  148. b"""[gui]
  149. \tfontdiff = -family \\\"Ubuntu Mono\\\" -size 11 -overstrike 0
  150. """
  151. )
  152. self.assertEqual(
  153. ConfigFile(
  154. {
  155. (b"gui",): {
  156. b"fontdiff": b'-family "Ubuntu Mono" -size 11 -overstrike 0',
  157. }
  158. }
  159. ),
  160. cf,
  161. )
  162. def test_quoted_multiline(self):
  163. cf = self.from_file(
  164. b"""[alias]
  165. who = \"!who() {\\
  166. git log --no-merges --pretty=format:'%an - %ae' $@ | uniq -c | sort -rn;\\
  167. };\\
  168. who\"
  169. """
  170. )
  171. self.assertEqual(
  172. ConfigFile(
  173. {
  174. (b"alias",): {
  175. b"who": (
  176. b"!who() {git log --no-merges --pretty=format:'%an - "
  177. b"%ae' $@ | uniq -c | sort -rn;};who"
  178. )
  179. }
  180. }
  181. ),
  182. cf,
  183. )
  184. def test_set_hash_gets_quoted(self):
  185. c = ConfigFile()
  186. c.set(b"xandikos", b"color", b"#665544")
  187. f = BytesIO()
  188. c.write_to_file(f)
  189. self.assertEqual(b'[xandikos]\n\tcolor = "#665544"\n', f.getvalue())
  190. class ConfigDictTests(TestCase):
  191. def test_get_set(self):
  192. cd = ConfigDict()
  193. self.assertRaises(KeyError, cd.get, b"foo", b"core")
  194. cd.set((b"core",), b"foo", b"bla")
  195. self.assertEqual(b"bla", cd.get((b"core",), b"foo"))
  196. cd.set((b"core",), b"foo", b"bloe")
  197. self.assertEqual(b"bloe", cd.get((b"core",), b"foo"))
  198. def test_get_boolean(self):
  199. cd = ConfigDict()
  200. cd.set((b"core",), b"foo", b"true")
  201. self.assertTrue(cd.get_boolean((b"core",), b"foo"))
  202. cd.set((b"core",), b"foo", b"false")
  203. self.assertFalse(cd.get_boolean((b"core",), b"foo"))
  204. cd.set((b"core",), b"foo", b"invalid")
  205. self.assertRaises(ValueError, cd.get_boolean, (b"core",), b"foo")
  206. def test_dict(self):
  207. cd = ConfigDict()
  208. cd.set((b"core",), b"foo", b"bla")
  209. cd.set((b"core2",), b"foo", b"bloe")
  210. self.assertEqual([(b"core",), (b"core2",)], list(cd.keys()))
  211. self.assertEqual(cd[(b"core",)], {b"foo": b"bla"})
  212. cd[b"a"] = b"b"
  213. self.assertEqual(cd[b"a"], b"b")
  214. def test_iteritems(self):
  215. cd = ConfigDict()
  216. cd.set((b"core",), b"foo", b"bla")
  217. cd.set((b"core2",), b"foo", b"bloe")
  218. self.assertEqual([(b"foo", b"bla")], list(cd.iteritems((b"core",))))
  219. def test_iteritems_nonexistant(self):
  220. cd = ConfigDict()
  221. cd.set((b"core2",), b"foo", b"bloe")
  222. self.assertEqual([], list(cd.iteritems((b"core",))))
  223. def test_itersections(self):
  224. cd = ConfigDict()
  225. cd.set((b"core2",), b"foo", b"bloe")
  226. self.assertEqual([(b"core2",)], list(cd.itersections()))
  227. class StackedConfigTests(TestCase):
  228. def setUp(self):
  229. super(StackedConfigTests, self).setUp()
  230. self._old_path = os.environ.get("PATH")
  231. def tearDown(self):
  232. super(StackedConfigTests, self).tearDown()
  233. os.environ["PATH"] = self._old_path
  234. def test_default_backends(self):
  235. StackedConfig.default_backends()
  236. @skipIf(sys.platform != "win32", "Windows specfic config location.")
  237. def test_windows_config_from_path(self):
  238. from dulwich.config import get_win_system_paths
  239. install_dir = os.path.join("C:", "foo", "Git")
  240. os.environ["PATH"] = os.path.join(install_dir, "cmd")
  241. with patch("os.path.exists", return_value=True):
  242. paths = set(get_win_system_paths())
  243. self.assertEqual(
  244. {
  245. os.path.join(os.environ.get("PROGRAMDATA"), "Git", "config"),
  246. os.path.join(install_dir, "etc", "gitconfig"),
  247. },
  248. paths,
  249. )
  250. @skipIf(sys.platform != "win32", "Windows specfic config location.")
  251. def test_windows_config_from_reg(self):
  252. import winreg
  253. from dulwich.config import get_win_system_paths
  254. del os.environ["PATH"]
  255. install_dir = os.path.join("C:", "foo", "Git")
  256. with patch("winreg.OpenKey"):
  257. with patch(
  258. "winreg.QueryValueEx",
  259. return_value=(install_dir, winreg.REG_SZ),
  260. ):
  261. paths = set(get_win_system_paths())
  262. self.assertEqual(
  263. {
  264. os.path.join(os.environ.get("PROGRAMDATA"), "Git", "config"),
  265. os.path.join(install_dir, "etc", "gitconfig"),
  266. },
  267. paths,
  268. )
  269. class EscapeValueTests(TestCase):
  270. def test_nothing(self):
  271. self.assertEqual(b"foo", _escape_value(b"foo"))
  272. def test_backslash(self):
  273. self.assertEqual(b"foo\\\\", _escape_value(b"foo\\"))
  274. def test_newline(self):
  275. self.assertEqual(b"foo\\n", _escape_value(b"foo\n"))
  276. class FormatStringTests(TestCase):
  277. def test_quoted(self):
  278. self.assertEqual(b'" foo"', _format_string(b" foo"))
  279. self.assertEqual(b'"\\tfoo"', _format_string(b"\tfoo"))
  280. def test_not_quoted(self):
  281. self.assertEqual(b"foo", _format_string(b"foo"))
  282. self.assertEqual(b"foo bar", _format_string(b"foo bar"))
  283. class ParseStringTests(TestCase):
  284. def test_quoted(self):
  285. self.assertEqual(b" foo", _parse_string(b'" foo"'))
  286. self.assertEqual(b"\tfoo", _parse_string(b'"\\tfoo"'))
  287. def test_not_quoted(self):
  288. self.assertEqual(b"foo", _parse_string(b"foo"))
  289. self.assertEqual(b"foo bar", _parse_string(b"foo bar"))
  290. def test_nothing(self):
  291. self.assertEqual(b"", _parse_string(b""))
  292. def test_tab(self):
  293. self.assertEqual(b"\tbar\t", _parse_string(b"\\tbar\\t"))
  294. def test_newline(self):
  295. self.assertEqual(b"\nbar\t", _parse_string(b"\\nbar\\t\t"))
  296. def test_quote(self):
  297. self.assertEqual(b'"foo"', _parse_string(b'\\"foo\\"'))
  298. class CheckVariableNameTests(TestCase):
  299. def test_invalid(self):
  300. self.assertFalse(_check_variable_name(b"foo "))
  301. self.assertFalse(_check_variable_name(b"bar,bar"))
  302. self.assertFalse(_check_variable_name(b"bar.bar"))
  303. def test_valid(self):
  304. self.assertTrue(_check_variable_name(b"FOO"))
  305. self.assertTrue(_check_variable_name(b"foo"))
  306. self.assertTrue(_check_variable_name(b"foo-bar"))
  307. class CheckSectionNameTests(TestCase):
  308. def test_invalid(self):
  309. self.assertFalse(_check_section_name(b"foo "))
  310. self.assertFalse(_check_section_name(b"bar,bar"))
  311. def test_valid(self):
  312. self.assertTrue(_check_section_name(b"FOO"))
  313. self.assertTrue(_check_section_name(b"foo"))
  314. self.assertTrue(_check_section_name(b"foo-bar"))
  315. self.assertTrue(_check_section_name(b"bar.bar"))
  316. class SubmodulesTests(TestCase):
  317. def testSubmodules(self):
  318. cf = ConfigFile.from_file(
  319. BytesIO(
  320. b"""\
  321. [submodule "core/lib"]
  322. \tpath = core/lib
  323. \turl = https://github.com/phhusson/QuasselC.git
  324. """
  325. )
  326. )
  327. got = list(parse_submodules(cf))
  328. self.assertEqual(
  329. [
  330. (
  331. b"core/lib",
  332. b"https://github.com/phhusson/QuasselC.git",
  333. b"core/lib",
  334. )
  335. ],
  336. got,
  337. )