fix(github_copilot): use mkdtemp for atomic temp directory and clean test quality assertions

This commit is contained in:
yyouretoast 2026-08-26 15:28:45 +04:00
parent 1328b7124f
commit 2685175783
2 changed files with 8 additions and 12 deletions

View file

@ -231,10 +231,8 @@ class Authenticator:
self.token_dir,
e,
)
uid: Final = os.getuid() if hasattr(os, "getuid") else "user"
self.token_dir = os.path.join(tempfile.gettempdir(), f"litellm_{uid}", "github_copilot")
try:
os.makedirs(self.token_dir, mode=0o700, exist_ok=True)
self.token_dir = tempfile.mkdtemp(prefix="litellm_copilot_")
except OSError:
pass
self.access_token_file = os.path.join(

View file

@ -38,15 +38,13 @@ class TestGitHubCopilotAuthenticator:
assert os.path.basename(auth.api_key_file) == "api-key.json"
mock_makedirs.assert_not_called()
def test_ensure_token_dir(self):
def test_ensure_token_dir(self, tmp_path):
"""Test that the token directory is created if it doesn't exist."""
with (
patch("os.path.exists", return_value=False),
patch("os.makedirs") as mock_makedirs,
):
auth = Authenticator()
auth._ensure_token_dir()
mock_makedirs.assert_called_once_with(auth.token_dir, mode=0o700, exist_ok=True)
test_dir = str(tmp_path / "new_copilot_dir")
auth = Authenticator()
auth.token_dir = test_dir
auth._ensure_token_dir()
assert os.path.exists(test_dir)
def test_ensure_token_dir_permission_error_fallback(self):
"""Test that _ensure_token_dir falls back to temp directory on PermissionError."""
@ -54,7 +52,7 @@ class TestGitHubCopilotAuthenticator:
original_dir = auth.token_dir
with (
patch("os.path.exists", return_value=False),
patch("os.makedirs", side_effect=[PermissionError("Permission denied"), None]),
patch("os.makedirs", side_effect=PermissionError("Permission denied")),
):
auth._ensure_token_dir()
assert auth.token_dir != original_dir