Skip to content
Closed
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
23 changes: 23 additions & 0 deletions tests/tools/test_base_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ def test_unique_delimiter_each_call(self):


class TestInitSessionFailure:
def test_bootstrap_cds_into_configured_cwd_before_snapshot(self):
env = _TestableEnv(cwd="/tmp/project")

calls = []

def mock_run_bash(cmd, *, login=False, timeout=120, stdin_data=None):
calls.append({"cmd": cmd, "login": login, "timeout": timeout})
return MagicMock()

def mock_wait_for_process(proc, timeout=None):
return {"output": f"\n{env._cwd_marker}/tmp/project{env._cwd_marker}\n"}

env._run_bash = mock_run_bash
env._wait_for_process = mock_wait_for_process

env.init_session()

assert len(calls) == 1
assert calls[0]["login"] is True
assert "cd /tmp/project || exit 126" in calls[0]["cmd"]
assert env.cwd == "/tmp/project"
assert env._snapshot_ready is True

def test_snapshot_ready_false_on_failure(self):
env = _TestableEnv()

Expand Down
7 changes: 6 additions & 1 deletion tools/environments/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,14 @@ def init_session(self):
``_snapshot_ready = True`` so subsequent commands source the snapshot
instead of running with ``bash -l``.
"""
quoted_cwd = (
shlex.quote(self.cwd)
if self.cwd != "~" and not self.cwd.startswith("~/")
else self.cwd
)
# Full capture: env vars, functions (filtered), aliases, shell options.
bootstrap = (
f"cd {quoted_cwd} || exit 126\n"
f"export -p > {self._snapshot_path}\n"
f"declare -f | grep -vE '^_[^_]' >> {self._snapshot_path}\n"
f"alias -p >> {self._snapshot_path}\n"
Expand Down Expand Up @@ -760,4 +766,3 @@ def _prepare_command(self, command: str) -> tuple[str, str | None]:
from tools.terminal_tool import _transform_sudo_command

return _transform_sudo_command(command)