Skip to content

Commit 71cff30

Browse files
committed
Fixing the Editor & File Management Bugs
1 parent dafbf03 commit 71cff30

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

atoffice_shell/project.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,16 @@ class AtonEditor(App):
5454
("ctrl+shift+q", "quit_app", "Ctrl+Shift+Q Quit"),
5555
]
5656

57-
def __init__(self, file_path: str, **kwargs) -> None:
57+
def __init__(self, file_path: str, initial_text: str | None = None, **kwargs) -> None:
5858
super().__init__(**kwargs)
5959
self.file_path = file_path
60-
self.file_content = ""
60+
self.file_content = initial_text if initial_text is not None else ""
6161

62-
import os
63-
64-
if os.path.exists(file_path):
62+
if initial_text is None and os.path.exists(file_path):
6563
try:
6664
with open(file_path, "r", encoding="utf-8") as handle:
6765
self.file_content = handle.read()
68-
except Exception as error:
66+
except OSError as error:
6967
self.file_content = f"Error reading file: {error}"
7068

7169
def compose(self) -> ComposeResult:
@@ -81,8 +79,8 @@ def action_save_file(self) -> None:
8179
try:
8280
with open(self.file_path, "w", encoding="utf-8") as handle:
8381
handle.write(text_area.text)
84-
except Exception:
85-
pass
82+
except (PermissionError, OSError) as error:
83+
self.notify(f"Failed to save file: {error}", severity="error")
8684

8785
def action_quit_app(self) -> None:
8886
self.exit()
@@ -139,7 +137,7 @@ def _handle_edit_command(raw_command: str) -> bool:
139137
initial_text = file_path.read_text(encoding="utf-8") if file_path.exists() else ""
140138

141139
try:
142-
AtonEditor(str(file_path), initial_text).run()
140+
AtonEditor(str(file_path), initial_text=initial_text).run()
143141
except Exception as error:
144142
typer.secho(f"Editor error: {error}", fg=typer.colors.RED)
145143

tests/test_cli.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typer.testing import CliRunner
55

66
from atoffice_shell.cli import app
7-
from atoffice_shell.project import _handle_cd_command, _handle_edit_command, _handle_local_command, _handle_os_fallback
7+
from atoffice_shell.project import AtonEditor, _handle_cd_command, _handle_edit_command, _handle_local_command, _handle_os_fallback
88

99

1010
runner = CliRunner()
@@ -118,3 +118,34 @@ def run(self) -> None:
118118

119119
assert _handle_edit_command(f"edit {file_path}") is True
120120
assert calls == {"filename": str(file_path), "initial_text": "hello", "ran": True}
121+
122+
123+
def test_aton_editor_uses_initial_text_without_re_reading_file(tmp_path) -> None:
124+
file_path = tmp_path / "note.txt"
125+
file_path.write_text("from-disk", encoding="utf-8")
126+
127+
editor = AtonEditor(str(file_path), initial_text="from-arg")
128+
129+
assert editor.file_content == "from-arg"
130+
131+
132+
def test_action_save_file_notifies_on_permission_error(monkeypatch, tmp_path) -> None:
133+
file_path = tmp_path / "readonly.txt"
134+
editor = AtonEditor(str(file_path), initial_text="draft")
135+
136+
notifications = []
137+
138+
class FakeTextArea:
139+
text = "draft"
140+
141+
monkeypatch.setattr(editor, "query_one", lambda *_args, **_kwargs: FakeTextArea())
142+
monkeypatch.setattr(editor, "notify", lambda message, severity=None: notifications.append((message, severity)))
143+
144+
def fail_open(*_args, **_kwargs):
145+
raise PermissionError("read-only")
146+
147+
monkeypatch.setattr("builtins.open", fail_open)
148+
149+
editor.action_save_file()
150+
151+
assert notifications and "Failed to save file" in notifications[0][0]

0 commit comments

Comments
 (0)