diff --git a/git/util.py b/git/util.py index f80580cff..dee467dd3 100644 --- a/git/util.py +++ b/git/util.py @@ -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: diff --git a/test/test_git.py b/test/test_git.py index 804cd22e4..f1d35a355 100644 --- a/test/test_git.py +++ b/test/test_git.py @@ -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 @@ -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): @@ -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.")