File: test_AppPackagesMergeMixin__merge_app_packages.py

package info (click to toggle)
python-briefcase 0.3.25-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,596 kB
  • sloc: python: 62,519; makefile: 60
file content (301 lines) | stat: -rw-r--r-- 10,984 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
import os
import subprocess
from pathlib import Path

import pytest

from briefcase.exceptions import BriefcaseCommandError

from ...utils import create_file, create_installed_package, file_content


@pytest.mark.parametrize("pre_existing", [True, False])
def test_merge(dummy_command, pre_existing, tmp_path):
    "Multiple source folders can be merged"
    if pre_existing:
        # Create some pre-existing package content. This should all be deleted as a
        # result of the merge process.
        create_installed_package(tmp_path / "merged_app_packages", "legacy")

    # Create 2 packages in the "gothic" architecture app package sources
    create_installed_package(tmp_path / "app_packages.gothic", "first", "1.2.3")
    create_installed_package(
        tmp_path / "app_packages.gothic",
        "second",
        "2.3.4",
        tag="macOS_11_0_gothic",
        extra_content=[
            ("second/other.py", "# other python"),
            ("second/different.py", "# different python"),
            ("second/some-binary", "# A file with executable permissions", 0o755),
            ("second/sub1/module1.dylib", b"\xca\xfe\xba\xbedylib-gothic"),
            ("second/sub1/module2.so", b"\xca\xfe\xba\xbedylib-gothic"),
            ("second/sub1/module3.dylib", b"\xca\xfe\xba\xbedylib-gothic"),
            ("second/sub1/module5.so", "\x7fELFso-elf"),
        ],
    )

    # Create 2 packages in the "modern" architecture app package sources
    # The first package is pure, so it won't exist in the second app_packages.
    # The "second" package:
    # - is missing the "other" python file and "module3" dylib
    # - has a "module4" dylib and an "extra" python file in a unique folder.
    create_installed_package(
        tmp_path / "app_packages.modern",
        "second",
        "2.3.4",
        tag="macOS_11_0_modern",
        extra_content=[
            ("second/different.py", "# I need to be different"),
            ("second/sub1/module1.dylib", b"\xca\xfe\xba\xbedylib-modern"),
            ("second/sub1/module2.so", b"\xca\xfe\xba\xbedylib-modern"),
            ("second/sub1/module4.dylib", b"\xca\xfe\xba\xbedylib-modern"),
            ("second/sub1/module5.so", "\x7fELFso-elf"),
            ("second/sub2/extra.py", "# extra python"),
        ],
    )

    # Mock subprocess so that lipo generates output files.
    def lipo(cmd, **kwargs):
        if cmd[0] != "lipo":
            pytest.fail(f"Subprocess called {cmd[0]}, not lipo")

        create_file(cmd[3], b"\xca\xfe\xba\xbedylib-merged", mode="wb")

    dummy_command.tools.subprocess.run.side_effect = lipo

    # Merge the two sources into a final location.
    merged_path = tmp_path / "merged_app_packages"
    dummy_command.merge_app_packages(
        merged_path,
        sources=[
            tmp_path / "app_packages.gothic",
            tmp_path / "app_packages.modern",
        ],
    )

    # The final merged app packages contains only the merged content.

    assert {
        (path.relative_to(merged_path), file_content(path))
        for path in merged_path.glob("**/*")
    } == {
        (Path("first"), None),
        (Path("first/__init__.py"), ""),
        (Path("first/app.py"), "# This is the app"),
        (Path("first-1.2.3.dist-info"), None),
        (Path("first-1.2.3.dist-info/INSTALLER"), "pip\n"),
        (
            Path("first-1.2.3.dist-info/METADATA"),
            "\n".join(
                [
                    "Metadata-Version: 2.1",
                    "Name: first",
                    "Version: 1.2.3",
                    "Summary: A packaged named first.",
                    "Author-email: Jane Developer <jane@example.com>",
                    "\n",
                ]
            ),
        ),
        (Path("first-1.2.3.dist-info/RECORD"), ""),
        (
            Path("first-1.2.3.dist-info/WHEEL"),
            "\n".join(
                [
                    "Wheel-Version: 1.0",
                    "Generator: test-case",
                    "Root-Is-Purelib: true",
                    "Tag: py3-none-any",
                    "\n",
                ]
            ),
        ),
        (Path("second"), None),
        (Path("second/__init__.py"), ""),
        (Path("second/app.py"), "# This is the app"),
        (Path("second/different.py"), "# different python"),
        (Path("second/some-binary"), "# A file with executable permissions"),
        (Path("second/other.py"), "# other python"),
        (Path("second/sub1"), None),
        (Path("second/sub1/module1.dylib"), b"\xca\xfe\xba\xbedylib-merged"),
        (Path("second/sub1/module2.so"), b"\xca\xfe\xba\xbedylib-merged"),
        (Path("second/sub1/module3.dylib"), b"\xca\xfe\xba\xbedylib-merged"),
        (Path("second/sub1/module4.dylib"), b"\xca\xfe\xba\xbedylib-merged"),
        (Path("second/sub1/module5.so"), "\x7fELFso-elf"),
        (Path("second/sub2"), None),
        (Path("second/sub2/extra.py"), "# extra python"),
        (Path("second-2.3.4.dist-info"), None),
        (Path("second-2.3.4.dist-info/INSTALLER"), "pip\n"),
        (
            Path("second-2.3.4.dist-info/METADATA"),
            "\n".join(
                [
                    "Metadata-Version: 2.1",
                    "Name: second",
                    "Version: 2.3.4",
                    "Summary: A packaged named second.",
                    "Author-email: Jane Developer <jane@example.com>",
                    "\n",
                ]
            ),
        ),
        (Path("second-2.3.4.dist-info/RECORD"), ""),
        (
            Path("second-2.3.4.dist-info/WHEEL"),
            "\n".join(
                [
                    "Wheel-Version: 1.0",
                    "Generator: test-case",
                    "Root-Is-Purelib: false",
                    # The first source wins
                    "Tag: macOS_11_0_gothic",
                    "\n",
                ]
            ),
        ),
    }

    # Check that the embedded binary has executable permissions
    assert os.access(merged_path / "second/some-binary", os.X_OK)


def test_merge_problem(dummy_command, tmp_path):
    "If a binary cannot be merged, an exception is raised."

    # Create 2 packages in the "gothic" architecture app package sources
    create_installed_package(tmp_path / "app_packages.gothic", "first", "1.2.3")
    create_installed_package(
        tmp_path / "app_packages.gothic",
        "second",
        "2.3.4",
        tag="macOS_11_0_gothic",
        extra_content=[
            ("second/sub1/module1.dylib", b"\xca\xfe\xba\xbedylib-gothic"),
        ],
    )
    # Create 2 packages in the "modern" architecture app package sources
    # The first package is pure, so it won't exist in the second app_packages.
    # The "second" package:
    # - is missing the "other" python file and "module3" dylib
    # - has a "module4" dylib and an "extra" python file in a unique folder.
    create_installed_package(
        tmp_path / "app_packages.modern",
        "second",
        "2.3.4",
        tag="macOS_11_0_modern",
        extra_content=[
            ("second/sub1/module1.dylib", b"\xca\xfe\xba\xbedylib-modern"),
        ],
    )

    # Mock subprocess so that lipo generates an exception
    dummy_command.tools.subprocess.run.side_effect = subprocess.CalledProcessError(
        returncode=1, cmd="lipo"
    )

    # Merge the two sources into a final location. This will raise an exception.
    with pytest.raises(
        BriefcaseCommandError,
        match=r"Unable to create fat library for second[/\\]sub1[/\\]module1.dylib",
    ):
        merged_path = tmp_path / "merged_app_packages"
        dummy_command.merge_app_packages(
            merged_path,
            sources=[
                tmp_path / "app_packages.gothic",
                tmp_path / "app_packages.modern",
            ],
        )


def test_merge_no_dylib(dummy_command, tmp_path, capsys):
    "If there are no dylibs, no merging is performed"

    # Create 2 pure python packages in the first app_packages folder.
    create_installed_package(tmp_path / "app_packages.gothic", "first", "1.2.3")
    create_installed_package(tmp_path / "app_packages.gothic", "second", "2.3.4")
    # Create an empty second app_packages folder.
    (tmp_path / "app_packages.modern").mkdir()

    # Merge the two sources into a final location.
    merged_path = tmp_path / "merged_app_packages"
    dummy_command.merge_app_packages(
        merged_path,
        sources=[
            tmp_path / "app_packages.gothic",
            tmp_path / "app_packages.modern",
        ],
    )

    # subprocess wasn't called.
    dummy_command.tools.subprocess.run.assert_not_called()

    # The final merged app packages contains only the merged content.
    assert {
        (path.relative_to(merged_path), file_content(path))
        for path in merged_path.glob("**/*")
    } == {
        (Path("first"), None),
        (Path("first/__init__.py"), ""),
        (Path("first/app.py"), "# This is the app"),
        (Path("first-1.2.3.dist-info"), None),
        (Path("first-1.2.3.dist-info/INSTALLER"), "pip\n"),
        (
            Path("first-1.2.3.dist-info/METADATA"),
            "\n".join(
                [
                    "Metadata-Version: 2.1",
                    "Name: first",
                    "Version: 1.2.3",
                    "Summary: A packaged named first.",
                    "Author-email: Jane Developer <jane@example.com>",
                    "\n",
                ]
            ),
        ),
        (Path("first-1.2.3.dist-info/RECORD"), ""),
        (
            Path("first-1.2.3.dist-info/WHEEL"),
            "\n".join(
                [
                    "Wheel-Version: 1.0",
                    "Generator: test-case",
                    "Root-Is-Purelib: true",
                    "Tag: py3-none-any",
                    "\n",
                ]
            ),
        ),
        (Path("second"), None),
        (Path("second/__init__.py"), ""),
        (Path("second/app.py"), "# This is the app"),
        (Path("second-2.3.4.dist-info"), None),
        (Path("second-2.3.4.dist-info/INSTALLER"), "pip\n"),
        (
            Path("second-2.3.4.dist-info/METADATA"),
            "\n".join(
                [
                    "Metadata-Version: 2.1",
                    "Name: second",
                    "Version: 2.3.4",
                    "Summary: A packaged named second.",
                    "Author-email: Jane Developer <jane@example.com>",
                    "\n",
                ]
            ),
        ),
        (Path("second-2.3.4.dist-info/RECORD"), ""),
        (
            Path("second-2.3.4.dist-info/WHEEL"),
            "\n".join(
                [
                    "Wheel-Version: 1.0",
                    "Generator: test-case",
                    "Root-Is-Purelib: true",
                    "Tag: py3-none-any",
                    "\n",
                ]
            ),
        ),
    }