test_config.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. _escape_value,
  33. _format_string,
  34. _parse_string,
  35. apply_instead_of,
  36. parse_submodules,
  37. )
  38. from . import TestCase
  39. class ConfigFileTests(TestCase):
  40. def from_file(self, text):
  41. return ConfigFile.from_file(BytesIO(text))
  42. def test_empty(self):
  43. ConfigFile()
  44. def test_eq(self):
  45. self.assertEqual(ConfigFile(), ConfigFile())
  46. def test_default_config(self):
  47. cf = self.from_file(
  48. b"""[core]
  49. \trepositoryformatversion = 0
  50. \tfilemode = true
  51. \tbare = false
  52. \tlogallrefupdates = true
  53. """
  54. )
  55. self.assertEqual(
  56. ConfigFile(
  57. {
  58. (b"core",): {
  59. b"repositoryformatversion": b"0",
  60. b"filemode": b"true",
  61. b"bare": b"false",
  62. b"logallrefupdates": b"true",
  63. }
  64. }
  65. ),
  66. cf,
  67. )
  68. def test_from_file_empty(self):
  69. cf = self.from_file(b"")
  70. self.assertEqual(ConfigFile(), cf)
  71. def test_empty_line_before_section(self):
  72. cf = self.from_file(b"\n[section]\n")
  73. self.assertEqual(ConfigFile({(b"section",): {}}), cf)
  74. def test_comment_before_section(self):
  75. cf = self.from_file(b"# foo\n[section]\n")
  76. self.assertEqual(ConfigFile({(b"section",): {}}), cf)
  77. def test_comment_after_section(self):
  78. cf = self.from_file(b"[section] # foo\n")
  79. self.assertEqual(ConfigFile({(b"section",): {}}), cf)
  80. def test_comment_after_variable(self):
  81. cf = self.from_file(b"[section]\nbar= foo # a comment\n")
  82. self.assertEqual(ConfigFile({(b"section",): {b"bar": b"foo"}}), cf)
  83. def test_comment_character_within_value_string(self):
  84. cf = self.from_file(b'[section]\nbar= "foo#bar"\n')
  85. self.assertEqual(ConfigFile({(b"section",): {b"bar": b"foo#bar"}}), cf)
  86. def test_comment_character_within_section_string(self):
  87. cf = self.from_file(b'[branch "foo#bar"] # a comment\nbar= foo\n')
  88. self.assertEqual(ConfigFile({(b"branch", b"foo#bar"): {b"bar": b"foo"}}), cf)
  89. def test_closing_bracket_within_section_string(self):
  90. cf = self.from_file(b'[branch "foo]bar"] # a comment\nbar= foo\n')
  91. self.assertEqual(ConfigFile({(b"branch", b"foo]bar"): {b"bar": b"foo"}}), cf)
  92. def test_from_file_section(self):
  93. cf = self.from_file(b"[core]\nfoo = bar\n")
  94. self.assertEqual(b"bar", cf.get((b"core",), b"foo"))
  95. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  96. def test_from_file_multiple(self):
  97. cf = self.from_file(b"[core]\nfoo = bar\nfoo = blah\n")
  98. self.assertEqual([b"bar", b"blah"], list(cf.get_multivar((b"core",), b"foo")))
  99. self.assertEqual([], list(cf.get_multivar((b"core",), b"blah")))
  100. def test_from_file_utf8_bom(self):
  101. text = "[core]\nfoo = b\u00e4r\n".encode("utf-8-sig")
  102. cf = self.from_file(text)
  103. self.assertEqual(b"b\xc3\xa4r", cf.get((b"core",), b"foo"))
  104. def test_from_file_section_case_insensitive_lower(self):
  105. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  106. self.assertEqual(b"bar", cf.get((b"core",), b"foo"))
  107. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  108. def test_from_file_section_case_insensitive_mixed(self):
  109. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  110. self.assertEqual(b"bar", cf.get((b"core",), b"fOo"))
  111. self.assertEqual(b"bar", cf.get((b"cOre", b"fOo"), b"fOo"))
  112. def test_from_file_with_mixed_quoted(self):
  113. cf = self.from_file(b'[core]\nfoo = "bar"la\n')
  114. self.assertEqual(b"barla", cf.get((b"core",), b"foo"))
  115. def test_from_file_section_with_open_brackets(self):
  116. self.assertRaises(ValueError, self.from_file, b"[core\nfoo = bar\n")
  117. def test_from_file_value_with_open_quoted(self):
  118. self.assertRaises(ValueError, self.from_file, b'[core]\nfoo = "bar\n')
  119. def test_from_file_with_quotes(self):
  120. cf = self.from_file(b"[core]\n" b'foo = " bar"\n')
  121. self.assertEqual(b" bar", cf.get((b"core",), b"foo"))
  122. def test_from_file_with_interrupted_line(self):
  123. cf = self.from_file(b"[core]\n" b"foo = bar\\\n" b" la\n")
  124. self.assertEqual(b"barla", cf.get((b"core",), b"foo"))
  125. def test_from_file_with_boolean_setting(self):
  126. cf = self.from_file(b"[core]\n" b"foo\n")
  127. self.assertEqual(b"true", cf.get((b"core",), b"foo"))
  128. def test_from_file_subsection(self):
  129. cf = self.from_file(b'[branch "foo"]\nfoo = bar\n')
  130. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  131. def test_from_file_subsection_invalid(self):
  132. self.assertRaises(ValueError, self.from_file, b'[branch "foo]\nfoo = bar\n')
  133. def test_from_file_subsection_not_quoted(self):
  134. cf = self.from_file(b"[branch.foo]\nfoo = bar\n")
  135. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  136. def test_write_preserve_multivar(self):
  137. cf = self.from_file(b"[core]\nfoo = bar\nfoo = blah\n")
  138. f = BytesIO()
  139. cf.write_to_file(f)
  140. self.assertEqual(b"[core]\n\tfoo = bar\n\tfoo = blah\n", f.getvalue())
  141. def test_write_to_file_empty(self):
  142. c = ConfigFile()
  143. f = BytesIO()
  144. c.write_to_file(f)
  145. self.assertEqual(b"", f.getvalue())
  146. def test_write_to_file_section(self):
  147. c = ConfigFile()
  148. c.set((b"core",), b"foo", b"bar")
  149. f = BytesIO()
  150. c.write_to_file(f)
  151. self.assertEqual(b"[core]\n\tfoo = bar\n", f.getvalue())
  152. def test_write_to_file_subsection(self):
  153. c = ConfigFile()
  154. c.set((b"branch", b"blie"), b"foo", b"bar")
  155. f = BytesIO()
  156. c.write_to_file(f)
  157. self.assertEqual(b'[branch "blie"]\n\tfoo = bar\n', f.getvalue())
  158. def test_same_line(self):
  159. cf = self.from_file(b"[branch.foo] foo = bar\n")
  160. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  161. def test_quoted_newlines_windows(self):
  162. cf = self.from_file(
  163. b"[alias]\r\n"
  164. b"c = '!f() { \\\r\n"
  165. b' printf \'[git commit -m \\"%s\\"]\\n\' \\"$*\\" && \\\r\n'
  166. b' git commit -m \\"$*\\"; \\\r\n'
  167. b" }; f'\r\n"
  168. )
  169. self.assertEqual(list(cf.sections()), [(b"alias",)])
  170. self.assertEqual(
  171. b"'!f() { printf '[git commit -m \"%s\"]\n' " b'"$*" && git commit -m "$*"',
  172. cf.get((b"alias",), b"c"),
  173. )
  174. def test_quoted(self):
  175. cf = self.from_file(
  176. b"""[gui]
  177. \tfontdiff = -family \\\"Ubuntu Mono\\\" -size 11 -overstrike 0
  178. """
  179. )
  180. self.assertEqual(
  181. ConfigFile(
  182. {
  183. (b"gui",): {
  184. b"fontdiff": b'-family "Ubuntu Mono" -size 11 -overstrike 0',
  185. }
  186. }
  187. ),
  188. cf,
  189. )
  190. def test_quoted_multiline(self):
  191. cf = self.from_file(
  192. b"""[alias]
  193. who = \"!who() {\\
  194. git log --no-merges --pretty=format:'%an - %ae' $@ | uniq -c | sort -rn;\\
  195. };\\
  196. who\"
  197. """
  198. )
  199. self.assertEqual(
  200. ConfigFile(
  201. {
  202. (b"alias",): {
  203. b"who": (
  204. b"!who() {git log --no-merges --pretty=format:'%an - "
  205. b"%ae' $@ | uniq -c | sort -rn;};who"
  206. )
  207. }
  208. }
  209. ),
  210. cf,
  211. )
  212. def test_set_hash_gets_quoted(self):
  213. c = ConfigFile()
  214. c.set(b"xandikos", b"color", b"#665544")
  215. f = BytesIO()
  216. c.write_to_file(f)
  217. self.assertEqual(b'[xandikos]\n\tcolor = "#665544"\n', f.getvalue())
  218. class ConfigDictTests(TestCase):
  219. def test_get_set(self):
  220. cd = ConfigDict()
  221. self.assertRaises(KeyError, cd.get, b"foo", b"core")
  222. cd.set((b"core",), b"foo", b"bla")
  223. self.assertEqual(b"bla", cd.get((b"core",), b"foo"))
  224. cd.set((b"core",), b"foo", b"bloe")
  225. self.assertEqual(b"bloe", cd.get((b"core",), b"foo"))
  226. def test_get_boolean(self):
  227. cd = ConfigDict()
  228. cd.set((b"core",), b"foo", b"true")
  229. self.assertTrue(cd.get_boolean((b"core",), b"foo"))
  230. cd.set((b"core",), b"foo", b"false")
  231. self.assertFalse(cd.get_boolean((b"core",), b"foo"))
  232. cd.set((b"core",), b"foo", b"invalid")
  233. self.assertRaises(ValueError, cd.get_boolean, (b"core",), b"foo")
  234. def test_dict(self):
  235. cd = ConfigDict()
  236. cd.set((b"core",), b"foo", b"bla")
  237. cd.set((b"core2",), b"foo", b"bloe")
  238. self.assertEqual([(b"core",), (b"core2",)], list(cd.keys()))
  239. self.assertEqual(cd[(b"core",)], {b"foo": b"bla"})
  240. cd[b"a"] = b"b"
  241. self.assertEqual(cd[b"a"], b"b")
  242. def test_items(self):
  243. cd = ConfigDict()
  244. cd.set((b"core",), b"foo", b"bla")
  245. cd.set((b"core2",), b"foo", b"bloe")
  246. self.assertEqual([(b"foo", b"bla")], list(cd.items((b"core",))))
  247. def test_items_nonexistant(self):
  248. cd = ConfigDict()
  249. cd.set((b"core2",), b"foo", b"bloe")
  250. self.assertEqual([], list(cd.items((b"core",))))
  251. def test_sections(self):
  252. cd = ConfigDict()
  253. cd.set((b"core2",), b"foo", b"bloe")
  254. self.assertEqual([(b"core2",)], list(cd.sections()))
  255. class StackedConfigTests(TestCase):
  256. def test_default_backends(self):
  257. StackedConfig.default_backends()
  258. @skipIf(sys.platform != "win32", "Windows specific config location.")
  259. def test_windows_config_from_path(self):
  260. from dulwich.config import get_win_system_paths
  261. install_dir = os.path.join("C:", "foo", "Git")
  262. self.overrideEnv("PATH", os.path.join(install_dir, "cmd"))
  263. with patch("os.path.exists", return_value=True):
  264. paths = set(get_win_system_paths())
  265. self.assertEqual(
  266. {
  267. os.path.join(os.environ.get("PROGRAMDATA"), "Git", "config"),
  268. os.path.join(install_dir, "etc", "gitconfig"),
  269. },
  270. paths,
  271. )
  272. @skipIf(sys.platform != "win32", "Windows specific config location.")
  273. def test_windows_config_from_reg(self):
  274. import winreg
  275. from dulwich.config import get_win_system_paths
  276. self.overrideEnv("PATH", None)
  277. install_dir = os.path.join("C:", "foo", "Git")
  278. with patch("winreg.OpenKey"):
  279. with patch(
  280. "winreg.QueryValueEx",
  281. return_value=(install_dir, winreg.REG_SZ),
  282. ):
  283. paths = set(get_win_system_paths())
  284. self.assertEqual(
  285. {
  286. os.path.join(os.environ.get("PROGRAMDATA"), "Git", "config"),
  287. os.path.join(install_dir, "etc", "gitconfig"),
  288. },
  289. paths,
  290. )
  291. class EscapeValueTests(TestCase):
  292. def test_nothing(self):
  293. self.assertEqual(b"foo", _escape_value(b"foo"))
  294. def test_backslash(self):
  295. self.assertEqual(b"foo\\\\", _escape_value(b"foo\\"))
  296. def test_newline(self):
  297. self.assertEqual(b"foo\\n", _escape_value(b"foo\n"))
  298. class FormatStringTests(TestCase):
  299. def test_quoted(self):
  300. self.assertEqual(b'" foo"', _format_string(b" foo"))
  301. self.assertEqual(b'"\\tfoo"', _format_string(b"\tfoo"))
  302. def test_not_quoted(self):
  303. self.assertEqual(b"foo", _format_string(b"foo"))
  304. self.assertEqual(b"foo bar", _format_string(b"foo bar"))
  305. class ParseStringTests(TestCase):
  306. def test_quoted(self):
  307. self.assertEqual(b" foo", _parse_string(b'" foo"'))
  308. self.assertEqual(b"\tfoo", _parse_string(b'"\\tfoo"'))
  309. def test_not_quoted(self):
  310. self.assertEqual(b"foo", _parse_string(b"foo"))
  311. self.assertEqual(b"foo bar", _parse_string(b"foo bar"))
  312. def test_nothing(self):
  313. self.assertEqual(b"", _parse_string(b""))
  314. def test_tab(self):
  315. self.assertEqual(b"\tbar\t", _parse_string(b"\\tbar\\t"))
  316. def test_newline(self):
  317. self.assertEqual(b"\nbar\t", _parse_string(b"\\nbar\\t\t"))
  318. def test_quote(self):
  319. self.assertEqual(b'"foo"', _parse_string(b'\\"foo\\"'))
  320. class CheckVariableNameTests(TestCase):
  321. def test_invalid(self):
  322. self.assertFalse(_check_variable_name(b"foo "))
  323. self.assertFalse(_check_variable_name(b"bar,bar"))
  324. self.assertFalse(_check_variable_name(b"bar.bar"))
  325. def test_valid(self):
  326. self.assertTrue(_check_variable_name(b"FOO"))
  327. self.assertTrue(_check_variable_name(b"foo"))
  328. self.assertTrue(_check_variable_name(b"foo-bar"))
  329. class CheckSectionNameTests(TestCase):
  330. def test_invalid(self):
  331. self.assertFalse(_check_section_name(b"foo "))
  332. self.assertFalse(_check_section_name(b"bar,bar"))
  333. def test_valid(self):
  334. self.assertTrue(_check_section_name(b"FOO"))
  335. self.assertTrue(_check_section_name(b"foo"))
  336. self.assertTrue(_check_section_name(b"foo-bar"))
  337. self.assertTrue(_check_section_name(b"bar.bar"))
  338. class SubmodulesTests(TestCase):
  339. def testSubmodules(self):
  340. cf = ConfigFile.from_file(
  341. BytesIO(
  342. b"""\
  343. [submodule "core/lib"]
  344. \tpath = core/lib
  345. \turl = https://github.com/phhusson/QuasselC.git
  346. """
  347. )
  348. )
  349. got = list(parse_submodules(cf))
  350. self.assertEqual(
  351. [
  352. (
  353. b"core/lib",
  354. b"https://github.com/phhusson/QuasselC.git",
  355. b"core/lib",
  356. )
  357. ],
  358. got,
  359. )
  360. def testMalformedSubmodules(self):
  361. cf = ConfigFile.from_file(
  362. BytesIO(
  363. b"""\
  364. [submodule "core/lib"]
  365. \tpath = core/lib
  366. \turl = https://github.com/phhusson/QuasselC.git
  367. [submodule "dulwich"]
  368. \turl = https://github.com/jelmer/dulwich
  369. """
  370. )
  371. )
  372. got = list(parse_submodules(cf))
  373. self.assertEqual(
  374. [
  375. (
  376. b"core/lib",
  377. b"https://github.com/phhusson/QuasselC.git",
  378. b"core/lib",
  379. )
  380. ],
  381. got,
  382. )
  383. class ApplyInsteadOfTests(TestCase):
  384. def test_none(self):
  385. config = ConfigDict()
  386. self.assertEqual(
  387. "https://example.com/", apply_instead_of(config, "https://example.com/")
  388. )
  389. def test_apply(self):
  390. config = ConfigDict()
  391. config.set(("url", "https://samba.org/"), "insteadOf", "https://example.com/")
  392. self.assertEqual(
  393. "https://samba.org/", apply_instead_of(config, "https://example.com/")
  394. )
  395. def test_apply_multiple(self):
  396. config = ConfigDict()
  397. config.set(("url", "https://samba.org/"), "insteadOf", "https://blah.com/")
  398. config.set(("url", "https://samba.org/"), "insteadOf", "https://example.com/")
  399. self.assertEqual(
  400. [b"https://blah.com/", b"https://example.com/"],
  401. list(config.get_multivar(("url", "https://samba.org/"), "insteadOf")),
  402. )
  403. self.assertEqual(
  404. "https://samba.org/", apply_instead_of(config, "https://example.com/")
  405. )