FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/tests/test_utils.py at v0.41.0 · pythonnative/pythonnative · GitHub
pythonnative
pythonnative
Repository navigation
Code
Issues
11
(11)
Pull requests
4
(4)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
tests
/
test_utils.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
177 lines (143 loc) · 7.37 KB
Breadcrumbs
pythonnative
/
tests
/
test_utils.py
Copy path
File metadata and controls
177 lines (143 loc) · 7.37 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
"""Tests for pythonnative.utils platform detection."""
import
os
import
sys
import
pytest
from
pythonnative
import
utils
from
pythonnative
.
utils
import
IS_ANDROID
,
IS_IOS
,
IS_WEB
,
_detect_ios
,
_detect_web
class
TestIosDetection
:
"""``_detect_ios()`` should key off explicit signals only, not on the
presence of optional packages.
"""
def
test_detects_via_pn_platform_env
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
monkeypatch
.
setenv
(
"PN_PLATFORM"
,
"ios"
)
assert
_detect_ios
()
is
True
def
test_other_pn_platform_values_ignored
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
monkeypatch
.
setenv
(
"PN_PLATFORM"
,
"web"
)
monkeypatch
.
delenv
(
"HOME"
,
raising
=
False
)
monkeypatch
.
setattr
(
sys
,
"platform"
,
"darwin"
)
assert
_detect_ios
()
is
False
def
test_detects_via_sys_platform_ios
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
monkeypatch
.
delenv
(
"PN_PLATFORM"
,
raising
=
False
)
monkeypatch
.
setattr
(
sys
,
"platform"
,
"ios"
)
assert
_detect_ios
()
is
True
def
test_core_simulator_home_alone_is_not_ios
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
# A macOS process whose HOME happens to point into a Simulator
# container (e.g. a host tool spawned by simctl) is still macOS;
# the embedded runtime reports sys.platform == "ios" itself.
monkeypatch
.
delenv
(
"PN_PLATFORM"
,
raising
=
False
)
monkeypatch
.
setattr
(
sys
,
"platform"
,
"darwin"
)
monkeypatch
.
setenv
(
"HOME"
,
"/Users/x/Library/Developer/CoreSimulator/Devices/ABCD/data"
,
)
assert
_detect_ios
()
is
False
def
test_plain_macos_is_not_ios
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
monkeypatch
.
delenv
(
"PN_PLATFORM"
,
raising
=
False
)
monkeypatch
.
setattr
(
sys
,
"platform"
,
"darwin"
)
monkeypatch
.
setenv
(
"HOME"
,
"/Users/owen"
)
assert
_detect_ios
()
is
False
def
test_plain_linux_is_not_ios
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
monkeypatch
.
delenv
(
"PN_PLATFORM"
,
raising
=
False
)
monkeypatch
.
setattr
(
sys
,
"platform"
,
"linux"
)
monkeypatch
.
setenv
(
"HOME"
,
"/home/runner"
)
assert
_detect_ios
()
is
False
class
TestWebDetection
:
"""``_detect_web()`` keys off ``PN_PLATFORM=web`` and nothing else.
Unlike Android and iOS, there is no host-level signal to fall back on: the
browser preview runs because ``pn preview`` or ``pn start`` asked for it.
"""
def
test_detects_via_pn_platform_env
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
monkeypatch
.
setenv
(
"PN_PLATFORM"
,
"web"
)
assert
_detect_web
()
is
True
def
test_unset_pn_platform_is_not_web
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
monkeypatch
.
delenv
(
"PN_PLATFORM"
,
raising
=
False
)
assert
_detect_web
()
is
False
def
test_empty_pn_platform_is_not_web
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
monkeypatch
.
setenv
(
"PN_PLATFORM"
,
""
)
assert
_detect_web
()
is
False
@
pytest
.
mark
.
parametrize
(
"platform"
, [
"ios"
,
"android"
,
"desktop"
])
def
test_other_pn_platform_values_ignored
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
,
platform
:
str
)
->
None
:
monkeypatch
.
setenv
(
"PN_PLATFORM"
,
platform
)
assert
_detect_web
()
is
False
def
test_host_platform_alone_is_not_web
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
"""Running on a laptop is not enough; only the explicit env var counts."""
monkeypatch
.
delenv
(
"PN_PLATFORM"
,
raising
=
False
)
monkeypatch
.
setattr
(
sys
,
"platform"
,
"darwin"
)
assert
_detect_web
()
is
False
def
_redetect
(
monkeypatch
:
pytest
.
MonkeyPatch
,
*
,
android
:
bool
,
ios
:
bool
,
web
:
bool
,
)
->
None
:
"""Re-run platform detection with each individual detector forced.
``_ensure_platform_detection()`` only computes a flag whose global is still
``None``, and all three were filled in at import time, so the caches are
cleared first. ``monkeypatch`` restores both the caches and the real
detectors when the test ends.
"""
monkeypatch
.
setattr
(
utils
,
"_is_android"
,
None
)
monkeypatch
.
setattr
(
utils
,
"_is_ios"
,
None
)
monkeypatch
.
setattr
(
utils
,
"_is_web"
,
None
)
monkeypatch
.
setattr
(
utils
,
"_detect_android"
,
lambda
:
android
)
monkeypatch
.
setattr
(
utils
,
"_detect_ios"
,
lambda
:
ios
)
monkeypatch
.
setattr
(
utils
,
"_detect_web"
,
lambda
:
web
)
utils
.
_ensure_platform_detection
()
class
TestPlatformFlagPrecedence
:
"""Android beats iOS beats web, so at most one flag is ever ``True``."""
def
test_web_wins_when_no_device_signal
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
_redetect
(
monkeypatch
,
android
=
False
,
ios
=
False
,
web
=
True
)
assert
utils
.
_get_is_web
()
is
True
assert
utils
.
_get_is_android
()
is
False
assert
utils
.
_get_is_ios
()
is
False
def
test_android_suppresses_web
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
"""A stale ``PN_PLATFORM=web`` must not fire on an Android device."""
_redetect
(
monkeypatch
,
android
=
True
,
ios
=
False
,
web
=
True
)
assert
utils
.
_get_is_android
()
is
True
assert
utils
.
_get_is_web
()
is
False
def
test_ios_suppresses_web
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
_redetect
(
monkeypatch
,
android
=
False
,
ios
=
True
,
web
=
True
)
assert
utils
.
_get_is_ios
()
is
True
assert
utils
.
_get_is_web
()
is
False
def
test_android_suppresses_ios
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
_redetect
(
monkeypatch
,
android
=
True
,
ios
=
True
,
web
=
False
)
assert
utils
.
_get_is_android
()
is
True
assert
utils
.
_get_is_ios
()
is
False
def
test_no_signal_leaves_every_flag_false
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
_redetect
(
monkeypatch
,
android
=
False
,
ios
=
False
,
web
=
False
)
assert
utils
.
_get_is_android
()
is
False
assert
utils
.
_get_is_ios
()
is
False
assert
utils
.
_get_is_web
()
is
False
@
pytest
.
mark
.
parametrize
(
"android"
, [
False
,
True
])
@
pytest
.
mark
.
parametrize
(
"ios"
, [
False
,
True
])
@
pytest
.
mark
.
parametrize
(
"web"
, [
False
,
True
])
def
test_at_most_one_flag_is_true
(
self
,
monkeypatch
:
pytest
.
MonkeyPatch
,
android
:
bool
,
ios
:
bool
,
web
:
bool
,
)
->
None
:
"""No combination of raw signals can light up two flags at once."""
_redetect
(
monkeypatch
,
android
=
android
,
ios
=
ios
,
web
=
web
)
flags
=
[
utils
.
_get_is_android
(),
utils
.
_get_is_ios
(),
utils
.
_get_is_web
()]
assert
sum
(
flags
)
<=
1
class
TestPlatformFlagsConsistency
:
def
test_all_flags_are_bools
(
self
)
->
None
:
assert
isinstance
(
IS_ANDROID
,
bool
)
assert
isinstance
(
IS_IOS
,
bool
)
assert
isinstance
(
IS_WEB
,
bool
)
def
test_flags_are_mutually_exclusive
(
self
)
->
None
:
# A single Python process is never simultaneously Android, iOS, and web.
assert
sum
([
IS_ANDROID
,
IS_IOS
,
IS_WEB
])
<=
1
def
test_ci_environment_has_no_platform
(
self
)
->
None
:
"""The test suite runs on Linux/macOS hosts, so all flags are False."""
# This is more of a smoke check of the import-time detection: if
# this ever starts being True on CI, someone has accidentally made
# platform detection overeager on non-device hosts.
if
os
.
environ
.
get
(
"PN_PLATFORM"
)
in
{
"ios"
,
"web"
}:
pytest
.
skip
(
"Running under an explicit PN_PLATFORM; flags correctly reflect that."
)
assert
IS_ANDROID
is
False
assert
IS_IOS
is
False
assert
IS_WEB
is
False
Back
|
FazBrowse Home
|
New Git URL