FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/tests/test_cli.py at main · nigelseah24/pythonnative · GitHub
nigelseah24
/
pythonnative
Public
forked from
pythonnative/pythonnative
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
tests
/
test_cli.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
694 lines (544 loc) · 25.8 KB
Breadcrumbs
pythonnative
/
tests
/
test_cli.py
Copy path
File metadata and controls
694 lines (544 loc) · 25.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
import
argparse
import
json
import
os
import
shutil
import
subprocess
import
sys
import
tempfile
from
pathlib
import
Path
from
typing
import
Callable
,
Dict
,
List
,
Optional
import
pytest
import
pythonnative
.
cli
.
pn
as
pn_cli
import
pythonnative
.
hot_reload
as
hot_reload_module
from
pythonnative
.
project
.
devices
import
Device
def
run_pn
(
args
:
List
[
str
],
cwd
:
str
,
env
:
Optional
[
Dict
[
str
,
str
]]
=
None
)
->
"subprocess.CompletedProcess[str]"
:
cmd
=
[
sys
.
executable
,
"-m"
,
"pythonnative.cli.pn"
]
+
args
return
subprocess
.
run
(
cmd
,
cwd
=
cwd
,
check
=
False
,
capture_output
=
True
,
text
=
True
,
env
=
env
)
def
test_cli_version
(
tmp_path
:
Path
)
->
None
:
result
=
run_pn
([
"--version"
],
str
(
tmp_path
))
assert
result
.
returncode
==
0
assert
result
.
stdout
.
strip
().
startswith
(
"pn "
)
def
test_cli_short_version_flag
(
tmp_path
:
Path
)
->
None
:
result
=
run_pn
([
"-V"
],
str
(
tmp_path
))
assert
result
.
returncode
==
0
assert
result
.
stdout
.
strip
().
startswith
(
"pn "
)
def
test_cli_init_and_clean
()
->
None
:
tmpdir
=
tempfile
.
mkdtemp
(
prefix
=
"pn_cli_test_"
)
try
:
result
=
run_pn
([
"init"
,
"my_app"
],
tmpdir
)
assert
result
.
returncode
==
0
,
result
.
stderr
project_dir
=
os
.
path
.
join
(
tmpdir
,
"my_app"
)
assert
os
.
path
.
isdir
(
os
.
path
.
join
(
project_dir
,
"app"
))
main_path
=
os
.
path
.
join
(
project_dir
,
"app"
,
"main.py"
)
assert
os
.
path
.
isfile
(
main_path
)
content
=
Path
(
main_path
).
read_text
(
encoding
=
"utf-8"
)
assert
"def App("
in
content
assert
"Stack.Navigator"
in
content
config_path
=
os
.
path
.
join
(
project_dir
,
"pythonnative.toml"
)
assert
os
.
path
.
isfile
(
config_path
)
toml_text
=
Path
(
config_path
).
read_text
(
encoding
=
"utf-8"
)
assert
'id = "com.example.my_app"'
in
toml_text
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
project_dir
,
".gitignore"
))
# The legacy JSON config and requirements.txt are no longer scaffolded.
assert
not
os
.
path
.
exists
(
os
.
path
.
join
(
project_dir
,
"pythonnative.json"
))
# clean on empty build is a no-op
result
=
run_pn
([
"clean"
],
tmpdir
)
assert
result
.
returncode
==
0
,
result
.
stderr
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_init_refuses_overwrite
()
->
None
:
tmpdir
=
tempfile
.
mkdtemp
(
prefix
=
"pn_cli_test_"
)
try
:
assert
run_pn
([
"init"
,
"my_app"
],
tmpdir
).
returncode
==
0
result
=
run_pn
([
"init"
,
"my_app"
],
tmpdir
)
assert
result
.
returncode
!=
0
assert
"Refusing to overwrite"
in
result
.
stdout
assert
run_pn
([
"init"
,
"my_app"
,
"--force"
],
tmpdir
).
returncode
==
0
finally
:
shutil
.
rmtree
(
tmpdir
,
ignore_errors
=
True
)
def
test_cli_init_creates_named_directory
(
tmp_path
:
Path
)
->
None
:
result
=
run_pn
([
"init"
,
"my_app"
],
str
(
tmp_path
))
assert
result
.
returncode
==
0
,
result
.
stderr
assert
"cd my_app"
in
result
.
stdout
project_dir
=
os
.
path
.
join
(
str
(
tmp_path
),
"my_app"
)
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
project_dir
,
"app"
,
"main.py"
))
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
project_dir
,
"pythonnative.toml"
))
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
project_dir
,
".gitignore"
))
toml_text
=
Path
(
os
.
path
.
join
(
project_dir
,
"pythonnative.toml"
)).
read_text
(
encoding
=
"utf-8"
)
assert
'id = "com.example.my_app"'
in
toml_text
# Nothing is scaffolded beside the project directory.
assert
os
.
listdir
(
str
(
tmp_path
))
==
[
"my_app"
]
def
test_cli_init_without_name_uses_cwd
(
tmp_path
:
Path
)
->
None
:
project_dir
=
tmp_path
/
"widgets"
project_dir
.
mkdir
()
result
=
run_pn
([
"init"
],
str
(
project_dir
))
assert
result
.
returncode
==
0
,
result
.
stderr
assert
"cd "
not
in
result
.
stdout
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
str
(
project_dir
),
"app"
,
"main.py"
))
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
str
(
project_dir
),
".gitignore"
))
toml_text
=
Path
(
os
.
path
.
join
(
str
(
project_dir
),
"pythonnative.toml"
)).
read_text
(
encoding
=
"utf-8"
)
assert
'id = "com.example.widgets"'
in
toml_text
assert
'name = "widgets"'
in
toml_text
def
test_cli_init_refuses_non_empty_directory
(
tmp_path
:
Path
)
->
None
:
project_dir
=
tmp_path
/
"my_app"
project_dir
.
mkdir
()
keeper
=
project_dir
/
"README.md"
keeper
.
write_text
(
"keep me
\n
"
,
encoding
=
"utf-8"
)
result
=
run_pn
([
"init"
,
"my_app"
],
str
(
tmp_path
))
assert
result
.
returncode
!=
0
assert
"Refusing to overwrite"
in
result
.
stdout
assert
"non-empty directory"
in
result
.
stdout
assert
not
os
.
path
.
exists
(
os
.
path
.
join
(
str
(
project_dir
),
"app"
))
assert
keeper
.
read_text
(
encoding
=
"utf-8"
)
==
"keep me
\n
"
result
=
run_pn
([
"init"
,
"my_app"
,
"--force"
],
str
(
tmp_path
))
assert
result
.
returncode
==
0
,
result
.
stderr
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
str
(
project_dir
),
"app"
,
"main.py"
))
# --force scaffolds over the directory; it doesn't empty it first.
assert
keeper
.
read_text
(
encoding
=
"utf-8"
)
==
"keep me
\n
"
def
test_cli_init_accepts_existing_empty_directory
(
tmp_path
:
Path
)
->
None
:
project_dir
=
tmp_path
/
"my_app"
project_dir
.
mkdir
()
result
=
run_pn
([
"init"
,
"my_app"
],
str
(
tmp_path
))
assert
result
.
returncode
==
0
,
result
.
stderr
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
str
(
project_dir
),
"app"
,
"main.py"
))
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
str
(
project_dir
),
"pythonnative.toml"
))
def
test_cli_init_refuses_existing_file
(
tmp_path
:
Path
)
->
None
:
blocker
=
tmp_path
/
"my_app"
blocker
.
write_text
(
"not a project
\n
"
,
encoding
=
"utf-8"
)
result
=
run_pn
([
"init"
,
"my_app"
],
str
(
tmp_path
))
assert
result
.
returncode
!=
0
assert
"Refusing to overwrite existing file"
in
result
.
stdout
# --force can't turn a file into a directory, so it is refused too.
result
=
run_pn
([
"init"
,
"my_app"
,
"--force"
],
str
(
tmp_path
))
assert
result
.
returncode
!=
0
assert
"Refusing to overwrite existing file"
in
result
.
stdout
assert
blocker
.
read_text
(
encoding
=
"utf-8"
)
==
"not a project
\n
"
@
pytest
.
mark
.
parametrize
(
"name"
, [
"{absolute}"
,
"nested/my_app"
,
"my_app/"
,
"."
,
".."
,
"../"
,
"a/.."
])
def
test_cli_init_rejects_path_like_names
(
tmp_path
:
Path
,
name
:
str
)
->
None
:
work_dir
=
tmp_path
/
"work"
work_dir
.
mkdir
()
result
=
run_pn
([
"init"
,
name
.
format
(
absolute
=
str
(
tmp_path
/
"elsewhere"
))],
str
(
work_dir
))
assert
result
.
returncode
!=
0
assert
"Refusing to"
in
result
.
stdout
assert
"project name"
in
result
.
stdout
# Nothing was created in the working directory or anywhere above it.
assert
os
.
listdir
(
str
(
work_dir
))
==
[]
assert
os
.
listdir
(
str
(
tmp_path
))
==
[
"work"
]
def
test_cli_init_force_does_not_escape_to_parent
(
tmp_path
:
Path
)
->
None
:
parent_config
=
tmp_path
/
"pythonnative.toml"
parent_config
.
write_text
(
"# hand-written
\n
"
,
encoding
=
"utf-8"
)
work_dir
=
tmp_path
/
"work"
work_dir
.
mkdir
()
result
=
run_pn
([
"init"
,
".."
,
"--force"
],
str
(
work_dir
))
assert
result
.
returncode
!=
0
assert
"Refusing to"
in
result
.
stdout
# --force does not lift the single-name rule, so the parent is untouched.
assert
parent_config
.
read_text
(
encoding
=
"utf-8"
)
==
"# hand-written
\n
"
assert
not
os
.
path
.
exists
(
os
.
path
.
join
(
str
(
tmp_path
),
"app"
))
assert
not
os
.
path
.
exists
(
os
.
path
.
join
(
str
(
tmp_path
),
".gitignore"
))
assert
os
.
listdir
(
str
(
work_dir
))
==
[]
@
pytest
.
mark
.
parametrize
(
"extra_args"
, [[], [
"--force"
]])
@
pytest
.
mark
.
parametrize
(
"populated"
, [
True
,
False
])
def
test_cli_init_rejects_symlinked_target
(
tmp_path
:
Path
,
populated
:
bool
,
extra_args
:
List
[
str
])
->
None
:
outside
=
tmp_path
/
"outside"
outside
.
mkdir
()
outside_config
=
outside
/
"pythonnative.toml"
if
populated
:
outside_config
.
write_text
(
"# hand-written
\n
"
,
encoding
=
"utf-8"
)
before
=
sorted
(
os
.
listdir
(
str
(
outside
)))
work_dir
=
tmp_path
/
"work"
work_dir
.
mkdir
()
os
.
symlink
(
str
(
outside
),
os
.
path
.
join
(
str
(
work_dir
),
"link"
))
result
=
run_pn
([
"init"
,
"link"
]
+
extra_args
,
str
(
work_dir
))
assert
result
.
returncode
!=
0
assert
"Refusing to"
in
result
.
stdout
# exists() and is_dir() follow symlinks, so the destination must stay untouched:
# no new entries, and no rewrite of a config that was already there.
assert
sorted
(
os
.
listdir
(
str
(
outside
)))
==
before
if
populated
:
assert
outside_config
.
read_text
(
encoding
=
"utf-8"
)
==
"# hand-written
\n
"
assert
os
.
listdir
(
str
(
work_dir
))
==
[
"link"
]
_ILLEGAL_NAMES
=
[
pytest
.
param
(
"MyApp"
,
id
=
"uppercase"
),
pytest
.
param
(
"my app"
,
id
=
"space"
),
pytest
.
param
(
'bad"name'
,
id
=
"quote"
),
pytest
.
param
(
"9lives"
,
id
=
"leading-digit"
),
pytest
.
param
(
"_leading"
,
id
=
"leading-underscore"
),
pytest
.
param
(
"café"
,
id
=
"non-ascii"
),
pytest
.
param
(
""
,
id
=
"empty"
),
# `$` matches before a trailing newline, so `match` would let this through.
pytest
.
param
(
"app
\n
"
,
id
=
"trailing-newline"
),
]
@
pytest
.
mark
.
parametrize
(
"name"
,
_ILLEGAL_NAMES
)
def
test_cli_init_rejects_illegal_names
(
tmp_path
:
Path
,
name
:
str
)
->
None
:
result
=
run_pn
([
"init"
,
name
],
str
(
tmp_path
))
assert
result
.
returncode
!=
0
assert
"Invalid project name"
in
result
.
stdout
assert
"Try: "
in
result
.
stdout
# Nothing is written, not even into the current directory.
assert
os
.
listdir
(
str
(
tmp_path
))
==
[]
@
pytest
.
mark
.
parametrize
(
"name"
,
_ILLEGAL_NAMES
)
def
test_cli_init_force_does_not_lift_name_validation
(
tmp_path
:
Path
,
name
:
str
)
->
None
:
result
=
run_pn
([
"init"
,
name
,
"--force"
],
str
(
tmp_path
))
assert
result
.
returncode
!=
0
assert
"Invalid project name"
in
result
.
stdout
assert
os
.
listdir
(
str
(
tmp_path
))
==
[]
def
test_cli_init_suggestion_is_itself_a_legal_name
()
->
None
:
# The suggestion is only useful if the user can paste it straight back.
awkward
=
[
"MyApp"
,
"my app"
,
'bad"name'
,
"9lives"
,
"_leading"
,
"café"
,
""
,
"___"
,
"---"
,
"123"
,
".."
,
"a/b"
,
"ÄÖÜ"
,
"
\t
"
,
"app
\n
"
,
"app
\n
\n
"
,
"
\n
app"
,
"
\n
"
,
"
\x00
"
,
"
\x7f
"
,
"sp ace"
,
"trailing_"
,
"-lead"
,
"mixed_-CASE-99"
,
"🙂"
,
"a"
*
200
,
]
for
name
in
awkward
:
assert
pn_cli
.
_NAME_RE
.
fullmatch
(
pn_cli
.
_sanitize_name
(
name
)),
name
def
test_cli_init_suggestion_is_stable_for_legal_names
()
->
None
:
for
name
in
[
"my_app"
,
"my-app"
,
"a"
,
"a1"
,
"hello-world_2"
]:
assert
pn_cli
.
_sanitize_name
(
name
)
==
name
def
test_cli_init_without_name_accepts_a_cwd_that_fails_the_pattern
(
tmp_path
:
Path
)
->
None
:
# Validation covers the typed name only. A directory named MyProject is
# extremely ordinary, and `pn init` there must keep working.
project_dir
=
tmp_path
/
"MyProject"
project_dir
.
mkdir
()
result
=
run_pn
([
"init"
],
str
(
project_dir
))
assert
result
.
returncode
==
0
,
result
.
stdout
toml_text
=
(
project_dir
/
"pythonnative.toml"
).
read_text
(
encoding
=
"utf-8"
)
assert
'name = "MyProject"'
in
toml_text
assert
'id = "com.example.myproject"'
in
toml_text
def
test_cli_run_help_lists_flags
()
->
None
:
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_build_help_lists_debug
()
->
None
:
tmpdir
=
tempfile
.
mkdtemp
(
prefix
=
"pn_cli_test_"
)
try
:
result
=
run_pn
([
"build"
,
"--help"
],
tmpdir
)
assert
result
.
returncode
==
0
,
result
.
stderr
assert
"--debug"
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_without_config_errors
()
->
None
:
tmpdir
=
tempfile
.
mkdtemp
(
prefix
=
"pn_cli_test_"
)
try
:
result
=
run_pn
([
"run"
,
"android"
],
tmpdir
)
assert
result
.
returncode
!=
0
assert
"No pythonnative.toml"
in
(
result
.
stdout
+
result
.
stderr
)
finally
:
shutil
.
rmtree
(
tmpdir
,
ignore_errors
=
True
)
def
test_cli_app_id_resolves
(
tmp_path
:
Path
)
->
None
:
assert
run_pn
([
"init"
,
"my_app"
],
str
(
tmp_path
)).
returncode
==
0
project_dir
=
str
(
tmp_path
/
"my_app"
)
result
=
run_pn
([
"app-id"
,
"android"
],
project_dir
)
assert
result
.
returncode
==
0
,
result
.
stderr
assert
result
.
stdout
.
strip
()
==
"com.example.my_app"
assert
run_pn
([
"app-id"
,
"ios"
],
project_dir
).
stdout
.
strip
()
==
"com.example.my_app"
def
test_cli_doctor_runs
(
tmp_path
:
Path
)
->
None
:
assert
run_pn
([
"init"
,
"my_app"
],
str
(
tmp_path
)).
returncode
==
0
result
=
run_pn
([
"doctor"
,
"android"
],
str
(
tmp_path
/
"my_app"
))
assert
"PythonNative doctor"
in
result
.
stdout
# android-only doctor on a CI box without adb still produces warnings, not errors.
assert
result
.
returncode
in
(
0
,
1
)
def
test_cli_run_prepare_only_android_and_ios
()
->
None
:
tmpdir
=
tempfile
.
mkdtemp
(
prefix
=
"pn_cli_test_"
)
try
:
assert
run_pn
([
"init"
,
"my_app"
],
tmpdir
).
returncode
==
0
project_dir
=
os
.
path
.
join
(
tmpdir
,
"my_app"
)
result
=
run_pn
([
"run"
,
"android"
,
"--prepare-only"
,
"--no-logs"
],
project_dir
)
assert
result
.
returncode
==
0
,
result
.
stderr
android_root
=
os
.
path
.
join
(
project_dir
,
"build"
,
"android"
,
"android_template"
)
assert
os
.
path
.
isdir
(
android_root
)
# Package relocated to the configured application id.
relocated
=
os
.
path
.
join
(
android_root
,
"app"
,
"src"
,
"main"
,
"java"
,
"com"
,
"example"
,
"my_app"
,
"ScreenFragment.kt"
)
assert
os
.
path
.
isfile
(
relocated
)
assert
not
os
.
path
.
exists
(
os
.
path
.
join
(
android_root
,
"app"
,
"src"
,
"main"
,
"java"
,
"com"
,
"pythonnative"
,
"android_template"
)
)
# App identity written into the Gradle config.
gradle
=
Path
(
os
.
path
.
join
(
android_root
,
"app"
,
"build.gradle"
)).
read_text
(
encoding
=
"utf-8"
)
assert
"com.example.my_app"
in
gradle
result
=
run_pn
([
"run"
,
"ios"
,
"--prepare-only"
,
"--no-logs"
],
project_dir
)
assert
result
.
returncode
==
0
,
result
.
stderr
ios_root
=
os
.
path
.
join
(
project_dir
,
"build"
,
"ios"
,
"ios_template"
)
assert
os
.
path
.
isdir
(
ios_root
)
info_plist
=
Path
(
os
.
path
.
join
(
ios_root
,
"ios_template"
,
"Info.plist"
)).
read_bytes
()
assert
b"CFBundleDisplayName"
in
info_plist
finally
:
shutil
.
rmtree
(
tmpdir
,
ignore_errors
=
True
)
# ---------------------------------------------------------------------------
# Pure helpers (no device required)
# ---------------------------------------------------------------------------
def
test_booted_ios_udid_picks_first_booted_device
(
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
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
:
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
:
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
# `pn devices` tests run devices_command() in-process rather than through run_pn,
# because run_pn spawns a child interpreter that monkeypatch can't reach: it would
# re-import the real list_devices and shell out to adb/xcrun, making the result
# depend on whatever hardware the test machine has attached. The one run_pn test
# below empties PATH instead, so every list_* call hits FileNotFoundError.
# A naive `state == "booted"` readiness check disagrees with is_ready on two of
# these: the connected Android device (ready, not booted) and the shutdown
# simulator (ready, because simulators boot on demand).
_FAKE_DEVICES
=
[
Device
(
"android"
,
"device"
,
"R5CT12345XYZ"
,
"Pixel 8 Pro"
,
""
,
"connected"
),
Device
(
"android"
,
"device"
,
"OFFLINE99"
,
"Galaxy S24"
,
""
,
"offline"
),
Device
(
"ios"
,
"simulator"
,
"ABC-123"
,
"iPhone 17 Pro"
,
"iOS 26.4"
,
"booted"
),
Device
(
"ios"
,
"simulator"
,
"DEF-456"
,
"iPad Pro 13-inch (M4)"
,
"iOS 26.4"
,
"shutdown"
),
]
_NO_DEVICES_HINTS
=
(
"No devices found.
\n
"
"Android: start an emulator or connect a device with USB debugging enabled.
\n
"
"iOS: open Xcode once to install Simulators, or plug in a device.
\n
"
)
_DEVICE_TABLE
=
(
" IDENTIFIER KIND STATE NAME
\n
"
" R5CT12345XYZ device connected Pixel 8 Pro
\n
"
" OFFLINE99 device offline Galaxy S24
\n
"
" ABC-123 simulator booted iPhone 17 Pro (iOS 26.4)
\n
"
" DEF-456 simulator shutdown iPad Pro 13-inch (M4) (iOS 26.4)
\n
"
"
\n
"
"Target one with: pn run <platform> --device <identifier or name>
\n
"
)
def
_fake_list_devices
(
devices
:
List
[
Device
],
calls
:
Optional
[
List
[
Optional
[
str
]]]
=
None
)
->
Callable
[...,
List
[
Device
]]:
"""Stand in for list_devices, filtering by platform and recording the argument."""
def
_list
(
platform
:
Optional
[
str
]
=
None
)
->
List
[
Device
]:
if
calls
is
not
None
:
calls
.
append
(
platform
)
return
[
device
for
device
in
devices
if
platform
in
(
None
,
device
.
platform
)]
return
_list
def
test_devices_json_emits_parseable_array
(
monkeypatch
:
pytest
.
MonkeyPatch
,
capsys
:
pytest
.
CaptureFixture
[
str
]
)
->
None
:
monkeypatch
.
setattr
(
pn_cli
.
devices_mod
,
"list_devices"
,
_fake_list_devices
(
_FAKE_DEVICES
))
# Returns rather than exiting, so a populated listing stays exit 0.
pn_cli
.
devices_command
(
argparse
.
Namespace
(
platform
=
None
,
json
=
True
))
payload
=
json
.
loads
(
capsys
.
readouterr
().
out
)
assert
[
entry
[
"identifier"
]
for
entry
in
payload
]
==
[
"R5CT12345XYZ"
,
"OFFLINE99"
,
"ABC-123"
,
"DEF-456"
,
]
for
entry
in
payload
:
assert
set
(
entry
)
==
{
"platform"
,
"kind"
,
"identifier"
,
"name"
,
"os_version"
,
"state"
,
"is_ready"
,
}
ready
=
{
entry
[
"identifier"
]:
entry
[
"is_ready"
]
for
entry
in
payload
}
assert
ready
==
{
"R5CT12345XYZ"
:
True
,
"OFFLINE99"
:
False
,
"ABC-123"
:
True
,
"DEF-456"
:
True
}
# The two entries a naive `state == "booted"` check would get wrong.
by_id
=
{
entry
[
"identifier"
]:
entry
for
entry
in
payload
}
assert
by_id
[
"DEF-456"
][
"state"
]
==
"shutdown"
and
by_id
[
"DEF-456"
][
"is_ready"
]
is
True
assert
by_id
[
"R5CT12345XYZ"
][
"state"
]
==
"connected"
and
by_id
[
"R5CT12345XYZ"
][
"is_ready"
]
is
True
assert
by_id
[
"OFFLINE99"
][
"kind"
]
==
"device"
and
by_id
[
"OFFLINE99"
][
"is_ready"
]
is
False
def
test_devices_json_empty_prints_array_and_hints_to_stderr
(
monkeypatch
:
pytest
.
MonkeyPatch
,
capsys
:
pytest
.
CaptureFixture
[
str
]
)
->
None
:
monkeypatch
.
setattr
(
pn_cli
.
devices_mod
,
"list_devices"
,
_fake_list_devices
([]))
# No SystemExit: an empty result is exit 0 under --json, unlike the table.
pn_cli
.
devices_command
(
argparse
.
Namespace
(
platform
=
None
,
json
=
True
))
captured
=
capsys
.
readouterr
()
assert
captured
.
out
==
"[]
\n
"
assert
json
.
loads
(
captured
.
out
)
==
[]
assert
captured
.
err
==
_NO_DEVICES_HINTS
@
pytest
.
mark
.
parametrize
(
(
"platform"
,
"expected"
),
[(
"android"
, [
"R5CT12345XYZ"
,
"OFFLINE99"
]), (
"ios"
, [
"ABC-123"
,
"DEF-456"
])],
)
def
test_devices_json_respects_platform_filter
(
monkeypatch
:
pytest
.
MonkeyPatch
,
capsys
:
pytest
.
CaptureFixture
[
str
],
platform
:
str
,
expected
:
List
[
str
],
)
->
None
:
calls
:
List
[
Optional
[
str
]]
=
[]
monkeypatch
.
setattr
(
pn_cli
.
devices_mod
,
"list_devices"
,
_fake_list_devices
(
_FAKE_DEVICES
,
calls
))
pn_cli
.
devices_command
(
argparse
.
Namespace
(
platform
=
platform
,
json
=
True
))
assert
calls
==
[
platform
]
payload
=
json
.
loads
(
capsys
.
readouterr
().
out
)
assert
{
entry
[
"platform"
]
for
entry
in
payload
}
==
{
platform
}
assert
[
entry
[
"identifier"
]
for
entry
in
payload
]
==
expected
@
pytest
.
mark
.
parametrize
(
"devices"
, [[],
_FAKE_DEVICES
],
ids
=
[
"empty"
,
"populated"
])
def
test_devices_json_keeps_human_text_off_stdout
(
monkeypatch
:
pytest
.
MonkeyPatch
,
capsys
:
pytest
.
CaptureFixture
[
str
],
devices
:
List
[
Device
]
)
->
None
:
monkeypatch
.
setattr
(
pn_cli
.
devices_mod
,
"list_devices"
,
_fake_list_devices
(
devices
))
pn_cli
.
devices_command
(
argparse
.
Namespace
(
platform
=
None
,
json
=
True
))
out
=
capsys
.
readouterr
().
out
json
.
loads
(
out
)
# Every non-JSON source the table mode prints: the three hints, the column
# header, each aligned row, and the trailer.
for
hint
in
_NO_DEVICES_HINTS
.
splitlines
():
assert
hint
not
in
out
assert
"IDENTIFIER"
not
in
out
assert
"Target one with:"
not
in
out
for
device
in
devices
:
assert
device
.
format
()
not
in
out
def
test_devices_human_output_unchanged_when_empty
(
tmp_path
:
Path
)
->
None
:
# An empty PATH makes adb/xcrun unresolvable, so every list_* call returns [].
env
=
{
**
os
.
environ
,
"PATH"
:
str
(
tmp_path
)}
result
=
run_pn
([
"devices"
],
str
(
tmp_path
),
env
=
env
)
assert
result
.
returncode
==
1
assert
result
.
stdout
==
_NO_DEVICES_HINTS
assert
result
.
stderr
==
""
def
test_devices_human_output_unchanged_when_populated
(
monkeypatch
:
pytest
.
MonkeyPatch
,
capsys
:
pytest
.
CaptureFixture
[
str
]
)
->
None
:
monkeypatch
.
setattr
(
pn_cli
.
devices_mod
,
"list_devices"
,
_fake_list_devices
(
_FAKE_DEVICES
))
pn_cli
.
devices_command
(
argparse
.
Namespace
(
platform
=
None
,
json
=
False
))
captured
=
capsys
.
readouterr
()
assert
captured
.
out
==
_DEVICE_TABLE
assert
captured
.
err
==
""
def
test_devices_json_flag_is_wired_through_argparse
(
tmp_path
:
Path
)
->
None
:
# The other JSON tests call devices_command() directly, so only this one
# proves the subparser actually accepts --json and routes it through.
env
=
{
**
os
.
environ
,
"PATH"
:
str
(
tmp_path
)}
result
=
run_pn
([
"devices"
,
"--json"
],
str
(
tmp_path
),
env
=
env
)
assert
result
.
returncode
==
0
assert
json
.
loads
(
result
.
stdout
)
==
[]
assert
result
.
stdout
==
"[]
\n
"
assert
result
.
stderr
==
_NO_DEVICES_HINTS
def
test_devices_json_serializes_awkward_field_values
(
monkeypatch
:
pytest
.
MonkeyPatch
,
capsys
:
pytest
.
CaptureFixture
[
str
]
)
->
None
:
# Quotes, a newline, non-ASCII, and the two empty-string defaults all survive
# the round trip today; nothing pinned them before.
awkward
=
Device
(
"android"
,
"device"
,
"SER!@#123"
,
'M
\u00e1
laga "Pixel"
\n
Lab'
,
""
,
""
)
monkeypatch
.
setattr
(
pn_cli
.
devices_mod
,
"list_devices"
,
_fake_list_devices
([
awkward
]))
pn_cli
.
devices_command
(
argparse
.
Namespace
(
platform
=
None
,
json
=
True
))
(
entry
,)
=
json
.
loads
(
capsys
.
readouterr
().
out
)
assert
entry
==
{
"platform"
:
"android"
,
"kind"
:
"device"
,
"identifier"
:
"SER!@#123"
,
"name"
:
'M
\u00e1
laga "Pixel"
\n
Lab'
,
"os_version"
:
""
,
"state"
:
""
,
"is_ready"
:
False
,
}
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
bundle_id
:
os
.
fspath
(
tmp_path
))
assert
pn_cli
.
_clear_ios_hot_reload_overlay
(
"com.example.app"
)
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
),
app_id
=
"com.example.app"
,
bundle_id
=
"com.example.app"
,
show_logs
=
False
,
)
assert
events
==
[
"start"
,
"stop"
]
Back
|
FazBrowse Home
|
New Git URL