Skip to content
Open
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ install-dev: check-uv-version

test: check-uv-version
@$(ECHO) "$(YELLOW)Run tests...$(RESET)"
uv run env HOME="$$(mktemp -d)" pytest --ignore=tests/snapshots
uv run pytest --ignore=tests/snapshots
@$(ECHO) "$(GREEN)Tests completed.$(RESET)"

test-snapshots: check-uv-version
@$(ECHO) "$(YELLOW)Run snapshots tests...$(RESET)"
uv run env HOME="$$(mktemp -d)" pytest tests/snapshots -v
uv run pytest tests/snapshots -v
@$(ECHO) "$(GREEN)Snapshots tests completed.$(RESET)"

test-binary: check-uv-version
@$(ECHO) "$(YELLOW)Run end-to-end tests...$(RESET)"
uv run env HOME="$$(mktemp -d)" pytest tui_e2e
uv run pytest tui_e2e
Comment thread
cbagwell marked this conversation as resolved.
@$(ECHO) "$(GREEN)End-to-end tests completed.$(RESET)"

test-all: test test-snapshots
Expand Down
44 changes: 44 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Root conftest.py - applies autouse fixtures to ALL tests including tui_e2e/."""

import os.path

import pytest


@pytest.fixture(autouse=True)
def isolate_test_persistence(tmp_path, monkeypatch):
"""Ensure all tests use isolated temp directories instead of ~/.openhands/.

This autouse fixture runs BEFORE every test and sets environment variables
to redirect all OpenHands persistence to an isolated temp directory.
This prevents tests from accidentally writing to the user's real
~/.openhands/ directory - including tests that don't use mock_locations.

Mocks:
- OPENHANDS_PERSISTENCE_DIR -> isolated temp dir
- OPENHANDS_CONVERSATIONS_DIR -> isolated temp dir
- PERSISTENCE_DIR -> isolated temp dir (legacy env var)
- os.path.expanduser("~") -> isolated temp home (fallback when env not checked)
"""
home_dir = tmp_path / "home"
home_dir.mkdir()
persistence_dir = home_dir / ".openhands"
persistence_dir.mkdir(exist_ok=True)
conversations_dir = persistence_dir / "conversations"
conversations_dir.mkdir(exist_ok=True)

monkeypatch.setenv("OPENHANDS_PERSISTENCE_DIR", str(persistence_dir))
monkeypatch.setenv("OPENHANDS_CONVERSATIONS_DIR", str(conversations_dir))
monkeypatch.setenv("PERSISTENCE_DIR", str(persistence_dir))

original_expanduser = os.path.expanduser

def mock_expanduser(path):
path_str = str(path)
if path_str == "~":
return str(home_dir)
elif path_str.startswith("~/"):
return str(home_dir / path_str[2:])
return original_expanduser(path_str)

monkeypatch.setattr(os.path, "expanduser", mock_expanduser)
22 changes: 21 additions & 1 deletion tests/tui/modals/settings/test_timeout_preservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,29 @@
timeout.
"""

import pytest

import openhands_cli.tui.modals.settings.utils as settings_utils
from openhands.sdk import LLM, Agent
from openhands_cli.tui.modals.settings.utils import SettingsFormData, save_settings


def test_invalid_timeout_keeps_existing_value():
class FakeAgentStore:
def __init__(self) -> None:
self.saved_agents: list[Agent] = []

def save(self, agent: Agent) -> None:
self.saved_agents.append(agent)


@pytest.fixture
def deps(monkeypatch) -> FakeAgentStore:
fake_store = FakeAgentStore()
monkeypatch.setattr(settings_utils, "agent_store", fake_store)
return fake_store


def test_invalid_timeout_keeps_existing_value(deps: FakeAgentStore):
# Existing agent with a known timeout (e.g., 120 seconds)
existing_llm = LLM(
model="openai/gpt-4o",
Expand Down Expand Up @@ -41,4 +59,6 @@ def test_invalid_timeout_keeps_existing_value():
assert result.success
# The saved agent should retain the original timeout (120 seconds)
assert result.error_message is None
assert deps.saved_agents
assert deps.saved_agents[-1].llm.timeout == 120
assert existing_agent.llm.timeout == 120
Loading