Skip to content

Better document env_case test/fixture and cwd #1657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Better explain the env_case test
This expands and adds comments in
test_it_avoids_upcasing_unrelated_environment_variable_names and
its supporting fixture env_case.py so it is clear exactly what is
being tested and how/why the test works to test it.
  • Loading branch information
EliahKagan committed Sep 12, 2023
commit 74d9c08bc4b8a61cd9f31e2294e1ff74d7b2be08
7 changes: 6 additions & 1 deletion test/fixtures/env_case.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Steps 3 and 4 for test_it_avoids_upcasing_unrelated_environment_variable_names.

import subprocess
import sys

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


_, working_dir, env_var_name = sys.argv

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

# Step 4: Create the non-Python grandchild that accesses the variable case-sensitively.
print(subprocess.check_output(["set", env_var_name], shell=True, text=True))
14 changes: 12 additions & 2 deletions test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,25 @@ def test_it_avoids_upcasing_unrelated_environment_variable_names(self):
old_name = "28f425ca_d5d8_4257_b013_8d63166c8158"
if old_name == old_name.upper():
raise RuntimeError("test bug or strange locale: old_name invariant under upcasing")
os.putenv(old_name, "1") # It has to be done this lower-level way to set it lower-case.

# Step 1: Set the environment variable in this parent process. Because os.putenv is a thin
# wrapper around a system API, os.environ never sees the variable in this parent
# process, so the name is not upcased even on Windows.
os.putenv(old_name, "1")

# Step 2: Create the child process that inherits the environment variable. It will see it
# in os.environ with an upcased name, but if it is not mutated through os.environ
# then it will pass it on to its own child processes with the original name. The
# child process will use GitPython, and we are testing that it passes the variable
# with the exact original name to its own child processes.
cmdline = [
sys.executable,
fixture_path("env_case.py"),
self.rorepo.working_dir,
old_name,
]
pair_text = subprocess.check_output(cmdline, shell=False, text=True)
pair_text = subprocess.check_output(cmdline, shell=False, text=True) # Steps 3 and 4.

new_name = pair_text.split("=")[0]
self.assertEqual(new_name, old_name)

Expand Down