-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
238 lines (190 loc) · 7.88 KB
/
test_cli.py
File metadata and controls
238 lines (190 loc) · 7.88 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import List
import pytest
import pythonnative.cli.pn as pn_cli
import pythonnative.hot_reload as hot_reload_module
def run_pn(args: List[str], cwd: str) -> subprocess.CompletedProcess[str]:
cmd = [sys.executable, "-m", "pythonnative.cli.pn"] + args
return subprocess.run(cmd, cwd=cwd, check=False, capture_output=True, text=True)
def test_cli_init_and_clean() -> None:
tmpdir = tempfile.mkdtemp(prefix="pn_cli_test_")
try:
# init
result = run_pn(["init", "MyApp"], tmpdir)
assert result.returncode == 0, result.stderr
assert os.path.isdir(os.path.join(tmpdir, "app"))
# scaffolded entrypoint
main_path = os.path.join(tmpdir, "app", "main.py")
assert os.path.isfile(main_path)
with open(main_path, "r", encoding="utf-8") as f:
content = f.read()
assert "def App(" in content
assert "Stack.Navigator" in content
assert "pn.run" not in content
assert os.path.isfile(os.path.join(tmpdir, "pythonnative.json"))
assert os.path.isfile(os.path.join(tmpdir, "requirements.txt"))
assert os.path.isfile(os.path.join(tmpdir, ".gitignore"))
# clean (on empty build should be no-op)
result = run_pn(["clean"], tmpdir)
assert result.returncode == 0, result.stderr
# create build dir and ensure clean removes it
os.makedirs(os.path.join(tmpdir, "build", "android"), exist_ok=True)
result = run_pn(["clean"], tmpdir)
assert result.returncode == 0, result.stderr
assert not os.path.exists(os.path.join(tmpdir, "build"))
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
def test_cli_run_help_lists_logging_flags() -> None:
"""`pn run --help` should advertise both --no-logs and --hot-reload."""
tmpdir = tempfile.mkdtemp(prefix="pn_cli_test_")
try:
result = run_pn(["run", "--help"], tmpdir)
assert result.returncode == 0, result.stderr
assert "--no-logs" in result.stdout
assert "--hot-reload" in result.stdout
assert "--prepare-only" in result.stdout
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
def test_cli_run_rejects_unknown_flag() -> None:
tmpdir = tempfile.mkdtemp(prefix="pn_cli_test_")
try:
result = run_pn(["run", "android", "--does-not-exist"], tmpdir)
assert result.returncode != 0
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
def test_cli_run_prepare_only_android_and_ios() -> None:
tmpdir = tempfile.mkdtemp(prefix="pn_cli_test_")
try:
# init to create app scaffold
result = run_pn(["init", "MyApp"], tmpdir)
assert result.returncode == 0, result.stderr
# prepare-only android, combined with --no-logs to verify both flags
# coexist without launching any adb/simctl subprocess (prepare-only
# returns before logcat would ever be spawned).
result = run_pn(["run", "android", "--prepare-only", "--no-logs"], tmpdir)
assert result.returncode == 0, result.stderr
android_root = os.path.join(tmpdir, "build", "android", "android_template")
assert os.path.isdir(android_root)
# Ensure new Fragment-based navigation exists
page_fragment = os.path.join(
android_root,
"app",
"src",
"main",
"java",
"com",
"pythonnative",
"android_template",
"ScreenFragment.kt",
)
assert os.path.isfile(page_fragment)
virtual_list_helper = os.path.join(
android_root,
"app",
"src",
"main",
"java",
"com",
"pythonnative",
"android_template",
"PNVirtualListView.java",
)
assert os.path.isfile(virtual_list_helper)
nav_graph = os.path.join(
android_root,
"app",
"src",
"main",
"res",
"navigation",
"nav_graph.xml",
)
assert os.path.isfile(nav_graph)
# prepare-only ios with --no-logs
result = run_pn(["run", "ios", "--prepare-only", "--no-logs"], tmpdir)
assert result.returncode == 0, result.stderr
assert os.path.isdir(os.path.join(tmpdir, "build", "ios", "ios_template"))
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
def test_booted_ios_udid_picks_first_booted_device(monkeypatch: pytest.MonkeyPatch) -> None:
"""`_booted_ios_udid` parses ``simctl list devices booted --json``."""
sample_json = (
'{"devices": {'
'"com.apple.CoreSimulator.SimRuntime.iOS-26-4": ['
'{"name": "iPhone 17 Pro", "state": "Booted", "udid": "abc-123"}'
"]}}"
)
class _StubResult:
def __init__(self, stdout: str) -> None:
self.stdout = stdout
def _fake_run(cmd: List[str], **kwargs: object) -> _StubResult:
assert cmd[:2] == ["xcrun", "simctl"]
assert "booted" in cmd
return _StubResult(sample_json)
monkeypatch.setattr(pn_cli.subprocess, "run", _fake_run)
assert pn_cli._booted_ios_udid() == "abc-123"
def test_booted_ios_udid_returns_none_when_no_devices(monkeypatch: pytest.MonkeyPatch) -> None:
"""`_booted_ios_udid` returns ``None`` when nothing is booted."""
class _StubResult:
stdout = '{"devices": {}}'
monkeypatch.setattr(pn_cli.subprocess, "run", lambda *a, **kw: _StubResult())
assert pn_cli._booted_ios_udid() is None
def test_booted_ios_udid_handles_xcrun_missing(monkeypatch: pytest.MonkeyPatch) -> None:
"""`_booted_ios_udid` returns ``None`` when ``xcrun`` isn't on PATH."""
def _raise(*args: object, **kwargs: object) -> None:
raise FileNotFoundError("xcrun missing")
monkeypatch.setattr(pn_cli.subprocess, "run", _raise)
assert pn_cli._booted_ios_udid() is None
def test_hot_reload_manifest_payload_maps_files_to_modules(tmp_path: Path) -> None:
app_dir = tmp_path / "app"
app_dir.mkdir()
changed = app_dir / "main.py"
changed.write_text("print('hi')\n", encoding="utf-8")
payload = pn_cli._hot_reload_manifest_payload([os.fspath(changed)], os.fspath(tmp_path), version="v1")
assert payload == {
"version": "v1",
"files": ["app/main.py"],
"modules": ["app.main"],
}
def test_android_hot_reload_dest_points_to_overlay() -> None:
assert pn_cli._android_hot_reload_dest("app/main.py") == os.path.join(
"files",
"pythonnative_dev",
"app/main.py",
)
def test_clear_ios_hot_reload_overlay_removes_stale_files(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
overlay = tmp_path / "Documents" / "pythonnative_dev"
overlay.mkdir(parents=True)
(overlay / "reload.json").write_text("{}", encoding="utf-8")
monkeypatch.setattr(pn_cli, "_ios_data_container", lambda: os.fspath(tmp_path))
assert pn_cli._clear_ios_hot_reload_overlay() is True
assert not overlay.exists()
def test_run_hot_reload_imports_top_level_watcher(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
app_dir = tmp_path / "app"
app_dir.mkdir()
build_dir = tmp_path / "build"
events: list[str] = []
class FakeWatcher:
def __init__(self, watch_dir: str, on_change: object, interval: float = 1.0) -> None:
assert watch_dir == os.fspath(app_dir)
def start(self) -> None:
events.append("start")
def stop(self) -> None:
events.append("stop")
def stop_loop(_seconds: float) -> None:
raise KeyboardInterrupt
monkeypatch.setattr(hot_reload_module, "FileWatcher", FakeWatcher)
monkeypatch.setattr("time.sleep", stop_loop)
pn_cli._run_hot_reload("ios", os.fspath(tmp_path), os.fspath(build_dir), show_logs=False)
assert events == ["start", "stop"]