File: test_install_app_code.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 (638 lines) | stat: -rw-r--r-- 18,493 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import os
import shutil
from unittest import mock

import pytest

import briefcase
from briefcase.exceptions import MissingAppSources

from ...utils import create_file


def assert_dist_info(app_path):
    dist_info_path = app_path / "my_app-1.2.3.dist-info"

    # Confirm the metadata files exist.
    assert (dist_info_path / "INSTALLER").exists()
    assert (dist_info_path / "METADATA").exists()

    with (dist_info_path / "INSTALLER").open(encoding="utf-8") as f:
        assert f.read() == "briefcase\n"

    with (dist_info_path / "METADATA").open(encoding="utf-8") as f:
        assert (
            f.read()
            == f"""Metadata-Version: 2.1
Briefcase-Version: {briefcase.__version__}
Name: my-app
Formal-Name: My App
App-ID: com.example.my-app
Version: 1.2.3
Home-page: https://example.com
Download-URL: https://example.com
Author: First Last
Author-email: first@example.com
Summary: This is a simple app
"""
        )


def test_no_code(
    create_command,
    myapp,
    app_path,
    app_requirements_path_index,
):
    """If an app has no code (?!), install_app_code is mostly a no-op; but distinfo is
    created."""
    # Mock shutil so we can track usage.
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)
    create_command.tools.os = mock.MagicMock(spec_set=os)

    myapp.sources = None

    create_command.install_app_code(myapp, test_mode=False)

    # No request was made to install requirements
    create_command.tools.shutil.rmtree.assert_called_once_with(app_path)
    create_command.tools.os.mkdir.assert_called_once_with(app_path)
    create_command.tools.shutil.copytree.assert_not_called()
    create_command.tools.shutil.copy.assert_not_called()

    # Metadata has been created
    assert_dist_info(app_path)


def test_empty_code(
    create_command,
    myapp,
    app_path,
    app_requirements_path_index,
):
    """If an app has an empty sources list (?!), install_app_code is mostly a no-op; but
    distinfo is created."""
    # Mock shutil so we can track usage.
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)
    create_command.tools.os = mock.MagicMock(spec_set=os)

    myapp.sources = []

    create_command.install_app_code(myapp, test_mode=False)

    # No request was made to install requirements
    create_command.tools.shutil.rmtree.assert_called_once_with(app_path)
    create_command.tools.os.mkdir.assert_called_once_with(app_path)
    create_command.tools.shutil.copytree.assert_not_called()
    create_command.tools.shutil.copy.assert_not_called()

    # Metadata has been created
    assert_dist_info(app_path)


def test_source_missing(
    create_command,
    myapp,
    app_path,
    app_requirements_path_index,
):
    """If an app defines sources that are missing, an error is raised."""
    # Set the app definition to point at sources that don't exist
    myapp.sources = ["missing"]

    with pytest.raises(MissingAppSources):
        create_command.install_app_code(myapp, test_mode=False)

    # Distinfo won't be created.
    dist_info_path = app_path / "myapp-1.2.3.dist-info"
    assert not dist_info_path.exists()


def test_source_dir(
    create_command,
    myapp,
    tmp_path,
    app_path,
    app_requirements_path_index,
):
    """If an app defines directories of sources, the whole directory is copied."""
    # Create the mock sources
    # src /
    #   first /
    #     demo.py
    #   second /
    #     shallow.py
    #     submodule /
    #       deeper.py
    create_file(
        tmp_path / "base_path/src/first/demo.py",
        "print('hello first')\n",
    )
    create_file(
        tmp_path / "base_path/src/second/shallow.py",
        "print('hello shallow second')\n",
    )
    create_file(
        tmp_path / "base_path/src/second/submodule/deeper.py",
        "print('hello deep second')\n",
    )

    # Set the app definition, and install sources
    myapp.sources = ["src/first", "src/second"]

    create_command.install_app_code(myapp, test_mode=False)

    # All the sources exist.
    assert (app_path / "first").exists()
    assert (app_path / "first/demo.py").exists()

    assert (app_path / "second").exists()
    assert (app_path / "second/shallow.py").exists()

    assert (app_path / "second/submodule").exists()
    assert (app_path / "second/submodule/deeper.py").exists()

    # Metadata has been created
    assert_dist_info(app_path)

    # Original app definitions haven't changed
    assert myapp.sources == ["src/first", "src/second"]
    assert myapp.test_sources is None


def test_source_file(
    create_command,
    myapp,
    tmp_path,
    app_path,
    app_requirements_path_index,
):
    """If an app defines single file sources, the files are copied."""
    # Create the mock sources
    # src /
    #   demo.py
    # other.py
    create_file(
        tmp_path / "base_path/src/demo.py",
        "print('hello first')\n",
    )
    create_file(
        tmp_path / "base_path/other.py",
        "print('hello second')\n",
    )

    # Set the app definition, and install sources
    myapp.sources = ["src/demo.py", "other.py"]

    create_command.install_app_code(myapp, test_mode=False)

    # All the sources exist.
    assert (app_path / "demo.py").exists()
    assert (app_path / "other.py").exists()

    # Metadata has been created
    assert_dist_info(app_path)

    # Original app definitions haven't changed
    assert myapp.sources == ["src/demo.py", "other.py"]
    assert myapp.test_sources is None


def test_no_existing_app_folder(
    create_command,
    myapp,
    tmp_path,
    app_path,
    app_requirements_path_index,
):
    """If there's no pre-existing app folder, one is created."""
    # Create the mock sources
    # src /
    #   first /
    #     demo.py
    #   second /
    #     shallow.py
    #     submodule /
    #       deeper.py
    create_file(
        tmp_path / "base_path/src/first/demo.py",
        "print('hello first')\n",
    )
    create_file(
        tmp_path / "base_path/src/second/shallow.py",
        "print('hello shallow second')\n",
    )
    create_file(
        tmp_path / "base_path/src/second/submodule/deeper.py",
        "print('hello deep second')\n",
    )

    # Remove the app folder created by the test fixture.
    shutil.rmtree(app_path)

    # Set the app definition, and install sources
    myapp.sources = ["src/first/demo.py", "src/second"]

    create_command.install_app_code(myapp, test_mode=False)

    # All the new sources exist, and contain the new content.
    assert (app_path / "demo.py").exists()
    with (app_path / "demo.py").open(encoding="utf-8") as f:
        assert f.read() == "print('hello first')\n"

    assert (app_path / "second").exists()
    assert (app_path / "second/shallow.py").exists()
    with (app_path / "second/shallow.py").open(encoding="utf-8") as f:
        assert f.read() == "print('hello shallow second')\n"

    assert (app_path / "second/submodule").exists()
    assert (app_path / "second/submodule/deeper.py").exists()
    with (app_path / "second/submodule/deeper.py").open(encoding="utf-8") as f:
        assert f.read() == "print('hello deep second')\n"

    # The stale/broken modules have been removed.
    assert not (app_path / "stale.py").exists()
    assert not (app_path / "second/stale.py").exists()
    assert not (app_path / "second/broken").exists()

    # Metadata has been updated.
    assert not (app_path / "my_app-1.2.2.dist-info").exists()
    assert_dist_info(app_path)

    # Original app definitions haven't changed
    assert myapp.sources == ["src/first/demo.py", "src/second"]
    assert myapp.test_sources is None


def test_replace_sources(
    create_command,
    myapp,
    tmp_path,
    app_path,
    app_requirements_path_index,
):
    """Stale sources and dist-info are removed on installation."""
    # Create the mock sources
    # src /
    #   first /
    #     demo.py
    #   second /
    #     shallow.py
    #     submodule /
    #       deeper.py
    create_file(
        tmp_path / "base_path/src/first/demo.py",
        "print('hello first')\n",
    )
    create_file(
        tmp_path / "base_path/src/second/shallow.py",
        "print('hello shallow second')\n",
    )
    create_file(
        tmp_path / "base_path/src/second/submodule/deeper.py",
        "print('hello deep second')\n",
    )

    # Also create some existing sources:
    # path / to / app /
    #   demo.py
    #   stale.py
    #   second /
    #     shallow.py
    #     stale.py
    #     submodule /
    #       deeper.py
    #     broken /
    #       other.py
    #   my_app-1.2.2.dist-info /
    create_file(
        app_path / "src/demo.py",
        "print('old hello first')\n",
    )
    create_file(
        app_path / "src/stale.py",
        "print('stale hello first')\n",
    )
    create_file(
        app_path / "src/second/shallow.py",
        "print('old hello shallow second')\n",
    )
    create_file(
        app_path / "src/second/stale.py",
        "print('hello second stale')\n",
    )
    create_file(
        app_path / "src/second/submodule/deeper.py",
        "print('hello deep second')\n",
    )
    create_file(
        app_path / "src/second/broken/other.py",
        "print('hello second deep broken')\n",
    )

    old_dist_info_dir = app_path / "my_app-1.2.2.dist-info"
    old_dist_info_dir.mkdir()

    # Set the app definition, and install sources
    myapp.sources = ["src/first/demo.py", "src/second"]

    create_command.install_app_code(myapp, test_mode=False)

    # All the new sources exist, and contain the new content.
    assert (app_path / "demo.py").exists()
    with (app_path / "demo.py").open(encoding="utf-8") as f:
        assert f.read() == "print('hello first')\n"

    assert (app_path / "second").exists()
    assert (app_path / "second/shallow.py").exists()
    with (app_path / "second/shallow.py").open(encoding="utf-8") as f:
        assert f.read() == "print('hello shallow second')\n"

    assert (app_path / "second/submodule").exists()
    assert (app_path / "second/submodule/deeper.py").exists()
    with (app_path / "second/submodule/deeper.py").open(encoding="utf-8") as f:
        assert f.read() == "print('hello deep second')\n"

    # The stale/broken modules have been removed.
    assert not (app_path / "stale.py").exists()
    assert not (app_path / "second/stale.py").exists()
    assert not (app_path / "second/broken").exists()

    # Metadata has been updated.
    assert not (app_path / "my_app-1.2.2.dist-info").exists()
    assert_dist_info(app_path)

    # Original app definitions haven't changed
    assert myapp.sources == ["src/first/demo.py", "src/second"]
    assert myapp.test_sources is None


def test_non_latin_metadata(
    create_command,
    myapp,
    app_path,
    app_requirements_path_index,
):
    """If the app metadata contains non-Latin-1 characters, the METADATA file is written
    correctly (Briefcase#767)"""
    myapp.formal_name = "My büggy app"
    myapp.author = "José Weiß-Müller"
    myapp.author_email = "钱华林@中科院.中国"
    myapp.url = "https://xn--7qvx15a.cn"
    myapp.description = "A Møøse once bit my sister..."

    # Mock shutil so we can track usage.
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)
    create_command.tools.os = mock.MagicMock(spec_set=os)

    myapp.sources = []

    create_command.install_app_code(myapp, test_mode=False)

    # No request was made to install requirements
    create_command.tools.shutil.rmtree.assert_called_once_with(app_path)
    create_command.tools.os.mkdir.assert_called_once_with(app_path)
    create_command.tools.shutil.copytree.assert_not_called()
    create_command.tools.shutil.copy.assert_not_called()

    # The dist-info file was created, and is readable.
    dist_info_path = app_path / "my_app-1.2.3.dist-info"

    # Confirm the metadata files exist.
    assert (dist_info_path / "INSTALLER").exists()
    assert (dist_info_path / "METADATA").exists()

    with (dist_info_path / "INSTALLER").open(encoding="utf-8") as f:
        assert f.read() == "briefcase\n"

    with (dist_info_path / "METADATA").open(encoding="utf-8") as f:
        assert (
            f.read()
            == f"""Metadata-Version: 2.1
Briefcase-Version: {briefcase.__version__}
Name: my-app
Formal-Name: My büggy app
App-ID: com.example.my-app
Version: 1.2.3
Home-page: https://xn--7qvx15a.cn
Download-URL: https://xn--7qvx15a.cn
Author: José Weiß-Müller
Author-email: 钱华林@中科院.中国
Summary: A Møøse once bit my sister...
"""
        )


def test_test_sources(
    create_command,
    myapp,
    tmp_path,
    app_path,
    app_requirements_path_index,
):
    """If an app defines test code, but we're not in test mode, it isn't copied."""
    # Create the mock sources
    # src /
    #   first /
    #     demo.py
    #   second /
    #     shallow.py
    # tests /
    #   first.py
    #   deep /
    #     test_case.py
    # othertests/
    #   tests_more.py
    #   special /
    #     test_weird.py
    create_file(
        tmp_path / "base_path/src/first/demo.py",
        "print('hello first')\n",
    )
    create_file(
        tmp_path / "base_path/src/second/shallow.py",
        "print('hello shallow second')\n",
    )
    create_file(
        tmp_path / "base_path/tests/first.py",
        "print('hello first test suite')\n",
    )
    create_file(
        tmp_path / "base_path/tests/deep/test_case.py",
        "print('hello test case')\n",
    )
    create_file(
        tmp_path / "base_path/othertests/test_more.py",
        "print('hello more tests')\n",
    )
    create_file(
        tmp_path / "base_path/othertests/special/test_weird.py",
        "print('hello weird tests')\n",
    )

    # Set the app definition, and install sources
    myapp.sources = ["src/first", "src/second"]
    myapp.test_sources = ["tests", "othertests"]

    create_command.install_app_code(myapp, test_mode=False)

    # App sources exist.
    assert (app_path / "first").exists()
    assert (app_path / "first/demo.py").exists()

    assert (app_path / "second").exists()
    assert (app_path / "second/shallow.py").exists()

    # Test sources do not exist
    assert not (app_path / "tests").exists()
    assert not (app_path / "othertests").exists()

    # Metadata has been created
    assert_dist_info(app_path)

    # Original app definitions haven't changed
    assert myapp.sources == ["src/first", "src/second"]
    assert myapp.test_sources == ["tests", "othertests"]


def test_test_sources_test_mode(
    create_command,
    myapp,
    tmp_path,
    app_path,
    app_requirements_path_index,
):
    """If an app defines test code, and we're in test mode, test sources are copied."""
    # Create the mock sources
    # src /
    #   first /
    #     demo.py
    #   second /
    #     shallow.py
    # tests /
    #   first.py
    #   deep /
    #     test_case.py
    # othertests/
    #   tests_more.py
    #   special /
    #     test_weird.py
    create_file(
        tmp_path / "base_path/src/first/demo.py",
        "print('hello first')\n",
    )
    create_file(
        tmp_path / "base_path/src/second/shallow.py",
        "print('hello shallow second')\n",
    )
    create_file(
        tmp_path / "base_path/tests/first.py",
        "print('hello first test suite')\n",
    )
    create_file(
        tmp_path / "base_path/tests/deep/test_case.py",
        "print('hello test case')\n",
    )
    create_file(
        tmp_path / "base_path/othertests/test_more.py",
        "print('hello more tests')\n",
    )
    create_file(
        tmp_path / "base_path/othertests/special/test_weird.py",
        "print('hello weird tests')\n",
    )

    # Set the app definition, and install sources
    myapp.sources = ["src/first", "src/second"]
    myapp.test_sources = ["tests", "othertests"]

    create_command.install_app_code(myapp, test_mode=True)

    # App sources exist.
    assert (app_path / "first").exists()
    assert (app_path / "first/demo.py").exists()

    assert (app_path / "second").exists()
    assert (app_path / "second/shallow.py").exists()

    # Test sources exist
    assert (app_path / "tests/first.py").exists()
    assert (app_path / "tests/deep/test_case.py").exists()

    assert (app_path / "othertests/test_more.py").exists()
    assert (app_path / "othertests/special/test_weird.py").exists()

    # Metadata has been created
    assert_dist_info(app_path)

    # Original app definitions haven't changed
    assert myapp.sources == ["src/first", "src/second"]
    assert myapp.test_sources == ["tests", "othertests"]


def test_only_test_sources_test_mode(
    create_command,
    myapp,
    tmp_path,
    app_path,
    app_requirements_path_index,
):
    """If an app only defines test code, and we're in test mode, test sources are
    copied."""
    # Create the mock sources
    # tests /
    #   first.py
    #   deep /
    #     test_case.py
    # othertests/
    #   tests_more.py
    #   special /
    #     test_weird.py
    create_file(
        tmp_path / "base_path/src/first/demo.py",
        "print('hello first')\n",
    )
    create_file(
        tmp_path / "base_path/src/second/shallow.py",
        "print('hello shallow second')\n",
    )
    create_file(
        tmp_path / "base_path/tests/first.py",
        "print('hello first test suite')\n",
    )
    create_file(
        tmp_path / "base_path/tests/deep/test_case.py",
        "print('hello test case')\n",
    )
    create_file(
        tmp_path / "base_path/othertests/test_more.py",
        "print('hello more tests')\n",
    )
    create_file(
        tmp_path / "base_path/othertests/special/test_weird.py",
        "print('hello weird tests')\n",
    )

    # Set the app definition, and install sources
    myapp.sources = None
    myapp.test_sources = ["tests", "othertests"]

    create_command.install_app_code(myapp, test_mode=True)

    # App sources do not exist.
    assert not (app_path / "first").exists()
    assert not (app_path / "second").exists()

    # Test sources exist
    assert (app_path / "tests/first.py").exists()
    assert (app_path / "tests/deep/test_case.py").exists()

    assert (app_path / "othertests/test_more.py").exists()
    assert (app_path / "othertests/special/test_weird.py").exists()

    # Metadata has been created
    assert_dist_info(app_path)

    # Original app definitions haven't changed
    assert myapp.sources is None
    assert myapp.test_sources == ["tests", "othertests"]