Skip to content

Commit

Permalink
Eliminate duplication of git.util.cwd logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EliahKagan committed Sep 7, 2023
1 parent c7fad20 commit eebdb25
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
1 change: 1 addition & 0 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def wrapper(self: "Remote", *args: Any, **kwargs: Any) -> T:

@contextlib.contextmanager
def cwd(new_dir: PathLike) -> Generator[PathLike, None, None]:
"""Context manager to temporarily change directory. Not reentrant."""
old_dir = os.getcwd()
os.chdir(new_dir)
try:
Expand Down
16 changes: 2 additions & 14 deletions test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
import contextlib
import os
import shutil
import subprocess
Expand All @@ -15,24 +14,13 @@
from git import Git, refresh, GitCommandError, GitCommandNotFound, Repo, cmd
from test.lib import TestBase, fixture_path
from test.lib import with_rw_directory
from git.util import finalize_process
from git.util import cwd, finalize_process

import os.path as osp

from git.compat import is_win


@contextlib.contextmanager
def _chdir(new_dir):
"""Context manager to temporarily change directory. Not reentrant."""
old_dir = os.getcwd()
os.chdir(new_dir)
try:
yield
finally:
os.chdir(old_dir)


class TestGit(TestBase):
@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -102,7 +90,7 @@ def test_it_executes_git_not_from_cwd(self):
print("#!/bin/sh", file=file)
os.chmod(impostor_path, 0o755)

with _chdir(tmpdir):
with cwd(tmpdir):
self.assertRegex(self.git.execute(["git", "version"]), r"^git version\b")

@skipUnless(is_win, "The regression only affected Windows, and this test logic is OS-specific.")
Expand Down

0 comments on commit eebdb25

Please sign in to comment.