Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions scripts/init.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"
Expand Down
Loading