File: test_select_target_device.py

package info (click to toggle)
python-briefcase 0.3.22-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,296 kB
  • sloc: python: 59,405; makefile: 57
file content (338 lines) | stat: -rw-r--r-- 12,524 bytes parent folder | download
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
from unittest import mock

import pytest

from briefcase.commands.base import BaseCommand
from briefcase.console import Console
from briefcase.exceptions import BriefcaseCommandError, InvalidDeviceError
from briefcase.platforms.iOS.xcode import iOSXcodeMixin
from tests.utils import DummyConsole


class DummyCommand(iOSXcodeMixin, BaseCommand):
    """A dummy command that includes the iOS Xcode mixin."""

    command = "dummy"

    def __init__(self, base_path, **kwargs):
        kwargs.setdefault("console", Console())
        super().__init__(base_path=base_path, **kwargs)
        self.tools.console = DummyConsole()


@pytest.fixture
def dummy_command(tmp_path):
    cmd = DummyCommand(base_path=tmp_path)

    # Mock the options object
    cmd.options = mock.MagicMock()
    cmd.options.device = None

    # Mock get_simulators
    mock_get_simulators = mock.MagicMock()
    cmd.get_simulators = mock_get_simulators

    return cmd


def test_explicit_device_udid(dummy_command):
    """If the user nominates a device UDID at the command line, it is used."""
    # get_simulators will return some options.
    dummy_command.get_simulators.return_value = {
        "iOS 13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        }
    }

    # The target device will be the one the user specified as an option.
    result = dummy_command.select_target_device("2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D")
    udid, iOS_version, device = result

    assert udid == "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D"
    assert iOS_version == "13.2"
    assert device == "iPhone 11"


def test_explicit_device_name_should_find_highest_version(dummy_command):
    """If an explicit device name is provided (case insensitive) it is used."""
    # get_simulators will return multiple options on 2 iOS versions.
    dummy_command.get_simulators.return_value = {
        "iOS 10.3": {
            "0BB80120-FA02-4597-A1BA-DB8CDE4F086D": "iPhone 5s",
            "D7BBAD14-38FD-48F5-ACFD-B1193F829216": "iPhone 6",
            "6998CA09-44B5-4963-8F80-265412D99683": "iPhone 7",
            "68A6E340-8376-47C5-9468-C9A005C88209": "iPhone 8",
        },
        "iOS 13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        },
    }

    # Try to load an iPhone 8.
    # It matches case insensitive, and finds the highest iOS version.
    udid, iOS_version, device = dummy_command.select_target_device("iphone 8")

    assert udid == "C9A005C8-9468-47C5-8376-68A6E3408209"
    assert iOS_version == "13.2"
    assert device == "iPhone 8"

    # User input was not solicited
    assert dummy_command.console.prompts == []


def test_explicit_device_name_should_find_highest_version_no_os(dummy_command):
    """If an explicit device name is provided (case insensitive) it is used."""
    # get_simulators will return multiple options on 2 iOS versions.
    # In older versions of Xcode, the OS name wasn't included in the version
    # string. Ensure that this still works, even though it doesn't happen in
    # practice (as of May 2022)
    dummy_command.get_simulators.return_value = {
        "10.3": {
            "0BB80120-FA02-4597-A1BA-DB8CDE4F086D": "iPhone 5s",
            "D7BBAD14-38FD-48F5-ACFD-B1193F829216": "iPhone 6",
            "6998CA09-44B5-4963-8F80-265412D99683": "iPhone 7",
            "68A6E340-8376-47C5-9468-C9A005C88209": "iPhone 8",
        },
        "13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        },
    }

    # Try to load an iPhone 8.
    # It matches case insensitive, and finds the highest iOS version.
    udid, iOS_version, device = dummy_command.select_target_device("iphone 8")

    assert udid == "C9A005C8-9468-47C5-8376-68A6E3408209"
    assert iOS_version == "13.2"
    assert device == "iPhone 8"

    # User input was not solicited
    assert dummy_command.console.prompts == []


def test_explicit_device_name_and_version(dummy_command):
    """If there are multiple options on multiple devices, two user inputs are needed."""
    # get_simulators will return multiple options on 2 iOS versions.
    dummy_command.get_simulators.return_value = {
        "iOS 10.3": {
            "0BB80120-FA02-4597-A1BA-DB8CDE4F086D": "iPhone 5s",
            "D7BBAD14-38FD-48F5-ACFD-B1193F829216": "iPhone 6",
            "6998CA09-44B5-4963-8F80-265412D99683": "iPhone 7",
            "68A6E340-8376-47C5-9468-C9A005C88209": "iPhone 8",
        },
        "iOS 13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        },
    }

    # Specify an iPhone 8 on 10.3
    # It matches case insensitive, and finds the explicit iOS version.
    udid, iOS_version, device = dummy_command.select_target_device("iphone 8::ios 10.3")

    assert udid == "68A6E340-8376-47C5-9468-C9A005C88209"
    assert iOS_version == "10.3"
    assert device == "iPhone 8"

    # User input was not solicited
    assert dummy_command.console.prompts == []


def test_invalid_explicit_device_udid(dummy_command):
    """If the user nominates an invalid device UDID, an error is raised."""
    # get_simulators will some options.
    dummy_command.get_simulators.return_value = {
        "iOS 13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        }
    }

    # The user nominates a specific device that doesn't exist
    with pytest.raises(InvalidDeviceError):
        dummy_command.select_target_device("deadbeef-dead-beef-cafe-deadbeefdead")

    # User input was not solicited
    assert dummy_command.console.prompts == []


def test_invalid_explicit_device_name(dummy_command):
    """If the user nominates an invalid device name, an error is raised."""
    # get_simulators will some options.
    dummy_command.get_simulators.return_value = {
        "iOS 13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        }
    }

    # The user nominates a specific device name that doesn't exist
    with pytest.raises(InvalidDeviceError):
        dummy_command.select_target_device("iphone 99")

    # User input was not solicited
    assert dummy_command.console.prompts == []


def test_explicit_name_invalid_version(dummy_command):
    """If the user nominates a device name but an invalid version, an error is
    raised."""
    # get_simulators will some options.
    dummy_command.get_simulators.return_value = {
        "iOS 13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        }
    }

    # The user nominates a valid device name, but on a version that
    # doesn't exist.
    with pytest.raises(InvalidDeviceError):
        dummy_command.select_target_device("iphone 11::37.42")


def test_invalid_explicit_device_name_valid_version(dummy_command):
    """If the user nominates an invalid device name but a valid version, an error is
    raised."""
    # get_simulators will some options.
    dummy_command.get_simulators.return_value = {
        "iOS 13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        }
    }

    # The user nominates a valid device name, but on a version that
    # doesn't exist.
    with pytest.raises(InvalidDeviceError):
        dummy_command.select_target_device("iphone 99::iOS 13.2")


def test_implied_device(dummy_command):
    """If there's only one device, no input is required; the device is returned."""
    # get_simulators will return one option.
    dummy_command.get_simulators.return_value = {
        "iOS 13.2": {
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
        }
    }

    udid, iOS_version, device = dummy_command.select_target_device()

    assert udid == "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D"
    assert iOS_version == "13.2"
    assert device == "iPhone 11"

    # No user input was solicited
    assert dummy_command.console.prompts == []


def test_implied_os(dummy_command):
    """If there is only one OS option, it's implied."""
    # get_simulators will return multiple options on 1 iOS version.
    dummy_command.get_simulators.return_value = {
        "iOS 13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        }
    }

    # Return option 2 (iPhone 11)
    dummy_command.console.values = ["2"]

    udid, iOS_version, device = dummy_command.select_target_device()

    assert udid == "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D"
    assert iOS_version == "13.2"
    assert device == "iPhone 11"

    # User input was solicited once
    assert dummy_command.console.prompts == ["Simulator: "]


def test_multiple_os_implied_device(dummy_command):
    """If there are multiple OS options, but only one device on the chosen OS, device is
    implied."""
    # get_simulators will return multiple options on 1 iOS version.
    dummy_command.get_simulators.return_value = {
        "iOS 10.3": {
            "0BB80120-FA02-4597-A1BA-DB8CDE4F086D": "iPhone 5s",
            "D7BBAD14-38FD-48F5-ACFD-B1193F829216": "iPhone 6",
            "6998CA09-44B5-4963-8F80-265412D99683": "iPhone 7",
        },
        "iOS 13.2": {
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
        },
    }

    # Return option 1 (13.2)
    dummy_command.console.values = ["2"]

    # Device for iOS 13.2 is implied.
    udid, iOS_version, device = dummy_command.select_target_device()

    assert udid == "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D"
    assert iOS_version == "13.2"
    assert device == "iPhone 11"

    # User input was solicited once
    assert dummy_command.console.prompts == ["iOS Version: "]


def test_os_and_device_options(dummy_command):
    """If there are multiple options on multiple devices, two user inputs are needed."""
    # get_simulators will return multiple options on 2 iOS versions.
    dummy_command.get_simulators.return_value = {
        "iOS 10.3": {
            "0BB80120-FA02-4597-A1BA-DB8CDE4F086D": "iPhone 5s",
            "D7BBAD14-38FD-48F5-ACFD-B1193F829216": "iPhone 6",
            "6998CA09-44B5-4963-8F80-265412D99683": "iPhone 7",
        },
        "iOS 13.2": {
            "C9A005C8-9468-47C5-8376-68A6E3408209": "iPhone 8",
            "2D3503A3-6EB9-4B37-9B17-C7EFEF2FA32D": "iPhone 11",
            "EEEBA06C-81F9-407C-885A-2261306DB2BE": "iPhone 11 Pro Max",
        },
    }

    # Return option 2 (13.2), then option 1 (iPhone 8)
    dummy_command.console.values = ["2", "1"]

    udid, iOS_version, device = dummy_command.select_target_device()

    assert udid == "C9A005C8-9468-47C5-8376-68A6E3408209"
    assert iOS_version == "13.2"
    assert device == "iPhone 8"

    # User input was solicited twice
    assert dummy_command.console.prompts == ["iOS Version: ", "Simulator: "]


def test_no_os_versions(dummy_command):
    """If there are no supported OS versions, raise an error."""
    # get_simulators returns no valid iOS versions.
    dummy_command.get_simulators.return_value = {}

    with pytest.raises(BriefcaseCommandError):
        dummy_command.select_target_device()


def test_no_devices_for_os(dummy_command):
    """If there are no devices for an OS version, raise an error."""
    # get_simulators returns no devices for iOS 13.2
    dummy_command.get_simulators.return_value = {"iOS 13.2": {}}

    with pytest.raises(BriefcaseCommandError):
        dummy_command.select_target_device()