fix: CSV credential loader skips comments correctly, isolate credential tests

_load_credentials_from_csv used enumerate index (counting comments/blanks)
instead of a data-line counter, so CSVs with comments on line 0 would
never match account_index 0.

test_credentials_missing_all failed on machines with ~/.unsandbox/accounts.csv
because the test didn't isolate the home directory lookup. Now mocks
_get_unsandbox_dir and chdir to tmp_path.

Fixed in both sync and async SDKs.
This commit is contained in:
russell@unturf.com 2026-02-26 17:58:27 -05:00
parent 1c5a35f2b5
commit 8fae03d9fd
4 changed files with 23 additions and 27 deletions

View file

@ -72,14 +72,16 @@ def _load_credentials_from_csv(csv_path: Path, account_index: int = 0) -> Option
try:
with open(csv_path, "r") as f:
for i, line in enumerate(f):
data_index = 0
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if i == account_index:
if data_index == account_index:
parts = line.split(",")
if len(parts) >= 2:
return (parts[0].strip(), parts[1].strip())
data_index += 1
return None
except Exception:
return None

View file

@ -53,11 +53,15 @@ class TestCredentialResolution:
assert pk == "env_pk"
assert sk == "env_sk"
def test_credentials_error_no_sources(self, monkeypatch):
def test_credentials_error_no_sources(self, monkeypatch, tmp_path):
"""Test CredentialsError when no credentials found."""
import un_async
# Clear environment
monkeypatch.delenv("UNSANDBOX_PUBLIC_KEY", raising=False)
monkeypatch.delenv("UNSANDBOX_SECRET_KEY", raising=False)
# Point _get_unsandbox_dir to empty tmp dir so CSV lookup finds nothing
monkeypatch.setattr(un_async, "_get_unsandbox_dir", lambda: tmp_path)
monkeypatch.chdir(tmp_path)
with pytest.raises(CredentialsError) as exc_info:
_resolve_credentials(None, None)