File: conftest.py

package info (click to toggle)
python-briefcase 0.3.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,300 kB
  • sloc: python: 59,405; makefile: 57
file content (349 lines) | stat: -rw-r--r-- 10,925 bytes parent folder | download | duplicates (2)
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
from __future__ import annotations

from unittest import mock

import pytest
import tomli_w
from cookiecutter.main import cookiecutter

from briefcase.commands import CreateCommand
from briefcase.config import AppConfig
from briefcase.integrations.base import Tool
from briefcase.integrations.subprocess import Subprocess

from ...utils import DummyConsole, create_file


@pytest.fixture
def monkeypatch_tool_host_os(monkeypatch):
    """Add testing host OS as supported for tools that support all platforms."""
    monkeypatch.setattr(
        Tool,
        "supported_host_os",
        Tool.supported_host_os.union({"c64"}),
    )


class DefaultCreateCommand(CreateCommand):
    # An instance of CreateCommand that inherits the default
    # behavior of create handling.

    # method is required by the interface, but are not needed for these tests.
    def binary_path(self, app):
        return NotImplementedError()


@pytest.fixture
def default_create_command(tmp_path):
    return DefaultCreateCommand(base_path=tmp_path, console=DummyConsole())


class DummyCreateCommand(CreateCommand):
    """A dummy create command that stubs out all the required interfaces of the Create
    command."""

    supported_host_os = {"c64"}
    # Platform and format contain upper case to test case normalization
    platform = "Tester"
    output_format = "Dummy"
    description = "Dummy create command"
    hidden_app_properties = {"permission", "request"}

    def __init__(self, *args, support_file=None, git=None, home_path=None, **kwargs):
        kwargs.setdefault("console", DummyConsole())
        super().__init__(*args, **kwargs)

        # Override the host properties
        self.tools.host_arch = "gothic"
        self.tools.host_os = "c64"

        self.tools.home_path = home_path

        # If a test sets this property, the tool verification step will fail.
        self._missing_tool = None

        # Mock the external services
        self.tools.git = git
        self.tools.subprocess = mock.MagicMock(spec_set=Subprocess)
        self.support_file = support_file
        self.tools.cookiecutter = mock.MagicMock(spec_set=cookiecutter)

    @property
    def support_package_url_query(self):
        """The query arguments to use in a support package query request."""
        return [
            ("platform", self.platform),
            ("version", self.python_version_tag),
            ("arch", self.tools.host_arch),
        ]

    def binary_path(self, app):
        return self.bundle_path(app) / f"{app.app_name}.bin"

    # Hard code the python version to make testing easier.
    @property
    def python_version_tag(self):
        return "3.X"

    # Define output format-specific template context.
    def output_format_template_context(self, app):
        return {"output_format": "dummy"}

    # Handle platform-specific permissions.
    # Convert all the cross-platform permissions to upper case, prefixing DUMMY_.
    # Add a "good lighting" request if the camera permission has been requested.
    def permissions_context(self, app: AppConfig, x_permissions: dict[str, str]):
        # We don't actually need anything from the superclass; but call it to ensure
        # coverage.
        context = super().permissions_context(app, x_permissions)
        if context:
            # Make sure the base class *isn't* doing anything.
            return context

        permissions = {
            f"DUMMY_{key.upper()}": value.upper()
            for key, value in x_permissions.items()
            if value
        }
        context["permissions"] = permissions
        context["custom_permissions"] = app.permission

        requests = {"good.lighting": True} if x_permissions["camera"] else {}
        requests.update(getattr(app, "request", {}))
        context["requests"] = requests

        return context


class TrackingCreateCommand(DummyCreateCommand):
    """A dummy creation command that doesn't actually do anything.

    It only serves to track which actions would be performed.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.actions = []

    def briefcase_toml(self, app):
        # default any app to an empty `briefcase.toml`
        return self._briefcase_toml.get(app, {})

    def verify_host(self):
        super().verify_host()
        self.actions.append(("verify-host",))

    def verify_tools(self):
        super().verify_tools()
        self.actions.append(("verify-tools",))

    def finalize_app_config(self, app):
        super().finalize_app_config(app=app)
        self.actions.append(("finalize-app-config", app.app_name))

    def verify_app_template(self, app):
        super().verify_app_template(app=app)
        self.actions.append(("verify-app-template", app.app_name))

    def verify_app_tools(self, app):
        super().verify_app_tools(app=app)
        self.actions.append(("verify-app-tools", app.app_name))

    # Override all the body methods of a CreateCommand
    # with versions that we can use to track actions performed.
    def generate_app_template(self, app):
        self.actions.append(("generate", app.app_name))

        # A mock version of template generation.
        create_file(self.bundle_path(app) / "new", "new template!")

    def install_app_support_package(self, app):
        self.actions.append(("support", app.app_name))

    def install_app_requirements(self, app, test_mode):
        self.actions.append(("requirements", app.app_name, test_mode))

    def install_app_code(self, app, test_mode):
        self.actions.append(("code", app.app_name, test_mode))

    def install_app_resources(self, app):
        self.actions.append(("resources", app.app_name))

    def install_stub_binary(self, app):
        self.actions.append(("stub", app.app_name))
        # A mock version of a stub binary
        create_file(self.bundle_path(app) / "Stub.bin", "stub binary")

    def cleanup_app_content(self, app):
        self.actions.append(("cleanup", app.app_name))


@pytest.fixture
def create_command(tmp_path, mock_git, monkeypatch_tool_host_os):
    return DummyCreateCommand(
        base_path=tmp_path / "base_path",
        data_path=tmp_path / "data",
        git=mock_git,
        home_path=tmp_path / "home",
    )


@pytest.fixture
def tracking_create_command(tmp_path, mock_git, monkeypatch_tool_host_os):
    return TrackingCreateCommand(
        git=mock_git,
        base_path=tmp_path / "base_path",
        apps={
            "first": AppConfig(
                app_name="first",
                bundle="com.example",
                version="0.0.1",
                description="The first simple app",
                sources=["src/first"],
                license={"file": "LICENSE"},
            ),
            "second": AppConfig(
                app_name="second",
                bundle="com.example",
                version="0.0.2",
                description="The second simple app",
                sources=["src/second"],
                license={"file": "LICENSE"},
            ),
        },
    )


@pytest.fixture
def myapp():
    return AppConfig(
        app_name="my-app",
        formal_name="My App",
        bundle="com.example",
        version="1.2.3",
        description="This is a simple app",
        sources=["src/my_app"],
        url="https://example.com",
        author="First Last",
        author_email="first@example.com",
        license={"file": "LICENSE"},
    )


@pytest.fixture
def bundle_path(myapp, tmp_path):
    # Return the bundle path for the app; however, as a side effect,
    # ensure that the app, and app_packages target directories
    # exist, and the briefcase index file has been created.
    bundle_path = tmp_path / "base_path/build" / myapp.app_name / "tester/dummy"
    (bundle_path / "path/to/app").mkdir(parents=True, exist_ok=True)

    return bundle_path


@pytest.fixture
def app_packages_path_index(bundle_path):
    (bundle_path / "path/to/app_packages").mkdir(parents=True, exist_ok=True)
    with (bundle_path / "briefcase.toml").open("wb") as f:
        index = {
            "paths": {
                "app_path": "path/to/app",
                "app_packages_path": "path/to/app_packages",
                "support_path": "path/to/support",
                "support_revision": 37,
            }
        }
        tomli_w.dump(index, f)


@pytest.fixture
def app_requirements_path_index(bundle_path):
    with (bundle_path / "briefcase.toml").open("wb") as f:
        index = {
            "paths": {
                "app_path": "path/to/app",
                "app_requirements_path": "path/to/requirements.txt",
                "support_path": "path/to/support",
                "support_revision": 37,
            }
        }
        tomli_w.dump(index, f)


@pytest.fixture
def app_requirement_installer_args_path_index(bundle_path):
    with (bundle_path / "briefcase.toml").open("wb") as f:
        index = {
            "paths": {
                "app_path": "path/to/app",
                "app_requirements_path": "path/to/requirements.txt",
                "app_requirement_installer_args_path": "path/to/installer-args.txt",
                "support_path": "path/to/support",
                "support_revision": 37,
            }
        }
        tomli_w.dump(index, f)


@pytest.fixture
def no_support_revision_index(bundle_path):
    with (bundle_path / "briefcase.toml").open("wb") as f:
        index = {
            "paths": {
                "app_path": "path/to/app",
                "app_requirements_path": "path/to/requirements.txt",
                "support_path": "path/to/support",
            }
        }
        tomli_w.dump(index, f)


@pytest.fixture
def no_support_path_index(bundle_path):
    with (bundle_path / "briefcase.toml").open("wb") as f:
        index = {
            "paths": {
                "app_path": "path/to/app",
                "app_requirements_path": "path/to/requirements.txt",
            }
        }
        tomli_w.dump(index, f)


@pytest.fixture
def support_path(bundle_path):
    return bundle_path / "path/to/support"


@pytest.fixture
def app_requirements_path(bundle_path):
    return bundle_path / "path/to/requirements.txt"


@pytest.fixture
def app_requirement_installer_args_path(bundle_path):
    return bundle_path / "path/to/installer-args.txt"


@pytest.fixture
def app_packages_path(bundle_path):
    return bundle_path / "path/to/app_packages"


@pytest.fixture
def app_path(bundle_path):
    return bundle_path / "path/to/app"


@pytest.fixture
def stub_binary_revision_path_index(bundle_path):
    with (bundle_path / "briefcase.toml").open("wb") as f:
        index = {
            "paths": {
                "app_path": "path/to/app",
                "app_requirements_path": "path/to/requirements.txt",
                "stub_binary_revision": 37,
            }
        }
        tomli_w.dump(index, f)