GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — GitPython/test/test_config.py at main · gitpython-developers/GitPython Expand file tree/ Copy pathtest_config.pyMore file actions 385 lines (344 loc) · 16.7 KB / Copy pathtest_config.pyFile metadata and controls385 lines (344 loc) · 16.7 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 """The configuration facade follows Git's parser and mutation semantics.""" import configparser from collections import UserList from io import BytesIO import os import time from unittest import mock import pytest from git import Git, GitCommandError, GitConfigParser, Repo from git.config import SectionConstraint from test.lib import fixture_path from test.lib.fixtures import read_only_fixture, writable_fixture @pytest.fixture def config_directory(request): scenario = "values" if request.node.originalname == "test_config_values_and_mutations" else "empty" with writable_fixture("config.sh", scenario) as fixture: yield fixture.path() @pytest.mark.parametrize( "text, expected", [ ("9007199254740993", 9007199254740993), ("9223372036854775807", 9223372036854775807), ("-9007199254740993", -9007199254740993), ("11.11", 11.11), ("1e3", 1000.0), ("nan", "nan"), ("NaN", "NaN"), ("true", True), ("false", False), ], ) def test_numeric_config_conversion_preserves_precision_and_types(text, expected): with GitConfigParser(BytesIO(("[values]\nnumber = %s\n" % text).encode("ascii"))) as config: value = config.get_value("values", "number") assert value == expected assert type(value) is type(expected) assert config.get_values("values", "number") == [expected] @pytest.mark.parametrize("stream", [False, True]) def test_config_values_and_mutations(config_directory, stream): path = config_directory / "config" content = path.read_bytes() source = BytesIO(content) if stream else path with GitConfigParser(source, read_only=False) as config: assert config.sections() == ["core", 'remote "Origin"'] assert config.get_values("CORE", "NUMBER") == [1, 2] assert config.getboolean("core", "flag") is True assert config.get_value("core", "flag") == "" config.set_value("core", "number", 3) config.add_value("core", "number", 4) assert config.items_all("core") == [("flag", [None]), ("number", ["3", "4"])] config.set_value("core", "empty", "") assert config.getboolean("core", "empty") is False config.rename_section('remote "Origin"', 'remote "Other"') assert config.get('REMOTE "Other"', "URL") == "first" assert config.remove_option("core", "empty") assert not config.remove_option("core", "empty") assert config.remove_section('remote "Other"') config.write() assert GitConfigParser(source).get_values("core", "number") == [3, 4] assert (source.getvalue() if stream else path.read_bytes()).startswith(b"[CoRe]\nFlag\n") @pytest.mark.parametrize("stream", [False, True]) def test_config_preserves_non_utf8_subsections_and_values(stream): path = read_only_fixture("config-bytes.sh") source = BytesIO((path / "config").read_bytes()) if stream else path / "config" assert ( Git() .config("list", "--file", str(path / "config"), "--null", stdout_as_string=False) .endswith(b"remote.origin-\xff.url\ntarget-\xfe\0") ) with GitConfigParser(source) as config: assert 'remote "origin-\udcff"' in config.sections() assert config.get('remote "origin-\udcff"', "url") == "target-\udcfe" with Repo(path / "repo") as repo: remote = repo.remotes[0] assert remote.name == "origin-\udcff" assert remote.url == "target-\udcfe" @pytest.mark.skipif(os.name == "nt", reason="Windows command arguments cannot carry arbitrary bytes") @pytest.mark.parametrize("stream", [False, True]) def test_config_mutations_preserve_non_utf8_values(stream): with writable_fixture("config-bytes.sh") as fixture: path = fixture.path() / "config" source = BytesIO(path.read_bytes()) if stream else path with GitConfigParser(source, read_only=False) as config: section = 'remote "origin-\udcff"' assert config.get(section, "url") == "target-\udcfe" config.set_value(section, "url", "updated-\udcfd") config.add_value(section, "url", b"bytes-\xfc") assert config.get_values(section, "url") == ["updated-\udcfd", "bytes-\udcfc"] content = source.getvalue() if stream else path.read_bytes() assert b'"origin-\xff"' in content assert b"updated-\xfd" in content assert b"bytes-\xfc" in content def test_config_includes_and_repository_conditions(): path = read_only_fixture("config.sh", "includes") with Repo(path / "repo") as repo: assert GitConfigParser(path / "config", repo=repo).get_values("values", "source") == [ "before", "after", "included", "included", ] assert GitConfigParser(path / "config", merge_includes=False).get_values("values", "source") == [ "before", "after", ] @pytest.mark.parametrize("directory, matches", [("target", True), ("other", False)]) def test_conditional_includes_expand_home_in_git_dir_pattern(monkeypatch, directory, matches): path = read_only_fixture("config.sh", "home-includes") home = path / "home" # Native Windows separators must reach Git's home expansion unchanged. monkeypatch.setenv("HOME", str(home)) monkeypatch.setenv("USERPROFILE", str(home)) with Repo(home / directory / "repo") as repo: expected = b"includeif.gitdir:~/target/.path\n./included\0" if matches: expected += b"values.source\nincluded\0" assert ( repo.git.config("list", "--null", "--includes", "--file", str(path / "config"), stdout_as_string=False) == expected ) with GitConfigParser(path / "config", repo=repo) as config: assert config.get('includeIf "gitdir:~/target/"', "path") == "./included" assert config.has_section("values") is matches if matches: assert config.get_value("values", "source") == "included" with GitConfigParser(path / "config", repo=repo, merge_includes=False) as config: assert config.sections() == ['includeif "gitdir:~/target/"'] @pytest.mark.parametrize("stream", [False, True]) def test_option_after_section_header_is_read_like_git(stream): path = read_only_fixture("config.sh", "inline-headers") / "config" assert Git().config("list", "--null", "--file", str(path), stdout_as_string=False) == ( b"submodule.lib.path\nlib\0" b"submodule.lib.url\nhttps://good.example/lib\0" b"submodule.lib.url\nhttps://evil.example/lib\0" b"a.x]y.k\nv\0b.flag\0c.k\nv ]\0d.k\nmulti line\0g.k\nv\0" ) source = BytesIO(path.read_bytes()) if stream else path with GitConfigParser(source) as config: assert config.get_value('submodule "lib"', "url") == "https://evil.example/lib" assert config.get_values('submodule "lib"', "url") == ["https://good.example/lib", "https://evil.example/lib"] assert config.get_value('a "x]y"', "k") == "v" assert config.getboolean("b", "flag") is True assert config.get_value("c", "k") == "v ]" assert config.get_value("d", "k") == "multi line" assert config.get_value("g", "k") == "v" # Git's enumeration omits empty sections; the facade cannot expose them. for section in ("e", "f"): with pytest.raises(configparser.NoSectionError): config.items(section) assert config.sections() == ['submodule "lib"', 'a "x]y"', "b", "c", "d", "g"] @pytest.mark.parametrize( "case, section, key", [ ("multiple", "third", "third.k"), ("escaped-quote", 'a "x\\"]y"', 'a.x"]y.k'), ("unbalanced-quote", None, None), ("missing-bracket", None, None), ("trailing-bracket", None, None), ("invalid-option", None, None), ("invalid-section", None, None), ], ) def test_inline_section_headers_match_git_validation(case, section, key): path = read_only_fixture("config.sh", "inline-header-cases", case) / "config" if section is None: with pytest.raises(GitCommandError, match="bad config line") as baseline: Git().config("list", "--file", str(path)) with GitConfigParser(path) as config, pytest.raises(configparser.ParsingError) as error: config.read() assert isinstance(error.value.__cause__, GitCommandError) assert error.value.__cause__.status == baseline.value.status return assert Git().config("list", "--null", "--file", str(path)) == key + "\nv\0" with GitConfigParser(path) as config: assert config.sections() == [section] assert config.get_value(section, "k") == "v" def test_config_multiple_files_and_missing_values(): tmp_path = read_only_fixture("config.sh", "multiple") one, two = tmp_path / "one", tmp_path / "two" reader = GitConfigParser(UserList([one, tmp_path / "absent", two])) assert reader.get_values("values", "number") == [1, 2] assert reader.getint("values", "number") == 2 assert reader.get_value("absent", "key", 5) == 5 assert reader.get("values", "absent", fallback="default") == "default" with pytest.raises(configparser.NoSectionError): reader.get("absent", "key") with pytest.raises(configparser.NoOptionError): reader.get("values", "absent") with pytest.raises(OSError): reader.set_value("values", "number", 3) with pytest.raises(ValueError): GitConfigParser([one, two], read_only=False) @pytest.mark.parametrize( "value", ["--file=outside", ' first # ; " \\ last ', "line one\nline two", "tabs\tand\bbackspace"] ) def test_config_values_are_literals(config_directory, value): path = config_directory / "config" with GitConfigParser(path, read_only=False) as config: config.set_value('remote "strange.name"', "url", value) assert config.get('remote "strange.name"', "url") == value assert Git().config("get", "--file", str(path), "remote.strange.name.url") == value.rstrip("\n") @pytest.mark.parametrize( "section, option, value", [ ("--file=outside", "name", "value"), ("user\n[other]", "name", "value"), ("user", "--file", "value"), ("user", "name\nother", "value"), ("user", "name", "value\0other"), ("user", "name", "value\rother"), ("user", "name", None), ], ) def test_config_rejects_invalid_fields_before_git(config_directory, section, option, value): config = GitConfigParser(config_directory / "config", read_only=False) with mock.patch.object(Git, "_call_process_safe", side_effect=AssertionError("Git must not run")): with pytest.raises(ValueError): config.set_value(section, option, value) def test_config_writers_use_git_locking(config_directory): path = config_directory / "config" first = GitConfigParser(path, read_only=False) second = GitConfigParser(path, read_only=False) first.set_value("values", "one", 1) second.set_value("values", "two", 2) assert GitConfigParser(path).options("values") == ["one", "two"] lock = config_directory / "config.lock" lock.write_text("reserved") with pytest.raises(OSError): first.set_value("values", "one", 3) assert GitConfigParser(path).get_value("values", "one") == 1 @pytest.mark.parametrize( "scenario", [ "missing", pytest.param("unwritable", marks=pytest.mark.skipif(os.name != "posix", reason="Requires POSIX permissions")), ], ) def test_config_mutation_propagates_non_contention_lock_errors(scenario): # Native fixture copies reset directory permissions, losing the unwritable state. with writable_fixture("config.sh", "lock-errors", scenario, execute=scenario == "unwritable") as fixture: parent = fixture.path().resolve() / "parent" path = parent / "config" try: if scenario == "unwritable" and os.access(parent, os.W_OK): pytest.skip("Current user can write to read-only directories") with pytest.raises(GitCommandError) as baseline: Git().config("set", "--file", str(path), "--all", "--", "values.number", "1") assert "could not lock config file" in baseline.value.stderr # Writers acquire Git's lock during each mutation, not at construction. writer = GitConfigParser(path, read_only=False) with mock.patch.object(time, "sleep", side_effect=AssertionError("Must not retry")) as sleep: with pytest.raises(OSError) as error: writer.set_value("values", "number", 1) sleep.assert_not_called() cause = error.value.__cause__ assert isinstance(cause, GitCommandError) assert (cause.status, cause.stderr) == (baseline.value.status, baseline.value.stderr) assert str(error.value) == cause.stderr assert not path.exists() assert not path.with_suffix(".lock").exists() finally: if scenario == "unwritable": parent.chmod(0o700) def test_config_section_constraint(config_directory): with SectionConstraint(GitConfigParser(config_directory / "config", read_only=False), 'remote "origin"') as remote: remote.set_value("url", "https://example.com/repo") remote.add_value("url", "ssh://example.com/repo") assert remote.get_values("url") == ["https://example.com/repo", "ssh://example.com/repo"] assert remote.options() == ["url"] def test_config_invalid_syntax_is_rejected_by_git(): path = read_only_fixture("config.sh", "invalid") / "config" with pytest.raises(configparser.ParsingError): GitConfigParser(path).read() def test_config_stream_relative_include_has_no_file_base(): with pytest.raises(configparser.ParsingError) as error: GitConfigParser(BytesIO(b"[include]\npath = relative\n")).read() assert isinstance(error.value.__cause__, GitCommandError) assert "relative config includes must come from files" in error.value.__cause__.stderr @pytest.mark.parametrize( "case, expected", [ (0, "one two"), (1, "one two"), (2, "one two\tthree"), (3, "one two # three"), (4, "value "), (5, "value "), (6, "value \t"), (7, "one two "), (8, None), (9, "val\\"), ], ) def test_continued_values_follow_git_reads_and_writes(case, expected): with writable_fixture("config.sh", "continued-values", case) as fixture: path = fixture.path() / "config" baseline = fixture.path() / "baseline" if expected is None: with pytest.raises(GitCommandError, match="bad config line"): Git().config("get", "--file", str(baseline), "a.k") with GitConfigParser(path) as config, pytest.raises(configparser.ParsingError): config.get_value("a", "k") return assert Git().config("get", "--null", "--file", str(baseline), "a.k") == expected + "\0" with GitConfigParser(path, read_only=False) as config: assert config.get_value("a", "k") == expected config.set_value("other", "value", "updated") Git().config("set", "--file", str(baseline), "--all", "--", "other.value", "updated") assert path.read_bytes() == baseline.read_bytes() # Git appends the section header into a dangling EOF continuation. dangling = case in (4, 5, 6, 7) written_section = "a" if dangling else "other" after = expected + "[other]" if dangling else expected assert Git().config("get", "--null", "--file", str(baseline), "a.k") == after + "\0" with GitConfigParser(path) as config: assert config.get_value("a", "k") == after assert config.get_value(written_section, "value") == "updated" @pytest.mark.parametrize( "option, expected", [ ("hasnewline", "first\nsecond"), ("hastab", "first\tsecond"), ("hasbackslash", "foo\\bar"), ("hasquote", 'ab"cd'), ("hastrailingbackslash", "word\\"), ("ordinary", "hello world"), ("withcolon", "host:port"), ("withequals", "key=value"), ("withhash", "value"), ("hasunknownescape", None), ("haswindowspath", None), ], ) def test_config_with_unquoted_escapes(option, expected): path = read_only_fixture("config.sh", "unquoted-escapes", fixture_path("git_config_unquoted_escapes")) / option if expected is None: with pytest.raises(GitCommandError, match="bad config line"): Git().config("get", "--file", str(path), "custom." + option) with GitConfigParser(path) as config, pytest.raises(configparser.ParsingError): config.get("custom", option) return assert Git().config("get", "--null", "--file", str(path), "custom." + option) == expected + "\0" with GitConfigParser(path) as config: assert config.get("custom", option) == expected |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |