Skip to content

Commit 5b7e8e8

Browse files
committed
Merge branch 'mr/ramonat/preserve-git-config-with-e3-pytest' into 'master'
Preserve Git environment variables when using the pytest plugin See merge request it/e3-core!148
2 parents edfd526 + 45fbbf3 commit 5b7e8e8

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/e3/pytest.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ def pytest_addoption(
6767
group.addoption("--e3-cov-rewrite", nargs=2, help="Use e3 fixtures and reporting")
6868

6969

70+
def set_git_env_config() -> None:
71+
"""Set the Git environment configuration.
72+
73+
This function is called by the env_protect fixture to ensure that
74+
the Git environment configuration is set to a known state.
75+
76+
Set the `init.defaultbranch` to `default_branch` to ensure that
77+
the default branch is not assumed to be `master` or `main`.
78+
79+
The best practice when writing test is to either explicitely name the branch
80+
or to get the value with `git branch --show-current`
81+
"""
82+
# If GIT_CONFIG_COUNT is already set, we increment it to avoid overwriting
83+
# existing configuration keys.
84+
if "GIT_CONFIG_COUNT" not in os.environ:
85+
git_config_count = 0
86+
else:
87+
git_config_count = int(os.environ["GIT_CONFIG_COUNT"])
88+
os.environ["GIT_CONFIG_COUNT"] = str(git_config_count + 1)
89+
os.environ[f"GIT_CONFIG_KEY_{git_config_count}"] = "init.defaultbranch"
90+
os.environ[f"GIT_CONFIG_VALUE_{git_config_count}"] = "default_branch"
91+
92+
7093
@pytest.fixture(autouse=True)
7194
def env_protect(request: pytest.FixtureRequest) -> None:
7295
"""Protection against environment change.
@@ -88,14 +111,7 @@ def env_protect(request: pytest.FixtureRequest) -> None:
88111
if "E3_HOSTNAME" in os.environ:
89112
del os.environ["E3_HOSTNAME"]
90113

91-
# Set environment variables for the default Git branch to
92-
# ensure that we do not assume that the default branch is
93-
# named master. The best practice when writing test is
94-
# to either explicitely name the branch or to get the
95-
# value with `git branch --show-current`
96-
os.environ["GIT_CONFIG_COUNT"] = "1"
97-
os.environ["GIT_CONFIG_KEY_0"] = "init.defaultbranch"
98-
os.environ["GIT_CONFIG_VALUE_0"] = "default_branch"
114+
set_git_env_config()
99115

100116
def restore_env() -> None:
101117
Env().restore()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
3+
from e3.pytest import set_git_env_config
4+
5+
6+
def test_env_protect_test_git_config() -> None:
7+
"""Test to ensure git config is protected.
8+
9+
GIT_CONFIG_COUNT can be already set by the environment, we need to protect
10+
the previous values.
11+
"""
12+
os.environ["GIT_CONFIG_COUNT"] = "11"
13+
os.environ["GIT_CONFIG_KEY_10"] = "color.diff.meta"
14+
os.environ["GIT_CONFIG_VALUE_10"] = "42"
15+
16+
set_git_env_config()
17+
18+
# After env_protect runs, the git config should be reset to default values
19+
assert os.environ.get("GIT_CONFIG_COUNT") == "12"
20+
assert os.environ.get("GIT_CONFIG_KEY_10") == "color.diff.meta"
21+
assert os.environ.get("GIT_CONFIG_VALUE_10") == "42"
22+
23+
assert os.environ.get("GIT_CONFIG_KEY_11") == "init.defaultbranch"
24+
assert os.environ.get("GIT_CONFIG_VALUE_11") == "default_branch"

0 commit comments

Comments
 (0)