Repository navigation
Expand file tree
/
Copy pathtest_cli.py
More file actions
205 lines (142 loc) · 6.32 KB
/
Copy pathtest_cli.py
File metadata and controls
205 lines (142 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import subprocess
from types import SimpleNamespace
from typer.testing import CliRunner
from trushell.cli import app
from trushell.project import (
TruShellEditor,
_handle_cd_command,
_handle_edit_command,
_handle_local_command,
_handle_os_fallback,
_run_external_command,
)
runner = CliRunner()
def test_version_command() -> None:
result = runner.invoke(app, ["version"])
assert result.exit_code == 0
assert "0.1.0" in result.stdout
def test_help_shows_usage() -> None:
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "Usage" in result.stdout
def test_unknown_command_uses_os_fallback(monkeypatch) -> None:
calls = {}
def fake_run(command: str, shell: bool, check: bool, cwd: str) -> subprocess.CompletedProcess[str]:
calls["command"] = command
calls["shell"] = shell
calls["check"] = check
calls["cwd"] = cwd
return subprocess.CompletedProcess(args=command, returncode=0)
monkeypatch.setattr("trushell.project._run_external_command", fake_run)
assert _handle_os_fallback("pwd") is True
assert calls == {"command": "pwd", "shell": True, "check": False, "cwd": os.getcwd()}
def test_run_external_command_profiles_resources(monkeypatch) -> None:
calls = {}
stats = []
class FakePopen:
def __init__(self, command: str, shell: bool, cwd: str | None = None) -> None:
calls["command"] = command
calls["shell"] = shell
calls["cwd"] = cwd
self.pid = 123
self.returncode = None
self._first = True
def wait(self, timeout: float | None = None) -> int:
if self._first:
self._first = False
raise subprocess.TimeoutExpired(cmd="echo hi", timeout=timeout)
self.returncode = 5
return 5
class FakeProcess:
def __init__(self, pid: int) -> None:
self.pid = pid
self._count = 0
def cpu_percent(self, interval=None) -> float:
self._count += 1
return 8.2 if self._count > 1 else 0.0
def memory_info(self):
return SimpleNamespace(rss=45 * 1024 * 1024)
monkeypatch.setattr("trushell.project.subprocess.Popen", FakePopen)
monkeypatch.setattr("trushell.project.psutil.Process", FakeProcess)
monkeypatch.setattr(
"trushell.project.typer.secho",
lambda message, fg=None: stats.append((message, fg)),
)
result = _run_external_command("echo hi", shell=True, check=False, cwd="/tmp")
assert result.returncode == 5
assert calls == {"command": "echo hi", "shell": True, "cwd": "/tmp"}
assert stats and "CPU peak" in stats[0][0] and "RAM peak" in stats[0][0]
def test_cd_command_changes_directory_and_runs_ls(monkeypatch) -> None:
calls = {}
def fake_chdir(path: str) -> None:
calls["chdir"] = path
def fake_run(command: str, shell: bool, check: bool, cwd: str) -> SimpleNamespace:
calls["ls_command"] = command
calls["ls_shell"] = shell
calls["ls_check"] = check
calls["ls_cwd"] = cwd
return SimpleNamespace(returncode=0)
monkeypatch.setattr("trushell.project.os.chdir", fake_chdir)
monkeypatch.setattr("trushell.project._run_external_command", fake_run)
assert _handle_cd_command("cd /tmp") is True
assert calls == {"chdir": "/tmp", "ls_command": "ls", "ls_shell": True, "ls_check": False, "ls_cwd": os.getcwd()}
def test_cd_without_target_prints_syntax_hint(monkeypatch) -> None:
calls = {}
def fake_chdir(path: str) -> None:
calls["chdir"] = path
def fake_run(command: str, shell: bool, check: bool, cwd: str) -> SimpleNamespace:
calls["ls_command"] = command
calls["ls_cwd"] = cwd
return SimpleNamespace(returncode=0)
monkeypatch.setattr("trushell.project.os.path.expanduser", lambda path: "/home/test")
monkeypatch.setattr("trushell.project.os.chdir", fake_chdir)
monkeypatch.setattr("trushell.project.subprocess.run", fake_run)
assert _handle_cd_command("cd") is True
assert calls == {}
def test_addtask_missing_arguments_is_blocked(monkeypatch) -> None:
messages = []
monkeypatch.setattr(
"trushell.project.typer.secho",
lambda message, fg=None: messages.append((message, fg)),
)
assert _handle_local_command("addtask", "") == "handled"
def test_edit_requires_filename(monkeypatch) -> None:
messages = []
monkeypatch.setattr(
"trushell.project.typer.secho",
lambda message, fg=None: messages.append((message, fg)),
)
assert _handle_edit_command("edit") is True
assert messages and "Syntax: edit <filename>" in messages[0][0]
def test_edit_launches_editor_for_existing_file(monkeypatch, tmp_path) -> None:
file_path = tmp_path / "note.txt"
file_path.write_text("hello", encoding="utf-8")
calls = {}
class FakeEditor:
def __init__(self, filename: str, initial_text: str) -> None:
calls["filename"] = filename
calls["initial_text"] = initial_text
def run(self) -> None:
calls["ran"] = True
monkeypatch.setattr("trushell.project.TruShellEditor", FakeEditor)
assert _handle_edit_command(f"edit {file_path}") is True
assert calls == {"filename": str(file_path), "initial_text": "hello", "ran": True}
def test_trushell_editor_uses_initial_text_without_re_reading_file(tmp_path) -> None:
file_path = tmp_path / "note.txt"
file_path.write_text("from-disk", encoding="utf-8")
editor = TruShellEditor(str(file_path), initial_text="from-arg")
assert editor.file_content == "from-arg"
def test_action_save_file_notifies_on_permission_error(monkeypatch, tmp_path) -> None:
file_path = tmp_path / "readonly.txt"
editor = TruShellEditor(str(file_path), initial_text="draft")
notifications = []
class FakeTextArea:
text = "draft"
monkeypatch.setattr(editor, "query_one", lambda *_args, **_kwargs: FakeTextArea())
monkeypatch.setattr(editor, "notify", lambda message, severity=None: notifications.append((message, severity)))
def fail_open(*_args, **_kwargs):
raise PermissionError("read-only")
monkeypatch.setattr("builtins.open", fail_open)
editor.action_save_file()
assert notifications and "Failed to save file" in notifications[0][0]