@@ -67,6 +67,29 @@ def pytest_addoption(
67
67
group .addoption ("--e3-cov-rewrite" , nargs = 2 , help = "Use e3 fixtures and reporting" )
68
68
69
69
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
+
70
93
@pytest .fixture (autouse = True )
71
94
def env_protect (request : pytest .FixtureRequest ) -> None :
72
95
"""Protection against environment change.
@@ -88,14 +111,7 @@ def env_protect(request: pytest.FixtureRequest) -> None:
88
111
if "E3_HOSTNAME" in os .environ :
89
112
del os .environ ["E3_HOSTNAME" ]
90
113
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 ()
99
115
100
116
def restore_env () -> None :
101
117
Env ().restore ()
0 commit comments