|
13 | 13 |
|
14 | 14 | import pytest |
15 | 15 |
|
16 | | -from git import GitConfigParser |
| 16 | +from git import GitConfigParser, Repo |
17 | 17 | from git.compat import defenc |
18 | 18 | from git.config import _OMD, cp |
19 | 19 | from git.util import cwd, rmfile |
@@ -103,6 +103,102 @@ def test_includes_order(self): |
103 | 103 | except AssertionError as e: |
104 | 104 | raise SkipTest("Known failure -- included values are not in effect right away") from e |
105 | 105 |
|
| 106 | + @with_rw_directory |
| 107 | + def test_case_insensitive_names(self, rw_dir): |
| 108 | + config_path = osp.join(rw_dir, "config") |
| 109 | + with open(config_path, "wb") as config_file: |
| 110 | + config_file.write( |
| 111 | + b"[core]\n\tBigName = 1\n" |
| 112 | + b"[CoRe]\n\tbigname = 2\n\tFlag\n" |
| 113 | + b'[REMOTE "Origin"]\n\tUrl = upper\n' |
| 114 | + b'[remote "origin"]\n\tURL = lower\n' |
| 115 | + ) |
| 116 | + |
| 117 | + with GitConfigParser(config_path) as config: |
| 118 | + for section in ("core", "CORE", "CoRe"): |
| 119 | + for option in ("BigName", "bigname", "BIGNAME"): |
| 120 | + self.assertTrue(config.has_section(section)) |
| 121 | + self.assertTrue(config.has_option(section, option)) |
| 122 | + self.assertEqual(config.get(section, option), "2") |
| 123 | + self.assertEqual(config.getint(section, option), 2) |
| 124 | + self.assertEqual(config.get_value(section, option), 2) |
| 125 | + self.assertEqual(config.get_values(section, option), [1, 2]) |
| 126 | + self.assertIs(config.getboolean(section, "FLAG"), True) |
| 127 | + self.assertEqual(config.sections(), ["core", 'REMOTE "Origin"', 'remote "origin"']) |
| 128 | + self.assertEqual(config.items("CORE"), [("BigName", "2"), ("Flag", None)]) |
| 129 | + self.assertEqual(config.items_all("CORE"), [("BigName", ["1", "2"]), ("Flag", [None])]) |
| 130 | + self.assertIn("BigName", config.options("CORE")) |
| 131 | + self.assertEqual(config.get('remote "Origin"', "URL"), "upper") |
| 132 | + self.assertEqual(config.get('REMOTE "origin"', "url"), "lower") |
| 133 | + self.assertFalse(config.has_section('remote "ORIGIN"')) |
| 134 | + with self.assertRaises(cp.NoSectionError): |
| 135 | + config.get('remote "ORIGIN"', "url") |
| 136 | + |
| 137 | + git_config = ["git", "config", "--file", config_path] |
| 138 | + self.assertEqual(subprocess.check_output(git_config + ["--get-all", "CORE.BIGNAME"]), b"1\n2\n") |
| 139 | + self.assertEqual(subprocess.check_output(git_config + ["--get", "remote.Origin.URL"]), b"upper\n") |
| 140 | + self.assertEqual(subprocess.check_output(git_config + ["--get", "REMOTE.origin.url"]), b"lower\n") |
| 141 | + |
| 142 | + @with_rw_directory |
| 143 | + def test_case_insensitive_writes_preserve_spelling(self, rw_dir): |
| 144 | + config_path = osp.join(rw_dir, "config") |
| 145 | + content = b'[CoRe]\n\tBigName = 1\n[REMOTE "Origin"]\n\tUrl = upper\n' |
| 146 | + with open(config_path, "wb") as config_file: |
| 147 | + config_file.write(content) |
| 148 | + |
| 149 | + with GitConfigParser(config_path, read_only=False) as config: |
| 150 | + config.set_value("core", "bigname", 1) |
| 151 | + with open(config_path, "rb") as config_file: |
| 152 | + self.assertEqual(config_file.read(), content) |
| 153 | + with self.assertRaises(cp.DuplicateSectionError): |
| 154 | + config.add_section("CORE") |
| 155 | + config.set("CORE", "BIGNAME", "3") |
| 156 | + config.add_value("core", "bigname", 4) |
| 157 | + config.set_value("CORE", "NewKey", "new") |
| 158 | + self.assertEqual(config.items_all("core"), [("BigName", ["3", "4"]), ("NewKey", ["new"])]) |
| 159 | + self.assertEqual(config.get_values("CORE", "BIGNAME"), [3, 4]) |
| 160 | + self.assertTrue(config.remove_option("CORE", "NEWKEY")) |
| 161 | + self.assertFalse(config.has_option("core", "newkey")) |
| 162 | + config.set_value("core", "newkey", "again") |
| 163 | + self.assertIn(("newkey", "again"), config.items("CORE")) |
| 164 | + self.assertTrue(config.remove_option("CORE", "NEWKEY")) |
| 165 | + config.rename_section('remote "Origin"', 'Remote "Other"') |
| 166 | + self.assertEqual(config.get('REMOTE "Other"', "URL"), "upper") |
| 167 | + self.assertTrue(config.remove_section('REMOTE "Other"')) |
| 168 | + |
| 169 | + with open(config_path, "rb") as config_file: |
| 170 | + self.assertEqual(config_file.read(), b"[CoRe]\n\tBigName = 3\n\tBigName = 4\n") |
| 171 | + with GitConfigParser(config_path) as config: |
| 172 | + self.assertEqual(config.get_values("CORE", "bigname"), [3, 4]) |
| 173 | + |
| 174 | + @with_rw_directory |
| 175 | + def test_case_insensitive_includes_and_remotes(self, rw_dir): |
| 176 | + with Repo.init(rw_dir) as repo: |
| 177 | + config_path = osp.join(repo.git_dir, "config") |
| 178 | + with open(config_path, "ab") as config_file: |
| 179 | + config_file.write( |
| 180 | + b'[REMOTE "Origin"]\n\tURL = upper\n' |
| 181 | + b'[Remote "origin"]\n\tUrl = lower\n' |
| 182 | + b"[core]\n\tBigName = 1\n" |
| 183 | + b"[INCLUDE]\n\tPaTh = included\n" |
| 184 | + b'[INCLUDEIF "onbranch:*"]\n\tPATH = conditional\n' |
| 185 | + b'[INCLUDEIF "ONBRANCH:*"]\n\tpath = wrong-case\n' |
| 186 | + ) |
| 187 | + for filename, content in ( |
| 188 | + ("included", b"[CORE]\n\tBIGNAME = 2\n"), |
| 189 | + ("conditional", b"[core]\n\tBranchName = 3\n"), |
| 190 | + ("wrong-case", b"[core]\n\tbigname = 4\n"), |
| 191 | + ): |
| 192 | + with open(osp.join(repo.git_dir, filename), "wb") as config_file: |
| 193 | + config_file.write(content) |
| 194 | + |
| 195 | + with repo.config_reader("repository") as config: |
| 196 | + self.assertEqual(config.get_values("CORE", "bigname"), [1, 2]) |
| 197 | + self.assertEqual(config.get_value("CORE", "branchname"), 3) |
| 198 | + self.assertEqual([remote.name for remote in repo.remotes], ["Origin", "origin"]) |
| 199 | + self.assertEqual(repo.remote("Origin").config_reader.get("url"), "upper") |
| 200 | + self.assertEqual(repo.remote("origin").config_reader.get("URL"), "lower") |
| 201 | + |
106 | 202 | @with_rw_directory |
107 | 203 | def test_lock_reentry(self, rw_dir): |
108 | 204 | fpl = osp.join(rw_dir, "l") |
@@ -1119,6 +1215,9 @@ def test_setlast(self): |
1119 | 1215 | omd.setlast("key", "value1") |
1120 | 1216 | self.assertEqual(omd["key"], "value1") |
1121 | 1217 | self.assertEqual(omd.getall("key"), ["value1"]) |
1122 | | - omd.setlast("key", "value2") |
| 1218 | + omd.setlast("KEY", "value2") |
1123 | 1219 | self.assertEqual(omd["key"], "value2") |
1124 | 1220 | self.assertEqual(omd.getall("key"), ["value2"]) |
| 1221 | + omd.clear() |
| 1222 | + omd.setall("KEY", ["value3"]) |
| 1223 | + self.assertEqual(omd.items_all(), [("KEY", ["value3"])]) |
0 commit comments