Skip to content

Commit

Permalink
moved session_path from config to session_file util
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeizhou-ap committed Sep 4, 2024
1 parent 744c7f9 commit 67935c5
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 24 deletions.
9 changes: 1 addition & 8 deletions src/goose/cli/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from functools import cache
from io import StringIO
from pathlib import Path
from typing import Callable, Dict, Mapping, Tuple

from rich import print
Expand All @@ -9,7 +8,7 @@
from rich.text import Text
from ruamel.yaml import YAML

from ..config import SESSIONS_PATH, SESSION_FILE_SUFFIX, PROFILES_CONFIG_PATH
from ..config import PROFILES_CONFIG_PATH
from goose.profile import Profile
from goose.utils import load_plugins
from goose.utils.diff import pretty_diff
Expand All @@ -19,12 +18,6 @@
def default_profiles() -> Mapping[str, Callable]:
return load_plugins(group="goose.profile")


def session_path(name: str) -> Path:
SESSIONS_PATH.mkdir(parents=True, exist_ok=True)
return SESSIONS_PATH.joinpath(f"{name}{SESSION_FILE_SUFFIX}")


def write_config(profiles: Dict[str, Profile]) -> None:
"""Overwrite the config with the passed profiles"""
PROFILES_CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
Expand Down
2 changes: 1 addition & 1 deletion src/goose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rich import print
from ruamel.yaml import YAML

from goose.cli.config import SESSIONS_PATH
from ..config import SESSIONS_PATH
from goose.cli.session import Session
from goose.utils import load_plugins
from goose.utils.session_file import list_sorted_session_files
Expand Down
2 changes: 1 addition & 1 deletion src/goose/cli/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
default_profiles,
ensure_config,
read_config,
session_path,
)
from ..utils.session_file import session_path
from goose.cli.prompt.goose_prompt_session import GoosePromptSession
from goose.notifier import Notifier
from goose.profile import Profile
Expand Down
6 changes: 5 additions & 1 deletion src/goose/utils/session_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from exchange import Message

from goose.cli.config import SESSION_FILE_SUFFIX
from ..config import SESSIONS_PATH, SESSION_FILE_SUFFIX

def write_to_file(file_path: Path, messages: List[Message]) -> None:
with open(file_path, "w") as f:
Expand Down Expand Up @@ -36,3 +36,7 @@ def session_file_exists(session_files_directory: Path) -> bool:
if not session_files_directory.exists():
return False
return any(list_session_files(session_files_directory))

def session_path(name: str) -> Path:
SESSIONS_PATH.mkdir(parents=True, exist_ok=True)
return SESSIONS_PATH.joinpath(f"{name}{SESSION_FILE_SUFFIX}")
5 changes: 1 addition & 4 deletions tests/cli/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import patch

import pytest
from goose.cli.config import ensure_config, read_config, session_path, write_config
from goose.cli.config import ensure_config, read_config, write_config
from goose.profile import default_profile


Expand Down Expand Up @@ -76,6 +76,3 @@ def test_ensure_config_keep_original_default_profile(

assert read_config() == {"default": existing_profile}


def test_session_path(mock_sessions_path):
assert session_path("session1") == mock_sessions_path / "session1.jsonl"
8 changes: 6 additions & 2 deletions tests/cli/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ def mock_specified_session_name():
with patch.object(PromptSession, "prompt", return_value=SPECIFIED_SESSION_NAME) as specified_session_name:
yield specified_session_name

@pytest.fixture
def mock_sessions_path(tmp_path):
with patch("goose.config.SESSIONS_PATH", tmp_path) as mock_path:
yield mock_path

@pytest.fixture
def create_session_with_mock_configs(mock_sessions_path, exchange_factory, profile_factory):
def create_session_with_mock_configs(exchange_factory, profile_factory, tmp_path):
with patch("goose.cli.session.build_exchange", return_value=exchange_factory()), patch(
"goose.cli.session.load_profile", return_value=profile_factory()
), patch("goose.cli.session.SessionNotifier") as mock_session_notifier, patch(
"goose.cli.session.load_provider", return_value="provider"
):
), patch("goose.utils.session_file.SESSIONS_PATH", tmp_path):
mock_session_notifier.return_value = MagicMock()

def create_session(session_attributes: dict = {}):
Expand Down
6 changes: 0 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ def _create_exchange(attributes={}):
return _create_exchange


@pytest.fixture
def mock_sessions_path(tmp_path):
with patch("goose.cli.config.SESSIONS_PATH", tmp_path) as mock_path:
yield mock_path


@pytest.fixture
def create_session_file():
def _create_session_file(messages, session_file_path, mtime=time()):
Expand Down
7 changes: 6 additions & 1 deletion tests/utils/test_session_file.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from pathlib import Path
from unittest.mock import patch

import pytest
from exchange import Message
from goose.utils.session_file import list_sorted_session_files, read_from_file, session_file_exists, write_to_file
from goose.utils.session_file import list_sorted_session_files, read_from_file, session_file_exists, session_path, write_to_file


@pytest.fixture
Expand Down Expand Up @@ -75,3 +76,7 @@ def create_session_file(file_path, file_name) -> Path:
file = file_path / f"{file_name}.jsonl"
file.touch()
return file

def test_session_path(tmp_path):
with patch("goose.utils.session_file.SESSIONS_PATH", tmp_path) as mock_session_path:
assert session_path("session1") == mock_session_path / "session1.jsonl"

0 comments on commit 67935c5

Please sign in to comment.