|
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
8 | | -from pythonnative.utils import IS_ANDROID, IS_IOS, _detect_ios |
| 8 | +from pythonnative import utils |
| 9 | +from pythonnative.utils import IS_ANDROID, IS_IOS, IS_WEB, _detect_ios, _detect_web |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class TestIosDetection: |
@@ -53,21 +54,124 @@ def test_plain_linux_is_not_ios(self, monkeypatch: pytest.MonkeyPatch) -> None: |
53 | 54 | assert _detect_ios() is False |
54 | 55 |
|
55 | 56 |
|
| 57 | +class TestWebDetection: |
| 58 | + """``_detect_web()`` keys off ``PN_PLATFORM=web`` and nothing else. |
| 59 | +
|
| 60 | + Unlike Android and iOS, there is no host-level signal to fall back on: the |
| 61 | + browser preview runs because ``pn preview`` or ``pn start`` asked for it. |
| 62 | + """ |
| 63 | + |
| 64 | + def test_detects_via_pn_platform_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 65 | + monkeypatch.setenv("PN_PLATFORM", "web") |
| 66 | + assert _detect_web() is True |
| 67 | + |
| 68 | + def test_unset_pn_platform_is_not_web(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 69 | + monkeypatch.delenv("PN_PLATFORM", raising=False) |
| 70 | + assert _detect_web() is False |
| 71 | + |
| 72 | + def test_empty_pn_platform_is_not_web(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 73 | + monkeypatch.setenv("PN_PLATFORM", "") |
| 74 | + assert _detect_web() is False |
| 75 | + |
| 76 | + @pytest.mark.parametrize("platform", ["ios", "android", "desktop"]) |
| 77 | + def test_other_pn_platform_values_ignored(self, monkeypatch: pytest.MonkeyPatch, platform: str) -> None: |
| 78 | + monkeypatch.setenv("PN_PLATFORM", platform) |
| 79 | + assert _detect_web() is False |
| 80 | + |
| 81 | + def test_host_platform_alone_is_not_web(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 82 | + """Running on a laptop is not enough; only the explicit env var counts.""" |
| 83 | + monkeypatch.delenv("PN_PLATFORM", raising=False) |
| 84 | + monkeypatch.setattr(sys, "platform", "darwin") |
| 85 | + assert _detect_web() is False |
| 86 | + |
| 87 | + |
| 88 | +def _redetect( |
| 89 | + monkeypatch: pytest.MonkeyPatch, |
| 90 | + *, |
| 91 | + android: bool, |
| 92 | + ios: bool, |
| 93 | + web: bool, |
| 94 | +) -> None: |
| 95 | + """Re-run platform detection with each individual detector forced. |
| 96 | +
|
| 97 | + ``_ensure_platform_detection()`` only computes a flag whose global is still |
| 98 | + ``None``, and all three were filled in at import time, so the caches are |
| 99 | + cleared first. ``monkeypatch`` restores both the caches and the real |
| 100 | + detectors when the test ends. |
| 101 | + """ |
| 102 | + monkeypatch.setattr(utils, "_is_android", None) |
| 103 | + monkeypatch.setattr(utils, "_is_ios", None) |
| 104 | + monkeypatch.setattr(utils, "_is_web", None) |
| 105 | + monkeypatch.setattr(utils, "_detect_android", lambda: android) |
| 106 | + monkeypatch.setattr(utils, "_detect_ios", lambda: ios) |
| 107 | + monkeypatch.setattr(utils, "_detect_web", lambda: web) |
| 108 | + utils._ensure_platform_detection() |
| 109 | + |
| 110 | + |
| 111 | +class TestPlatformFlagPrecedence: |
| 112 | + """Android beats iOS beats web, so at most one flag is ever ``True``.""" |
| 113 | + |
| 114 | + def test_web_wins_when_no_device_signal(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 115 | + _redetect(monkeypatch, android=False, ios=False, web=True) |
| 116 | + assert utils._get_is_web() is True |
| 117 | + assert utils._get_is_android() is False |
| 118 | + assert utils._get_is_ios() is False |
| 119 | + |
| 120 | + def test_android_suppresses_web(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 121 | + """A stale ``PN_PLATFORM=web`` must not fire on an Android device.""" |
| 122 | + _redetect(monkeypatch, android=True, ios=False, web=True) |
| 123 | + assert utils._get_is_android() is True |
| 124 | + assert utils._get_is_web() is False |
| 125 | + |
| 126 | + def test_ios_suppresses_web(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 127 | + _redetect(monkeypatch, android=False, ios=True, web=True) |
| 128 | + assert utils._get_is_ios() is True |
| 129 | + assert utils._get_is_web() is False |
| 130 | + |
| 131 | + def test_android_suppresses_ios(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 132 | + _redetect(monkeypatch, android=True, ios=True, web=False) |
| 133 | + assert utils._get_is_android() is True |
| 134 | + assert utils._get_is_ios() is False |
| 135 | + |
| 136 | + def test_no_signal_leaves_every_flag_false(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 137 | + _redetect(monkeypatch, android=False, ios=False, web=False) |
| 138 | + assert utils._get_is_android() is False |
| 139 | + assert utils._get_is_ios() is False |
| 140 | + assert utils._get_is_web() is False |
| 141 | + |
| 142 | + @pytest.mark.parametrize("android", [False, True]) |
| 143 | + @pytest.mark.parametrize("ios", [False, True]) |
| 144 | + @pytest.mark.parametrize("web", [False, True]) |
| 145 | + def test_at_most_one_flag_is_true( |
| 146 | + self, |
| 147 | + monkeypatch: pytest.MonkeyPatch, |
| 148 | + android: bool, |
| 149 | + ios: bool, |
| 150 | + web: bool, |
| 151 | + ) -> None: |
| 152 | + """No combination of raw signals can light up two flags at once.""" |
| 153 | + _redetect(monkeypatch, android=android, ios=ios, web=web) |
| 154 | + flags = [utils._get_is_android(), utils._get_is_ios(), utils._get_is_web()] |
| 155 | + assert sum(flags) <= 1 |
| 156 | + |
| 157 | + |
56 | 158 | class TestPlatformFlagsConsistency: |
57 | | - def test_both_flags_are_bools(self) -> None: |
| 159 | + def test_all_flags_are_bools(self) -> None: |
58 | 160 | assert isinstance(IS_ANDROID, bool) |
59 | 161 | assert isinstance(IS_IOS, bool) |
| 162 | + assert isinstance(IS_WEB, bool) |
60 | 163 |
|
61 | 164 | def test_flags_are_mutually_exclusive(self) -> None: |
62 | | - # A single Python process is never simultaneously Android and iOS. |
63 | | - assert not (IS_ANDROID and IS_IOS) |
| 165 | + # A single Python process is never simultaneously Android, iOS, and web. |
| 166 | + assert sum([IS_ANDROID, IS_IOS, IS_WEB]) <= 1 |
64 | 167 |
|
65 | | - def test_ci_environment_is_neither(self) -> None: |
66 | | - """The test suite runs on Linux/macOS hosts, so both flags are False.""" |
| 168 | + def test_ci_environment_has_no_platform(self) -> None: |
| 169 | + """The test suite runs on Linux/macOS hosts, so all flags are False.""" |
67 | 170 | # This is more of a smoke check of the import-time detection: if |
68 | 171 | # this ever starts being True on CI, someone has accidentally made |
69 | 172 | # platform detection overeager on non-device hosts. |
70 | | - if os.environ.get("PN_PLATFORM") == "ios": |
71 | | - pytest.skip("Running under PN_PLATFORM=ios; flags correctly reflect that.") |
| 173 | + if os.environ.get("PN_PLATFORM") in {"ios", "web"}: |
| 174 | + pytest.skip("Running under an explicit PN_PLATFORM; flags correctly reflect that.") |
72 | 175 | assert IS_ANDROID is False |
73 | 176 | assert IS_IOS is False |
| 177 | + assert IS_WEB is False |
0 commit comments