diff --git a/honcho-cli/src/honcho_cli/config.py b/honcho-cli/src/honcho_cli/config.py index db7d7082..16c46f7a 100644 --- a/honcho-cli/src/honcho_cli/config.py +++ b/honcho-cli/src/honcho_cli/config.py @@ -1,6 +1,7 @@ """Configuration management for Honcho CLI. -Config stored at ``~/.honcho/config.json`` with env var overrides. +Config stored at ``~/.honcho/config.json`` with env var overrides. The config +directory defaults to ``~/.honcho`` and can be relocated with `HONCHO_CONFIG_DIR` The CLI owns these top-level keys in that file: @@ -30,7 +31,13 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: from honcho_cli.oauth import TokenResponse -CONFIG_DIR = Path.home() / ".honcho" +def _config_dir() -> Path: + """Config directory: ``$HONCHO_CONFIG_DIR`` if set, else ``~/.honcho``.""" + override = os.environ.get("HONCHO_CONFIG_DIR") + return Path(override).expanduser() if override else Path.home() / ".honcho" + + +CONFIG_DIR = _config_dir() CONFIG_FILE = CONFIG_DIR / "config.json" DEFAULT_BASE_URL = "https://api.honcho.dev" diff --git a/honcho-cli/tests/test_config.py b/honcho-cli/tests/test_config.py index 6074232e..46f7b384 100644 --- a/honcho-cli/tests/test_config.py +++ b/honcho-cli/tests/test_config.py @@ -3,9 +3,10 @@ import json import os import time +from pathlib import Path import pytest -from honcho_cli.config import CLIConfig, OAuthTokens +from honcho_cli.config import CLIConfig, OAuthTokens, _config_dir @pytest.fixture @@ -19,6 +20,20 @@ def cfg_path(tmp_path, monkeypatch): return f +class TestConfigDir: + def test_defaults_to_dot_honcho(self, monkeypatch): + monkeypatch.delenv("HONCHO_CONFIG_DIR", raising=False) + assert _config_dir() == Path.home() / ".honcho" + + def test_honcho_config_dir_override(self, monkeypatch, tmp_path): + monkeypatch.setenv("HONCHO_CONFIG_DIR", str(tmp_path / "profile")) + assert _config_dir() == tmp_path / "profile" + + def test_expands_user_in_override(self, monkeypatch): + monkeypatch.setenv("HONCHO_CONFIG_DIR", "~/.honcho-test") + assert _config_dir() == Path.home() / ".honcho-test" + + class TestLoad: def test_defaults_when_no_file(self, cfg_path): loaded = CLIConfig.load()