| 1 |
import subprocess |
| 2 |
from pathlib import Path |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from cyclopts._edit import ( |
| 7 |
EditorDidNotChangeError, |
| 8 |
EditorDidNotSaveError, |
| 9 |
EditorError, |
| 10 |
EditorNotFoundError, |
| 11 |
edit, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
@pytest.fixture |
| 16 |
def mock_editor(): |
| 17 |
"""Mock editor that simulates saving file with edited content.""" |
| 18 |
|
| 19 |
def fake_editor(args): |
| 20 |
Path(args[1]).write_text("edited content") |
| 21 |
return 0 |
| 22 |
|
| 23 |
return fake_editor |
| 24 |
|
| 25 |
|
| 26 |
def test_basic_edit(mocker, mock_editor, monkeypatch): |
| 27 |
"""Test basic editing functionality.""" |
| 28 |
monkeypatch.setenv("EDITOR", "test_editor") |
| 29 |
mocker.patch("shutil.which", return_value=True) |
| 30 |
mocker.patch("subprocess.check_call", side_effect=mock_editor) |
| 31 |
|
| 32 |
result = edit("initial text") |
| 33 |
assert result == "edited content" |
| 34 |
|
| 35 |
|
| 36 |
def test_custom_path(mocker, mock_editor, tmp_path, monkeypatch): |
| 37 |
"""Test editing with custom path.""" |
| 38 |
custom_path = tmp_path / "custom.txt" |
| 39 |
monkeypatch.setenv("EDITOR", "test_editor") |
| 40 |
mocker.patch("shutil.which", return_value=True) |
| 41 |
mocker.patch("subprocess.check_call", side_effect=mock_editor) |
| 42 |
|
| 43 |
result = edit("initial text", path=custom_path) |
| 44 |
assert result == "edited content" |
| 45 |
|
| 46 |
|
| 47 |
def test_editor_not_found(mocker, monkeypatch): |
| 48 |
"""Test behavior when no editor is found.""" |
| 49 |
monkeypatch.delenv("EDITOR", raising=False) |
| 50 |
mocker.patch("shutil.which", return_value=False) |
| 51 |
|
| 52 |
with pytest.raises(EditorNotFoundError): |
| 53 |
edit("test") |
| 54 |
|
| 55 |
|
| 56 |
def test_did_not_save(mocker, monkeypatch): |
| 57 |
"""Test behavior when user doesn't save.""" |
| 58 |
monkeypatch.setenv("EDITOR", "test_editor") |
| 59 |
|
| 60 |
def fake_editor(_): |
| 61 |
return 0 |
| 62 |
|
| 63 |
mocker.patch("shutil.which", return_value=True) |
| 64 |
mocker.patch("subprocess.check_call", side_effect=fake_editor) |
| 65 |
|
| 66 |
with pytest.raises(EditorDidNotSaveError): |
| 67 |
edit("initial text", save=True) |
| 68 |
|
| 69 |
|
| 70 |
def test_did_not_change(mocker, monkeypatch): |
| 71 |
"""Test behavior when content isn't changed.""" |
| 72 |
monkeypatch.setenv("EDITOR", "test_editor") |
| 73 |
initial_text = "unchanged text" |
| 74 |
|
| 75 |
def fake_editor(args): |
| 76 |
assert args[0] == "test_editor" |
| 77 |
Path(args[1]).write_text(initial_text) |
| 78 |
return 0 |
| 79 |
|
| 80 |
mocker.patch("shutil.which", return_value=True) |
| 81 |
mocker.patch("subprocess.check_call", side_effect=fake_editor) |
| 82 |
|
| 83 |
with pytest.raises(EditorDidNotChangeError): |
| 84 |
edit(initial_text, required=True) |
| 85 |
|
| 86 |
|
| 87 |
def test_editor_error(mocker, monkeypatch): |
| 88 |
"""Test handling of editor errors.""" |
| 89 |
monkeypatch.setenv("EDITOR", "test_editor") |
| 90 |
mocker.patch("shutil.which", return_value=True) |
| 91 |
mocker.patch("subprocess.check_call", side_effect=subprocess.CalledProcessError(1, "test_editor")) |
| 92 |
|
| 93 |
with pytest.raises(EditorError) as exc_info: |
| 94 |
edit("test") |
| 95 |
assert "status 1" in str(exc_info.value) |
| 96 |
|
| 97 |
|
| 98 |
def test_custom_encoding(mocker, tmp_path, monkeypatch): |
| 99 |
"""Test custom encoding support.""" |
| 100 |
test_path = tmp_path / "test.txt" |
| 101 |
monkeypatch.setenv("EDITOR", "test_editor") |
| 102 |
|
| 103 |
def fake_editor_with_encoding(args): |
| 104 |
assert args[0] == "test_editor" |
| 105 |
Path(args[1]).write_text("текст", encoding="utf-16") |
| 106 |
return 0 |
| 107 |
|
| 108 |
mocker.patch("shutil.which", return_value=True) |
| 109 |
mocker.patch("subprocess.check_call", side_effect=fake_editor_with_encoding) |
| 110 |
|
| 111 |
result = edit("initial", path=test_path, encoding="utf-16") |
| 112 |
assert result == "текст" |
| 113 |
|
| 114 |
|
| 115 |
def test_fallback_editors(mocker, mock_editor, monkeypatch): |
| 116 |
"""Test fallback editor selection.""" |
| 117 |
monkeypatch.delenv("EDITOR", raising=False) |
| 118 |
|
| 119 |
def mock_which(editor_name): |
| 120 |
return editor_name == "vim" |
| 121 |
|
| 122 |
mocker.patch("shutil.which", side_effect=mock_which) |
| 123 |
mock_check_call = mocker.patch("subprocess.check_call", side_effect=mock_editor) |
| 124 |
|
| 125 |
result = edit("test", fallback_editors=["emacs", "vim", "nano"]) |
| 126 |
assert result == "edited content" |
| 127 |
assert mock_check_call.call_args_list[0].args[0][0] == "vim" |
| 128 |
|
| 129 |
|
| 130 |
def test_editor_args(mocker, monkeypatch): |
| 131 |
"""Test passing additional arguments to editor.""" |
| 132 |
monkeypatch.setenv("EDITOR", "test_editor") |
| 133 |
|
| 134 |
def check_editor_args(args): |
| 135 |
assert args[0] == "test_editor" |
| 136 |
assert "--no-splash" in args |
| 137 |
Path(args[1]).write_text("edited with args") |
| 138 |
return 0 |
| 139 |
|
| 140 |
mocker.patch("shutil.which", return_value=True) |
| 141 |
mocker.patch("subprocess.check_call", side_effect=check_editor_args) |
| 142 |
|
| 143 |
result = edit("test", editor_args=["--no-splash"]) |
| 144 |
assert result == "edited with args" |
| 145 |
|
| 146 |
|
| 147 |
def test_optional_change(mocker, monkeypatch): |
| 148 |
"""Test when content changes are optional.""" |
| 149 |
monkeypatch.setenv("EDITOR", "test_editor") |
| 150 |
initial_text = "unchanged" |
| 151 |
|
| 152 |
def fake_editor(args): |
| 153 |
assert args[0] == "test_editor" |
| 154 |
Path(args[1]).write_text(initial_text) |
| 155 |
return 0 |
| 156 |
|
| 157 |
mocker.patch("shutil.which", return_value=True) |
| 158 |
mocker.patch("subprocess.check_call", side_effect=fake_editor) |
| 159 |
|
| 160 |
|
| 161 |
result = edit(initial_text, required=False) |
| 162 |
assert result == initial_text |
| 163 |
|
| 164 |
|
| 165 |
def test_file_cleanup(mocker, tmp_path, monkeypatch, mock_editor): |
| 166 |
"""Test temporary file cleanup.""" |
| 167 |
test_path = tmp_path / "cleanup_test.txt" |
| 168 |
monkeypatch.setenv("EDITOR", "test_editor") |
| 169 |
|
| 170 |
mocker.patch("shutil.which", return_value=True) |
| 171 |
mocker.patch("subprocess.check_call", side_effect=mock_editor) |
| 172 |
|
| 173 |
edit("test", path=test_path) |
| 174 |
assert not test_path.exists() |