test_config.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. # test_config.py -- Tests for reading and writing configuration files
  2. # Copyright (C) 2011 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for reading and writing configuration files."""
  22. import os
  23. import sys
  24. from io import BytesIO
  25. from unittest import skipIf
  26. from unittest.mock import patch
  27. from dulwich.config import (
  28. CaseInsensitiveOrderedMultiDict,
  29. ConfigDict,
  30. ConfigFile,
  31. StackedConfig,
  32. _check_section_name,
  33. _check_variable_name,
  34. _escape_value,
  35. _format_string,
  36. _parse_string,
  37. apply_instead_of,
  38. parse_submodules,
  39. )
  40. from . import TestCase
  41. class ConfigFileTests(TestCase):
  42. def from_file(self, text):
  43. return ConfigFile.from_file(BytesIO(text))
  44. def test_empty(self) -> None:
  45. ConfigFile()
  46. def test_eq(self) -> None:
  47. self.assertEqual(ConfigFile(), ConfigFile())
  48. def test_default_config(self) -> None:
  49. cf = self.from_file(
  50. b"""[core]
  51. \trepositoryformatversion = 0
  52. \tfilemode = true
  53. \tbare = false
  54. \tlogallrefupdates = true
  55. """
  56. )
  57. self.assertEqual(
  58. ConfigFile(
  59. {
  60. (b"core",): {
  61. b"repositoryformatversion": b"0",
  62. b"filemode": b"true",
  63. b"bare": b"false",
  64. b"logallrefupdates": b"true",
  65. }
  66. }
  67. ),
  68. cf,
  69. )
  70. def test_from_file_empty(self) -> None:
  71. cf = self.from_file(b"")
  72. self.assertEqual(ConfigFile(), cf)
  73. def test_empty_line_before_section(self) -> None:
  74. cf = self.from_file(b"\n[section]\n")
  75. self.assertEqual(ConfigFile({(b"section",): {}}), cf)
  76. def test_comment_before_section(self) -> None:
  77. cf = self.from_file(b"# foo\n[section]\n")
  78. self.assertEqual(ConfigFile({(b"section",): {}}), cf)
  79. def test_comment_after_section(self) -> None:
  80. cf = self.from_file(b"[section] # foo\n")
  81. self.assertEqual(ConfigFile({(b"section",): {}}), cf)
  82. def test_comment_after_variable(self) -> None:
  83. cf = self.from_file(b"[section]\nbar= foo # a comment\n")
  84. self.assertEqual(ConfigFile({(b"section",): {b"bar": b"foo"}}), cf)
  85. def test_comment_character_within_value_string(self) -> None:
  86. cf = self.from_file(b'[section]\nbar= "foo#bar"\n')
  87. self.assertEqual(ConfigFile({(b"section",): {b"bar": b"foo#bar"}}), cf)
  88. def test_comment_character_within_section_string(self) -> None:
  89. cf = self.from_file(b'[branch "foo#bar"] # a comment\nbar= foo\n')
  90. self.assertEqual(ConfigFile({(b"branch", b"foo#bar"): {b"bar": b"foo"}}), cf)
  91. def test_closing_bracket_within_section_string(self) -> None:
  92. cf = self.from_file(b'[branch "foo]bar"] # a comment\nbar= foo\n')
  93. self.assertEqual(ConfigFile({(b"branch", b"foo]bar"): {b"bar": b"foo"}}), cf)
  94. def test_from_file_section(self) -> None:
  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_multiple(self) -> None:
  99. cf = self.from_file(b"[core]\nfoo = bar\nfoo = blah\n")
  100. self.assertEqual([b"bar", b"blah"], list(cf.get_multivar((b"core",), b"foo")))
  101. self.assertEqual([], list(cf.get_multivar((b"core",), b"blah")))
  102. def test_from_file_utf8_bom(self) -> None:
  103. text = "[core]\nfoo = b\u00e4r\n".encode("utf-8-sig")
  104. cf = self.from_file(text)
  105. self.assertEqual(b"b\xc3\xa4r", cf.get((b"core",), b"foo"))
  106. def test_from_file_section_case_insensitive_lower(self) -> None:
  107. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  108. self.assertEqual(b"bar", cf.get((b"core",), b"foo"))
  109. self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
  110. def test_from_file_section_case_insensitive_mixed(self) -> None:
  111. cf = self.from_file(b"[cOre]\nfOo = bar\n")
  112. self.assertEqual(b"bar", cf.get((b"core",), b"fOo"))
  113. self.assertEqual(b"bar", cf.get((b"cOre", b"fOo"), b"fOo"))
  114. def test_from_file_with_mixed_quoted(self) -> None:
  115. cf = self.from_file(b'[core]\nfoo = "bar"la\n')
  116. self.assertEqual(b"barla", cf.get((b"core",), b"foo"))
  117. def test_from_file_section_with_open_brackets(self) -> None:
  118. self.assertRaises(ValueError, self.from_file, b"[core\nfoo = bar\n")
  119. def test_from_file_value_with_open_quoted(self) -> None:
  120. self.assertRaises(ValueError, self.from_file, b'[core]\nfoo = "bar\n')
  121. def test_from_file_with_quotes(self) -> None:
  122. cf = self.from_file(b'[core]\nfoo = " bar"\n')
  123. self.assertEqual(b" bar", cf.get((b"core",), b"foo"))
  124. def test_from_file_with_interrupted_line(self) -> None:
  125. cf = self.from_file(b"[core]\nfoo = bar\\\n la\n")
  126. self.assertEqual(b"barla", cf.get((b"core",), b"foo"))
  127. def test_from_file_with_boolean_setting(self) -> None:
  128. cf = self.from_file(b"[core]\nfoo\n")
  129. self.assertEqual(b"true", cf.get((b"core",), b"foo"))
  130. def test_from_file_subsection(self) -> None:
  131. cf = self.from_file(b'[branch "foo"]\nfoo = bar\n')
  132. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  133. def test_from_file_subsection_invalid(self) -> None:
  134. self.assertRaises(ValueError, self.from_file, b'[branch "foo]\nfoo = bar\n')
  135. def test_from_file_subsection_not_quoted(self) -> None:
  136. cf = self.from_file(b"[branch.foo]\nfoo = bar\n")
  137. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  138. def test_write_preserve_multivar(self) -> None:
  139. cf = self.from_file(b"[core]\nfoo = bar\nfoo = blah\n")
  140. f = BytesIO()
  141. cf.write_to_file(f)
  142. self.assertEqual(b"[core]\n\tfoo = bar\n\tfoo = blah\n", f.getvalue())
  143. def test_write_to_file_empty(self) -> None:
  144. c = ConfigFile()
  145. f = BytesIO()
  146. c.write_to_file(f)
  147. self.assertEqual(b"", f.getvalue())
  148. def test_write_to_file_section(self) -> None:
  149. c = ConfigFile()
  150. c.set((b"core",), b"foo", b"bar")
  151. f = BytesIO()
  152. c.write_to_file(f)
  153. self.assertEqual(b"[core]\n\tfoo = bar\n", f.getvalue())
  154. def test_write_to_file_section_multiple(self) -> None:
  155. c = ConfigFile()
  156. c.set((b"core",), b"foo", b"old")
  157. c.set((b"core",), b"foo", b"new")
  158. f = BytesIO()
  159. c.write_to_file(f)
  160. self.assertEqual(b"[core]\n\tfoo = new\n", f.getvalue())
  161. def test_write_to_file_subsection(self) -> None:
  162. c = ConfigFile()
  163. c.set((b"branch", b"blie"), b"foo", b"bar")
  164. f = BytesIO()
  165. c.write_to_file(f)
  166. self.assertEqual(b'[branch "blie"]\n\tfoo = bar\n', f.getvalue())
  167. def test_same_line(self) -> None:
  168. cf = self.from_file(b"[branch.foo] foo = bar\n")
  169. self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
  170. def test_quoted_newlines_windows(self) -> None:
  171. cf = self.from_file(
  172. b"[alias]\r\n"
  173. b"c = '!f() { \\\r\n"
  174. b' printf \'[git commit -m \\"%s\\"]\\n\' \\"$*\\" && \\\r\n'
  175. b' git commit -m \\"$*\\"; \\\r\n'
  176. b" }; f'\r\n"
  177. )
  178. self.assertEqual(list(cf.sections()), [(b"alias",)])
  179. self.assertEqual(
  180. b'\'!f() { printf \'[git commit -m "%s"]\n\' "$*" && git commit -m "$*"',
  181. cf.get((b"alias",), b"c"),
  182. )
  183. def test_quoted(self) -> None:
  184. cf = self.from_file(
  185. b"""[gui]
  186. \tfontdiff = -family \\\"Ubuntu Mono\\\" -size 11 -overstrike 0
  187. """
  188. )
  189. self.assertEqual(
  190. ConfigFile(
  191. {
  192. (b"gui",): {
  193. b"fontdiff": b'-family "Ubuntu Mono" -size 11 -overstrike 0',
  194. }
  195. }
  196. ),
  197. cf,
  198. )
  199. def test_quoted_multiline(self) -> None:
  200. cf = self.from_file(
  201. b"""[alias]
  202. who = \"!who() {\\
  203. git log --no-merges --pretty=format:'%an - %ae' $@ | uniq -c | sort -rn;\\
  204. };\\
  205. who\"
  206. """
  207. )
  208. self.assertEqual(
  209. ConfigFile(
  210. {
  211. (b"alias",): {
  212. b"who": (
  213. b"!who() {git log --no-merges --pretty=format:'%an - "
  214. b"%ae' $@ | uniq -c | sort -rn;};who"
  215. )
  216. }
  217. }
  218. ),
  219. cf,
  220. )
  221. def test_set_hash_gets_quoted(self) -> None:
  222. c = ConfigFile()
  223. c.set(b"xandikos", b"color", b"#665544")
  224. f = BytesIO()
  225. c.write_to_file(f)
  226. self.assertEqual(b'[xandikos]\n\tcolor = "#665544"\n', f.getvalue())
  227. def test_windows_path_with_trailing_backslash_unquoted(self) -> None:
  228. """Test that Windows paths ending with escaped backslash are handled correctly."""
  229. # This reproduces the issue from https://github.com/jelmer/dulwich/issues/1088
  230. # A single backslash at the end should actually be a line continuation in strict Git config
  231. # But we want to be more tolerant like Git itself
  232. cf = self.from_file(
  233. b'[core]\n\trepositoryformatversion = 0\n[remote "origin"]\n\turl = C:/Users/test\\\\\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n'
  234. )
  235. self.assertEqual(b"C:/Users/test\\", cf.get((b"remote", b"origin"), b"url"))
  236. self.assertEqual(
  237. b"+refs/heads/*:refs/remotes/origin/*",
  238. cf.get((b"remote", b"origin"), b"fetch"),
  239. )
  240. def test_windows_path_with_trailing_backslash_quoted(self) -> None:
  241. """Test that quoted Windows paths with escaped backslashes work correctly."""
  242. cf = self.from_file(
  243. b'[core]\n\trepositoryformatversion = 0\n[remote "origin"]\n\turl = "C:\\\\Users\\\\test\\\\"\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n'
  244. )
  245. self.assertEqual(b"C:\\Users\\test\\", cf.get((b"remote", b"origin"), b"url"))
  246. self.assertEqual(
  247. b"+refs/heads/*:refs/remotes/origin/*",
  248. cf.get((b"remote", b"origin"), b"fetch"),
  249. )
  250. def test_single_backslash_at_line_end_shows_proper_escaping_needed(self) -> None:
  251. """Test that demonstrates proper escaping is needed for single backslashes."""
  252. # This test documents the current behavior: a single backslash at the end of a line
  253. # is treated as a line continuation per Git config spec. Users should escape backslashes.
  254. # This reproduces the original issue - single backslash causes line continuation
  255. cf = self.from_file(
  256. b'[remote "origin"]\n\turl = C:/Users/test\\\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n'
  257. )
  258. # The result shows that line continuation occurred
  259. self.assertEqual(
  260. b"C:/Users/testfetch = +refs/heads/*:refs/remotes/origin/*",
  261. cf.get((b"remote", b"origin"), b"url"),
  262. )
  263. # The proper way to include a literal backslash is to escape it
  264. cf2 = self.from_file(
  265. b'[remote "origin"]\n\turl = C:/Users/test\\\\\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n'
  266. )
  267. self.assertEqual(b"C:/Users/test\\", cf2.get((b"remote", b"origin"), b"url"))
  268. self.assertEqual(
  269. b"+refs/heads/*:refs/remotes/origin/*",
  270. cf2.get((b"remote", b"origin"), b"fetch"),
  271. )
  272. class ConfigDictTests(TestCase):
  273. def test_get_set(self) -> None:
  274. cd = ConfigDict()
  275. self.assertRaises(KeyError, cd.get, b"foo", b"core")
  276. cd.set((b"core",), b"foo", b"bla")
  277. self.assertEqual(b"bla", cd.get((b"core",), b"foo"))
  278. cd.set((b"core",), b"foo", b"bloe")
  279. self.assertEqual(b"bloe", cd.get((b"core",), b"foo"))
  280. def test_get_boolean(self) -> None:
  281. cd = ConfigDict()
  282. cd.set((b"core",), b"foo", b"true")
  283. self.assertTrue(cd.get_boolean((b"core",), b"foo"))
  284. cd.set((b"core",), b"foo", b"false")
  285. self.assertFalse(cd.get_boolean((b"core",), b"foo"))
  286. cd.set((b"core",), b"foo", b"invalid")
  287. self.assertRaises(ValueError, cd.get_boolean, (b"core",), b"foo")
  288. def test_dict(self) -> None:
  289. cd = ConfigDict()
  290. cd.set((b"core",), b"foo", b"bla")
  291. cd.set((b"core2",), b"foo", b"bloe")
  292. self.assertEqual([(b"core",), (b"core2",)], list(cd.keys()))
  293. self.assertEqual(cd[(b"core",)], {b"foo": b"bla"})
  294. cd[b"a"] = b"b"
  295. self.assertEqual(cd[b"a"], b"b")
  296. def test_items(self) -> None:
  297. cd = ConfigDict()
  298. cd.set((b"core",), b"foo", b"bla")
  299. cd.set((b"core2",), b"foo", b"bloe")
  300. self.assertEqual([(b"foo", b"bla")], list(cd.items((b"core",))))
  301. def test_items_nonexistant(self) -> None:
  302. cd = ConfigDict()
  303. cd.set((b"core2",), b"foo", b"bloe")
  304. self.assertEqual([], list(cd.items((b"core",))))
  305. def test_sections(self) -> None:
  306. cd = ConfigDict()
  307. cd.set((b"core2",), b"foo", b"bloe")
  308. self.assertEqual([(b"core2",)], list(cd.sections()))
  309. def test_set_vs_add(self) -> None:
  310. cd = ConfigDict()
  311. # Test add() creates multivars
  312. cd.add((b"core",), b"foo", b"value1")
  313. cd.add((b"core",), b"foo", b"value2")
  314. self.assertEqual(
  315. [b"value1", b"value2"], list(cd.get_multivar((b"core",), b"foo"))
  316. )
  317. # Test set() replaces values
  318. cd.set((b"core",), b"foo", b"value3")
  319. self.assertEqual([b"value3"], list(cd.get_multivar((b"core",), b"foo")))
  320. self.assertEqual(b"value3", cd.get((b"core",), b"foo"))
  321. class StackedConfigTests(TestCase):
  322. def test_default_backends(self) -> None:
  323. StackedConfig.default_backends()
  324. @skipIf(sys.platform != "win32", "Windows specific config location.")
  325. def test_windows_config_from_path(self) -> None:
  326. from dulwich.config import get_win_system_paths
  327. install_dir = os.path.join("C:", "foo", "Git")
  328. self.overrideEnv("PATH", os.path.join(install_dir, "cmd"))
  329. with patch("os.path.exists", return_value=True):
  330. paths = set(get_win_system_paths())
  331. self.assertEqual(
  332. {
  333. os.path.join(os.environ.get("PROGRAMDATA"), "Git", "config"),
  334. os.path.join(install_dir, "etc", "gitconfig"),
  335. },
  336. paths,
  337. )
  338. @skipIf(sys.platform != "win32", "Windows specific config location.")
  339. def test_windows_config_from_reg(self) -> None:
  340. import winreg
  341. from dulwich.config import get_win_system_paths
  342. self.overrideEnv("PATH", None)
  343. install_dir = os.path.join("C:", "foo", "Git")
  344. with patch("winreg.OpenKey"):
  345. with patch(
  346. "winreg.QueryValueEx",
  347. return_value=(install_dir, winreg.REG_SZ),
  348. ):
  349. paths = set(get_win_system_paths())
  350. self.assertEqual(
  351. {
  352. os.path.join(os.environ.get("PROGRAMDATA"), "Git", "config"),
  353. os.path.join(install_dir, "etc", "gitconfig"),
  354. },
  355. paths,
  356. )
  357. class EscapeValueTests(TestCase):
  358. def test_nothing(self) -> None:
  359. self.assertEqual(b"foo", _escape_value(b"foo"))
  360. def test_backslash(self) -> None:
  361. self.assertEqual(b"foo\\\\", _escape_value(b"foo\\"))
  362. def test_newline(self) -> None:
  363. self.assertEqual(b"foo\\n", _escape_value(b"foo\n"))
  364. class FormatStringTests(TestCase):
  365. def test_quoted(self) -> None:
  366. self.assertEqual(b'" foo"', _format_string(b" foo"))
  367. self.assertEqual(b'"\\tfoo"', _format_string(b"\tfoo"))
  368. def test_not_quoted(self) -> None:
  369. self.assertEqual(b"foo", _format_string(b"foo"))
  370. self.assertEqual(b"foo bar", _format_string(b"foo bar"))
  371. class ParseStringTests(TestCase):
  372. def test_quoted(self) -> None:
  373. self.assertEqual(b" foo", _parse_string(b'" foo"'))
  374. self.assertEqual(b"\tfoo", _parse_string(b'"\\tfoo"'))
  375. def test_not_quoted(self) -> None:
  376. self.assertEqual(b"foo", _parse_string(b"foo"))
  377. self.assertEqual(b"foo bar", _parse_string(b"foo bar"))
  378. def test_nothing(self) -> None:
  379. self.assertEqual(b"", _parse_string(b""))
  380. def test_tab(self) -> None:
  381. self.assertEqual(b"\tbar\t", _parse_string(b"\\tbar\\t"))
  382. def test_newline(self) -> None:
  383. self.assertEqual(b"\nbar\t", _parse_string(b"\\nbar\\t\t"))
  384. def test_quote(self) -> None:
  385. self.assertEqual(b'"foo"', _parse_string(b'\\"foo\\"'))
  386. class CheckVariableNameTests(TestCase):
  387. def test_invalid(self) -> None:
  388. self.assertFalse(_check_variable_name(b"foo "))
  389. self.assertFalse(_check_variable_name(b"bar,bar"))
  390. self.assertFalse(_check_variable_name(b"bar.bar"))
  391. def test_valid(self) -> None:
  392. self.assertTrue(_check_variable_name(b"FOO"))
  393. self.assertTrue(_check_variable_name(b"foo"))
  394. self.assertTrue(_check_variable_name(b"foo-bar"))
  395. class CheckSectionNameTests(TestCase):
  396. def test_invalid(self) -> None:
  397. self.assertFalse(_check_section_name(b"foo "))
  398. self.assertFalse(_check_section_name(b"bar,bar"))
  399. def test_valid(self) -> None:
  400. self.assertTrue(_check_section_name(b"FOO"))
  401. self.assertTrue(_check_section_name(b"foo"))
  402. self.assertTrue(_check_section_name(b"foo-bar"))
  403. self.assertTrue(_check_section_name(b"bar.bar"))
  404. class SubmodulesTests(TestCase):
  405. def testSubmodules(self) -> None:
  406. cf = ConfigFile.from_file(
  407. BytesIO(
  408. b"""\
  409. [submodule "core/lib"]
  410. \tpath = core/lib
  411. \turl = https://github.com/phhusson/QuasselC.git
  412. """
  413. )
  414. )
  415. got = list(parse_submodules(cf))
  416. self.assertEqual(
  417. [
  418. (
  419. b"core/lib",
  420. b"https://github.com/phhusson/QuasselC.git",
  421. b"core/lib",
  422. )
  423. ],
  424. got,
  425. )
  426. def testMalformedSubmodules(self) -> None:
  427. cf = ConfigFile.from_file(
  428. BytesIO(
  429. b"""\
  430. [submodule "core/lib"]
  431. \tpath = core/lib
  432. \turl = https://github.com/phhusson/QuasselC.git
  433. [submodule "dulwich"]
  434. \turl = https://github.com/jelmer/dulwich
  435. """
  436. )
  437. )
  438. got = list(parse_submodules(cf))
  439. self.assertEqual(
  440. [
  441. (
  442. b"core/lib",
  443. b"https://github.com/phhusson/QuasselC.git",
  444. b"core/lib",
  445. )
  446. ],
  447. got,
  448. )
  449. class ApplyInsteadOfTests(TestCase):
  450. def test_none(self) -> None:
  451. config = ConfigDict()
  452. self.assertEqual(
  453. "https://example.com/", apply_instead_of(config, "https://example.com/")
  454. )
  455. def test_apply(self) -> None:
  456. config = ConfigDict()
  457. config.set(("url", "https://samba.org/"), "insteadOf", "https://example.com/")
  458. self.assertEqual(
  459. "https://samba.org/", apply_instead_of(config, "https://example.com/")
  460. )
  461. def test_apply_multiple(self) -> None:
  462. config = ConfigDict()
  463. config.add(("url", "https://samba.org/"), "insteadOf", "https://blah.com/")
  464. config.add(("url", "https://samba.org/"), "insteadOf", "https://example.com/")
  465. self.assertEqual(
  466. [b"https://blah.com/", b"https://example.com/"],
  467. list(config.get_multivar(("url", "https://samba.org/"), "insteadOf")),
  468. )
  469. self.assertEqual(
  470. "https://samba.org/", apply_instead_of(config, "https://example.com/")
  471. )
  472. def test_apply_preserves_case_in_subsection(self) -> None:
  473. """Test that mixed-case URLs (like those with access tokens) are preserved."""
  474. config = ConfigDict()
  475. # GitHub access tokens have mixed case that must be preserved
  476. url_with_token = "https://ghp_AbCdEfGhIjKlMnOpQrStUvWxYz1234567890@github.com/"
  477. config.set(("url", url_with_token), "insteadOf", "https://github.com/")
  478. # Apply the substitution
  479. result = apply_instead_of(config, "https://github.com/jelmer/dulwich.git")
  480. expected = "https://ghp_AbCdEfGhIjKlMnOpQrStUvWxYz1234567890@github.com/jelmer/dulwich.git"
  481. self.assertEqual(expected, result)
  482. # Verify the token case is preserved
  483. self.assertIn("ghp_AbCdEfGhIjKlMnOpQrStUvWxYz1234567890", result)
  484. class CaseInsensitiveConfigTests(TestCase):
  485. def test_case_insensitive(self) -> None:
  486. config = CaseInsensitiveOrderedMultiDict()
  487. config[("core",)] = "value"
  488. self.assertEqual("value", config[("CORE",)])
  489. self.assertEqual("value", config[("CoRe",)])
  490. self.assertEqual([("core",)], list(config.keys()))
  491. def test_multiple_set(self) -> None:
  492. config = CaseInsensitiveOrderedMultiDict()
  493. config[("core",)] = "value1"
  494. config[("core",)] = "value2"
  495. # The second set overwrites the first one
  496. self.assertEqual("value2", config[("core",)])
  497. self.assertEqual("value2", config[("CORE",)])
  498. def test_get_all(self) -> None:
  499. config = CaseInsensitiveOrderedMultiDict()
  500. config[("core",)] = "value1"
  501. config[("CORE",)] = "value2"
  502. config[("CoRe",)] = "value3"
  503. self.assertEqual(
  504. ["value1", "value2", "value3"], list(config.get_all(("core",)))
  505. )
  506. self.assertEqual(
  507. ["value1", "value2", "value3"], list(config.get_all(("CORE",)))
  508. )
  509. def test_delitem(self) -> None:
  510. config = CaseInsensitiveOrderedMultiDict()
  511. config[("core",)] = "value1"
  512. config[("CORE",)] = "value2"
  513. config[("other",)] = "value3"
  514. del config[("core",)]
  515. self.assertNotIn(("core",), config)
  516. self.assertNotIn(("CORE",), config)
  517. self.assertEqual("value3", config[("other",)])
  518. self.assertEqual(1, len(config))
  519. def test_len(self) -> None:
  520. config = CaseInsensitiveOrderedMultiDict()
  521. self.assertEqual(0, len(config))
  522. config[("core",)] = "value1"
  523. self.assertEqual(1, len(config))
  524. config[("CORE",)] = "value2"
  525. self.assertEqual(1, len(config)) # Same key, case insensitive
  526. def test_subsection_case_preserved(self) -> None:
  527. """Test that subsection names preserve their case."""
  528. config = CaseInsensitiveOrderedMultiDict()
  529. # Section names should be case-insensitive, but subsection names should preserve case
  530. config[("url", "https://Example.COM/Path")] = "value1"
  531. # Can retrieve with different case section name
  532. self.assertEqual("value1", config[("URL", "https://Example.COM/Path")])
  533. self.assertEqual("value1", config[("url", "https://Example.COM/Path")])
  534. # But not with different case subsection name
  535. with self.assertRaises(KeyError):
  536. config[("url", "https://example.com/path")]
  537. # Verify the stored key preserves subsection case
  538. stored_keys = list(config.keys())
  539. self.assertEqual(1, len(stored_keys))
  540. self.assertEqual(("url", "https://Example.COM/Path"), stored_keys[0])
  541. config[("other",)] = "value3"
  542. self.assertEqual(2, len(config))
  543. def test_make_from_dict(self) -> None:
  544. original = {("core",): "value1", ("other",): "value2"}
  545. config = CaseInsensitiveOrderedMultiDict.make(original)
  546. self.assertEqual("value1", config[("core",)])
  547. self.assertEqual("value1", config[("CORE",)])
  548. self.assertEqual("value2", config[("other",)])
  549. def test_make_from_self(self) -> None:
  550. config1 = CaseInsensitiveOrderedMultiDict()
  551. config1[("core",)] = "value"
  552. config2 = CaseInsensitiveOrderedMultiDict.make(config1)
  553. self.assertIs(config1, config2)
  554. def test_make_invalid_type(self) -> None:
  555. self.assertRaises(TypeError, CaseInsensitiveOrderedMultiDict.make, "invalid")
  556. def test_get_with_default(self) -> None:
  557. config = CaseInsensitiveOrderedMultiDict()
  558. config[("core",)] = "value"
  559. self.assertEqual("value", config.get(("core",)))
  560. self.assertEqual("value", config.get(("CORE",)))
  561. self.assertEqual("default", config.get(("missing",), "default"))
  562. # Test SENTINEL behavior
  563. result = config.get(("missing",))
  564. self.assertIsInstance(result, CaseInsensitiveOrderedMultiDict)
  565. self.assertEqual(0, len(result))
  566. def test_setdefault(self) -> None:
  567. config = CaseInsensitiveOrderedMultiDict()
  568. # Set new value
  569. result1 = config.setdefault(("core",), "value1")
  570. self.assertEqual("value1", result1)
  571. self.assertEqual("value1", config[("core",)])
  572. # Try to set again with different case - should return existing
  573. result2 = config.setdefault(("CORE",), "value2")
  574. self.assertEqual("value1", result2)
  575. self.assertEqual("value1", config[("core",)])
  576. def test_values(self) -> None:
  577. config = CaseInsensitiveOrderedMultiDict()
  578. config[("core",)] = "value1"
  579. config[("other",)] = "value2"
  580. config[("CORE",)] = "value3" # Overwrites previous core value
  581. self.assertEqual({"value3", "value2"}, set(config.values()))
  582. def test_items_iteration(self) -> None:
  583. config = CaseInsensitiveOrderedMultiDict()
  584. config[("core",)] = "value1"
  585. config[("other",)] = "value2"
  586. config[("CORE",)] = "value3"
  587. items = list(config.items())
  588. self.assertEqual(3, len(items))
  589. self.assertEqual((("core",), "value1"), items[0])
  590. self.assertEqual((("other",), "value2"), items[1])
  591. self.assertEqual((("CORE",), "value3"), items[2])
  592. def test_str_keys(self) -> None:
  593. config = CaseInsensitiveOrderedMultiDict()
  594. config["core"] = "value"
  595. self.assertEqual("value", config["CORE"])
  596. self.assertEqual("value", config["CoRe"])
  597. def test_nested_tuple_keys(self) -> None:
  598. config = CaseInsensitiveOrderedMultiDict()
  599. config[("branch", "master")] = "value"
  600. # Section names are case-insensitive
  601. self.assertEqual("value", config[("BRANCH", "master")])
  602. self.assertEqual("value", config[("Branch", "master")])
  603. # But subsection names are case-sensitive
  604. with self.assertRaises(KeyError):
  605. config[("branch", "MASTER")]
  606. class ConfigFileSetTests(TestCase):
  607. def test_set_replaces_value(self) -> None:
  608. # Test that set() replaces the value instead of appending
  609. cf = ConfigFile()
  610. cf.set((b"core",), b"sshCommand", b"ssh -i ~/.ssh/id_rsa1")
  611. cf.set((b"core",), b"sshCommand", b"ssh -i ~/.ssh/id_rsa2")
  612. # Should only have one value
  613. self.assertEqual(b"ssh -i ~/.ssh/id_rsa2", cf.get((b"core",), b"sshCommand"))
  614. # When written to file, should only have one entry
  615. f = BytesIO()
  616. cf.write_to_file(f)
  617. content = f.getvalue()
  618. self.assertEqual(1, content.count(b"sshCommand"))
  619. self.assertIn(b"sshCommand = ssh -i ~/.ssh/id_rsa2", content)
  620. self.assertNotIn(b"id_rsa1", content)