Skip to content
Merged
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
16 changes: 11 additions & 5 deletions src/vectorcode/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
logger = logging.getLogger(name=__name__)


GLOBAL_CONFIG_PATH = os.path.join(
os.path.expanduser("~"), ".config", "vectorcode", "config.json"
GLOBAL_CONFIG_DIR = os.path.join(
os.path.expanduser("~"),
".config",
"vectorcode",
)
GLOBAL_INCLUDE_SPEC = os.path.join(
os.path.expanduser("~"), ".config", "vectorcode", "vectorcode.include"
Expand Down Expand Up @@ -442,10 +444,14 @@ def expand_envs_in_dict(d: dict):


async def load_config_file(path: Optional[Union[str, Path]] = None):
"""Load config file from ~/.config/vectorcode/config.json"""
"""Load config file from ~/.config/vectorcode/config.json(5)"""
if path is None:
path = GLOBAL_CONFIG_PATH
if os.path.isfile(path):
for name in ("config.json5", "config.json"):
p = os.path.join(GLOBAL_CONFIG_DIR, name)
if os.path.isfile(p):
path = str(p)
break
if path and os.path.isfile(path):
logger.debug(f"Loading config from {path}")
with open(path) as fin:
content = fin.read()
Expand Down
4 changes: 2 additions & 2 deletions src/vectorcode/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from pathlib import Path
from typing import Optional

from vectorcode.cli_utils import GLOBAL_CONFIG_PATH, Config, find_project_root
from vectorcode.cli_utils import GLOBAL_CONFIG_DIR, Config, find_project_root

logger = logging.getLogger(name=__name__)

__GLOBAL_HOOKS_PATH = Path(GLOBAL_CONFIG_PATH).parent / "hooks"
__GLOBAL_HOOKS_PATH = Path(GLOBAL_CONFIG_DIR) / "hooks"


# Keys: name of the hooks, ie. `pre-commit`
Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest

from vectorcode.cli_utils import GLOBAL_CONFIG_PATH
from vectorcode.cli_utils import GLOBAL_CONFIG_DIR


@pytest.fixture(autouse=True)
def restore_global_config_path():
global GLOBAL_CONFIG_PATH
original_global_config_path = GLOBAL_CONFIG_PATH
global GLOBAL_CONFIG_DIR
original_global_config_path = GLOBAL_CONFIG_DIR
yield
GLOBAL_CONFIG_PATH = original_global_config_path
GLOBAL_CONFIG_DIR = original_global_config_path
24 changes: 24 additions & 0 deletions tests/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest

from vectorcode import cli_utils
from vectorcode.cli_utils import (
CliAction,
Config,
Expand Down Expand Up @@ -207,6 +208,29 @@ async def test_load_config_file_invalid_json():
await load_config_file(config_path)


@pytest.mark.asyncio
async def test_load_from_default_config():
for name in ("config.json5", "config.json"):
with (
tempfile.TemporaryDirectory() as fake_home,
):
os.environ.update({"HOME": fake_home})
config_path = os.path.join(fake_home, ".config", "vectorcode", name)
config_dir = os.path.join(fake_home, ".config", "vectorcode")
setattr(
cli_utils,
"GLOBAL_CONFIG_DIR",
config_dir,
)
os.makedirs(config_dir, exist_ok=True)
config_content = '{"db_url": "http://default.url:8000"}'
with open(config_path, "w") as fin:
fin.write(config_content)

config = await load_config_file()
assert config.db_url == "http://default.url:8000"


@pytest.mark.asyncio
async def test_load_config_file_invalid_config():
with tempfile.TemporaryDirectory() as temp_dir:
Expand Down