for line in f.read_text().strip().splitlines()
)
env |= self.environment
+ env |= finalize_git_config(self.proxy_url, env)
return env
return term if sys.stderr.isatty() else "dumb"
+def finalize_git_config(proxy_url: Optional[str], env: dict[str, str]) -> dict[str, str]:
+ if proxy_url is None:
+ return {}
+
+ try:
+ cnt = int(env.get("GIT_CONFIG_COUNT", "0"))
+ except ValueError:
+ raise ValueError("GIT_CONFIG_COUNT environment variable must be set to a valid integer")
+
+ # Override HTTP/HTTPS proxy in case its set in .gitconfig to a different value than proxy_url.
+ # No need to override http.proxy / https.proxy if set in a previous GIT_CONFIG_* variable since
+ # the last setting always wins.
+ return {
+ "GIT_CONFIG_COUNT": str(cnt + 2),
+ f"GIT_CONFIG_KEY_{cnt}": "http.proxy",
+ f"GIT_CONFIG_VALUE_{cnt}": proxy_url,
+ f"GIT_CONFIG_KEY_{cnt + 1}": "https.proxy",
+ f"GIT_CONFIG_VALUE_{cnt + 1}": proxy_url,
+ }
+
+
def yes_no(b: bool) -> str:
return "yes" if b else "no"
assert "TestValue2" not in sub.environment
+def test_proxy(tmp_path: Path) -> None:
+ d = tmp_path
+
+ # Verify environment variables are set correctly when GIT_CONFIG_COUNT is not set
+ (d / "mkosi.conf").write_text(
+ """\
+ [Build]
+ ProxyUrl=http://proxy:8080
+ """
+ )
+
+ with chdir(d):
+ _, _, [config] = parse_config()
+
+ expected = {
+ "GIT_CONFIG_COUNT": "2",
+ "GIT_CONFIG_KEY_0": "http.proxy",
+ "GIT_CONFIG_VALUE_0": "http://proxy:8080",
+ "GIT_CONFIG_KEY_1": "https.proxy",
+ "GIT_CONFIG_VALUE_1": "http://proxy:8080",
+ }
+
+ # Only check values for keys from expected, as config.environment contains other items as well
+ assert {k: config.finalize_environment()[k] for k in expected.keys()} == expected
+
+ (d / "mkosi.conf").write_text(
+ """\
+ [Build]
+ ProxyUrl=http://proxy:8080
+ Environment=GIT_CONFIG_COUNT=1
+ GIT_CONFIG_KEY_0=user.name
+ GIT_CONFIG_VALUE_0=bob
+ """
+ )
+
+ with chdir(d):
+ _, _, [config] = parse_config()
+
+ expected = {
+ "GIT_CONFIG_COUNT": "3",
+ "GIT_CONFIG_KEY_0": "user.name",
+ "GIT_CONFIG_VALUE_0": "bob",
+ "GIT_CONFIG_KEY_1": "http.proxy",
+ "GIT_CONFIG_VALUE_1": "http://proxy:8080",
+ "GIT_CONFIG_KEY_2": "https.proxy",
+ "GIT_CONFIG_VALUE_2": "http://proxy:8080",
+ }
+
+ # Only check values for keys from expected, as config.environment contains other items as well
+ assert {k: config.finalize_environment()[k] for k in expected.keys()} == expected
+
+
def test_mkosi_version_executable(tmp_path: Path) -> None:
d = tmp_path