Files
mistral-vibe/tests/cli/test_external_editor.py
Clément Drouin e1a25caa52 v2.7.5 (#589)
Co-authored-by: Bastien <bastien.baret@gmail.com>
Co-authored-by: Clément Sirieix <clement.sirieix@mistral.ai>
Co-authored-by: Julien Legrand <72564015+JulienLGRD@users.noreply.github.com>
Co-authored-by: Kim-Adeline Miguel <51720070+kimadeline@users.noreply.github.com>
Co-authored-by: Mathias Gesbert <mathias.gesbert@mistral.ai>
Co-authored-by: Pierre Rossinès <pierre.rossines@mistral.ai>
Co-authored-by: Quentin <quentin.torroba@mistral.ai>
Co-authored-by: Vincent G <10739306+VinceOPS@users.noreply.github.com>
Co-authored-by: Mistral Vibe <vibe@mistral.ai>
2026-04-14 10:33:15 +02:00

73 lines
3.2 KiB
Python

"""Tests for the external editor module."""
from __future__ import annotations
from unittest.mock import patch
from vibe.cli.textual_ui.external_editor import ExternalEditor
class TestGetEditor:
def test_returns_visual_first(self) -> None:
with patch.dict("os.environ", {"VISUAL": "vim", "EDITOR": "nvim"}, clear=True):
assert ExternalEditor.get_editor() == "vim"
def test_falls_back_to_editor(self) -> None:
with patch.dict("os.environ", {"EDITOR": "nvim"}, clear=True):
assert ExternalEditor.get_editor() == "nvim"
def test_falls_back_when_no_editor(self) -> None:
with patch.dict("os.environ", {}, clear=True):
assert ExternalEditor.get_editor() == "nano"
class TestEdit:
def test_returns_modified_content(self) -> None:
with patch.dict("os.environ", {"VISUAL": "vim"}, clear=True):
with patch("subprocess.run") as mock_run:
with patch("pathlib.Path.read_bytes", return_value=b"modified"):
with patch("pathlib.Path.unlink"):
editor = ExternalEditor()
result = editor.edit("original")
assert result == "modified"
mock_run.assert_called_once()
def test_returns_none_when_content_unchanged(self) -> None:
with patch.dict("os.environ", {"VISUAL": "vim"}, clear=True):
with patch("subprocess.run"):
with patch("pathlib.Path.read_bytes", return_value=b"same"):
with patch("pathlib.Path.unlink"):
editor = ExternalEditor()
result = editor.edit("same")
assert result is None
def test_strips_trailing_whitespace(self) -> None:
with patch.dict("os.environ", {"VISUAL": "vim"}, clear=True):
with patch("subprocess.run"):
with patch("pathlib.Path.read_bytes", return_value=b"content\n\n"):
with patch("pathlib.Path.unlink"):
editor = ExternalEditor()
result = editor.edit("original")
assert result == "content"
def test_handles_editor_with_args(self) -> None:
with patch.dict("os.environ", {"VISUAL": "code --wait"}, clear=True):
with patch("subprocess.run") as mock_run:
with patch("pathlib.Path.read_bytes", return_value=b"edited"):
with patch("pathlib.Path.unlink"):
editor = ExternalEditor()
editor.edit("original")
call_args = mock_run.call_args[0][0]
assert call_args[0] == "code"
assert call_args[1] == "--wait"
def test_returns_none_on_subprocess_error(self) -> None:
import subprocess as sp
with patch.dict("os.environ", {"VISUAL": "vim"}, clear=True):
with patch("subprocess.run", side_effect=sp.CalledProcessError(1, "vim")):
with patch("pathlib.Path.unlink"):
editor = ExternalEditor()
result = editor.edit("test")
assert result is None