diff --git a/scripts/init.py b/scripts/init.py index c4c8cae..06a0290 100644 --- a/scripts/init.py +++ b/scripts/init.py @@ -1,6 +1,7 @@ import os import re import shutil +from functools import lru_cache from pathlib import Path from subprocess import CalledProcessError, TimeoutExpired, check_output @@ -64,6 +65,7 @@ def _get_git_config(key: str) -> str: return "" +@lru_cache(maxsize=1) def _get_default_github() -> str: # Try git config first username = _get_git_config("github.user") or _get_git_config("user.name") diff --git a/tests/test_init.py b/tests/test_init.py index 977f56a..1329110 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -67,12 +67,16 @@ def mock_check_output(args, **kwargs): def test_get_default_github(monkeypatch): + # Clear lru_cache on _get_default_github before testing + init._get_default_github.cache_clear() + # Case 1: valid username in git config monkeypatch.setattr(init, "_git_config_loaded", True) monkeypatch.setattr(init, "_git_config_cache", {"github.user": "jane-doe"}) assert init._get_default_github() == "jane-doe" # Case 2: invalid username in git config, fallback to remote URL (HTTPS format) + init._get_default_github.cache_clear() monkeypatch.setattr(init, "_git_config_cache", {}) def mock_check_output(args, **kwargs): @@ -84,6 +88,8 @@ def mock_check_output(args, **kwargs): assert init._get_default_github() == "some-org" # Case 3: invalid username in git config, fallback to remote URL (SSH format) + init._get_default_github.cache_clear() + def mock_check_output_ssh(args, **kwargs): if "remote" in args and "get-url" in args: return "git@github.com:ssh-user/some-repo.git\n"