Skip to content

Commit 769ca1e

Browse files
authored
Merge pull request #1657 from EliahKagan/envcase-doc
Better document env_case test/fixture and cwd
2 parents d6d8ecd + c547555 commit 769ca1e

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

git/util.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ def wrapper(self: "Remote", *args: Any, **kwargs: Any) -> T:
150150

151151
@contextlib.contextmanager
152152
def cwd(new_dir: PathLike) -> Generator[PathLike, None, None]:
153-
"""Context manager to temporarily change directory. Not reentrant."""
153+
"""Context manager to temporarily change directory.
154+
155+
This is similar to contextlib.chdir introduced in Python 3.11, but the context
156+
manager object returned by a single call to this function is not reentrant."""
154157
old_dir = os.getcwd()
155158
os.chdir(new_dir)
156159
try:

test/fixtures/env_case.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
# Steps 3 and 4 for test_it_avoids_upcasing_unrelated_environment_variable_names.
2+
13
import subprocess
24
import sys
35

6+
# Step 3a: Import the module, in case that upcases the environment variable name.
47
import git
58

69

710
_, working_dir, env_var_name = sys.argv
811

9-
# Importing git should be enough, but this really makes sure Git.execute is called.
12+
# Step 3b: Use Git.execute explicitly, in case that upcases the environment variable.
13+
# (Importing git should be enough, but this ensures Git.execute is called.)
1014
repo = git.Repo(working_dir) # Hold the reference.
1115
git.Git(repo.working_dir).execute(["git", "version"])
1216

17+
# Step 4: Create the non-Python grandchild that accesses the variable case-sensitively.
1318
print(subprocess.check_output(["set", env_var_name], shell=True, text=True))

test/test_git.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,23 @@ def test_it_avoids_upcasing_unrelated_environment_variable_names(self):
9898
old_name = "28f425ca_d5d8_4257_b013_8d63166c8158"
9999
if old_name == old_name.upper():
100100
raise RuntimeError("test bug or strange locale: old_name invariant under upcasing")
101-
os.putenv(old_name, "1") # It has to be done this lower-level way to set it lower-case.
102101

102+
# Step 1: Set the environment variable in this parent process. Because os.putenv is a thin
103+
# wrapper around a system API, os.environ never sees the variable in this parent
104+
# process, so the name is not upcased even on Windows.
105+
os.putenv(old_name, "1")
106+
107+
# Step 2: Create the child process that inherits the environment variable. The child uses
108+
# GitPython, and we are testing that it passes the variable with the exact original
109+
# name to its own child process (the grandchild).
103110
cmdline = [
104111
sys.executable,
105-
fixture_path("env_case.py"),
112+
fixture_path("env_case.py"), # Contains steps 3 and 4.
106113
self.rorepo.working_dir,
107114
old_name,
108115
]
109-
pair_text = subprocess.check_output(cmdline, shell=False, text=True)
116+
pair_text = subprocess.check_output(cmdline, shell=False, text=True) # Run steps 3 and 4.
117+
110118
new_name = pair_text.split("=")[0]
111119
self.assertEqual(new_name, old_name)
112120

0 commit comments

Comments
 (0)