File: test_cleanup_app_content.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 (350 lines) | stat: -rw-r--r-- 14,028 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
import pytest

from briefcase.console import LogLevel

from ...utils import create_file


@pytest.fixture
def myapp_unrolled(myapp, support_path, app_packages_path_index):
    # Create some files that can be cleaned up
    create_file(support_path / "dir1/a_file1.txt", "pork")
    create_file(support_path / "dir1/a_file2.doc", "ham")
    create_file(support_path / "dir1/b_file.txt", "eggs")
    create_file(support_path / "dir1/__pycache__/first.pyc", "pyc 1")
    create_file(support_path / "dir1/__pycache__/second.pyc", "pyc 2")
    create_file(support_path / "dir2/b_file.txt", "spam")
    create_file(support_path / "other/deep/b_file.doc", "wigs")
    create_file(support_path / "other/deep/other.doc", "wigs")

    # Create a symlink to a file
    (support_path / "dir1/symlink.txt").symlink_to(support_path / "dir2/b_file.txt")

    # Create a symlink to a directory
    (support_path / "mirror").symlink_to(support_path / "dir1")

    return myapp


@pytest.mark.parametrize("debug", [True, False])
def test_no_cleanup(create_command, myapp_unrolled, support_path, debug, capsys):
    """If there are no cleanup directives, bundle content isn't touched; but __pycache__
    is cleaned."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm the files are still there, except for the pycache
    assert (support_path / "dir1/a_file1.txt").exists()
    assert (support_path / "dir1/a_file2.doc").exists()
    assert (support_path / "dir1/b_file.txt").exists()
    assert not (support_path / "dir1/__pycache__").exists()
    assert (support_path / "dir2/b_file.txt").exists()
    assert (support_path / "other/deep/b_file.doc").exists()

    # Symlinks still exist
    assert (support_path / "dir1/symlink.txt").is_symlink()
    assert (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (5 if debug else 4)


@pytest.mark.parametrize("debug", [True, False])
def test_dir_cleanup(create_command, myapp_unrolled, support_path, debug, capsys):
    """A directory can be cleaned up."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    myapp_unrolled.cleanup_paths = ["path/to/support/dir1"]

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm that dir1 has been removed
    assert not (support_path / "dir1").exists()
    assert (support_path / "dir2/b_file.txt").exists()
    assert (support_path / "other/deep/b_file.doc").exists()

    # Directory symlinks still exists, but the file symlink doesn't
    assert not (support_path / "dir1/symlink.txt").exists()
    assert (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (5 if debug else 4)


@pytest.mark.parametrize("debug", [True, False])
def test_file_cleanup(create_command, myapp_unrolled, support_path, debug, capsys):
    """A single file can be cleaned up."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    myapp_unrolled.cleanup_paths = ["path/to/support/dir1/a_file1.txt"]

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm the named file (plus __pycache__) has been removed
    assert not (support_path / "dir1/a_file1.txt").exists()
    assert (support_path / "dir1/a_file2.doc").exists()
    assert (support_path / "dir1/b_file.txt").exists()
    assert (support_path / "dir2/b_file.txt").exists()
    assert not (support_path / "dir1/__pycache__").exists()
    assert (support_path / "other/deep/b_file.doc").exists()

    # Symlinks still exist
    assert (support_path / "dir1/symlink.txt").is_symlink()
    assert (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (6 if debug else 4)


@pytest.mark.parametrize("debug", [True, False])
def test_all_files_in_dir_cleanup(
    create_command, myapp_unrolled, support_path, debug, capsys
):
    """All files in a directory can be cleaned up."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    myapp_unrolled.cleanup_paths = ["path/to/support/dir1/*"]

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm the named files (and __pycache__) have been removed,
    # but the dir still exists
    assert not (support_path / "dir1/a_file1.txt").exists()
    assert not (support_path / "dir1/a_file2.doc").exists()
    assert not (support_path / "dir1/b_file.txt").exists()
    assert not (support_path / "dir1/__pycache__").exists()
    assert (support_path / "dir1").exists()
    assert (support_path / "dir2/b_file.txt").exists()
    assert (support_path / "other/deep/b_file.doc").exists()

    # Directory symlinks still exist; file symlink doesn't
    assert not (support_path / "dir1/symlink.txt").exists()
    assert (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (9 if debug else 4)


@pytest.mark.parametrize("debug", [True, False])
def test_dir_glob_cleanup(create_command, myapp_unrolled, support_path, debug, capsys):
    """A glob of directories can be cleaned up."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    myapp_unrolled.cleanup_paths = ["path/to/support/dir*"]

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm the matching directories have been removed
    assert not (support_path / "dir1").exists()
    assert not (support_path / "dir2").exists()
    assert (support_path / "other/deep/b_file.doc").exists()

    # Directory symlinks still exist; file symlink doesn't
    assert not (support_path / "dir1/symlink.txt").exists()
    assert (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (6 if debug else 4)


@pytest.mark.parametrize("debug", [True, False])
def test_file_glob_cleanup(create_command, myapp_unrolled, support_path, debug, capsys):
    """A glob of files can be cleaned up."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    myapp_unrolled.cleanup_paths = ["path/to/support/dir1/*.txt"]

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm the matching files (plus __pycache__) have been removed
    assert not (support_path / "dir1/a_file1.txt").exists()
    assert (support_path / "dir1/a_file2.doc").exists()
    assert not (support_path / "dir1/b_file.txt").exists()
    assert not (support_path / "dir1/__pycache__").exists()
    assert (support_path / "dir2/b_file.txt").exists()
    assert (support_path / "other/deep/b_file.doc").exists()

    # Directory symlinks still exist; file symlink doesn't
    assert not (support_path / "dir1/symlink.txt").exists()
    assert (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (8 if debug else 4)


@pytest.mark.parametrize("debug", [True, False])
def test_deep_glob_cleanup(create_command, myapp_unrolled, support_path, debug, capsys):
    """A glob that matches all directories will be added to the cleanup list."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    myapp_unrolled.cleanup_paths = ["path/to/support/**/b_file.*"]

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm the matching files (plus __pycache__) have been removed
    assert (support_path / "dir1/a_file1.txt").exists()
    assert (support_path / "dir1/a_file2.doc").exists()
    assert not (support_path / "dir1/b_file.txt").exists()
    assert not (support_path / "dir1/__pycache__").exists()
    assert not (support_path / "dir2/b_file.txt").exists()
    assert not (support_path / "other/deep/b_file.doc").exists()
    assert (support_path / "other/deep/other.doc").exists()

    # Symlinks still exist
    assert (support_path / "dir1/symlink.txt").is_symlink()
    assert (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (8 if debug else 4)


@pytest.mark.parametrize("debug", [True, False])
def test_symlink_cleanup(create_command, myapp_unrolled, support_path, debug, capsys):
    """Symlinks can be cleaned up."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    myapp_unrolled.cleanup_paths = [
        "path/to/support/dir1/symlink.txt",
        "path/to/support/mirror",
    ]

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm none of the files (except __pycache__) have been removed
    assert (support_path / "dir1/a_file1.txt").exists()
    assert (support_path / "dir1/a_file2.doc").exists()
    assert (support_path / "dir1/b_file.txt").exists()
    assert not (support_path / "dir1/__pycache__").exists()
    assert (support_path / "dir2/b_file.txt").exists()
    assert (support_path / "other/deep/b_file.doc").exists()

    # Directory symlinks still exist; file symlink doesn't
    assert not (support_path / "dir1/symlink.txt").exists()
    assert not (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (7 if debug else 4)


@pytest.mark.parametrize("debug", [True, False])
def test_template_glob_cleanup(
    create_command, myapp_unrolled, support_path, debug, capsys
):
    """A glob of files specified in the template will be added to the cleanup list."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    # Define a cleanup_paths in the template *and* on the app
    create_command._briefcase_toml[myapp_unrolled] = {
        "paths": {
            "cleanup_paths": ["path/to/support/dir1/a_*.*"],
        }
    }
    myapp_unrolled.cleanup_paths = ["path/to/support/other/*"]

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm the files from the app config and template config have been
    # removed, as well as __pycache__
    assert not (support_path / "dir1/a_file1.txt").exists()
    assert not (support_path / "dir1/a_file2.doc").exists()
    assert (support_path / "dir1/b_file.txt").exists()
    assert not (support_path / "dir1/__pycache__").exists()
    assert (support_path / "dir2/b_file.txt").exists()
    assert not (support_path / "other/deep/b_file.doc").exists()

    # Symlinks still exist
    assert (support_path / "dir1/symlink.txt").is_symlink()
    assert (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (8 if debug else 4)


@pytest.mark.parametrize("debug", [True, False])
def test_non_existent_cleanup(
    create_command,
    myapp_unrolled,
    support_path,
    debug,
    capsys,
):
    """Referencing a specific file that doesn't exist doesn't cause a problem."""
    if debug:
        create_command.console.verbosity = LogLevel.DEBUG

    myapp_unrolled.cleanup_paths = [
        # This file exists
        "path/to/support/dir1/a_file1.txt",
        # None of these paths do.
        "path/to/support/dir1/missing.txt",
        "path/to/support/nowhere",
        "path/to/support/absent/*",
    ]

    # Cleanup app content
    create_command.cleanup_app_content(myapp_unrolled)

    # Confirm the single existing file named has been removed,
    # as well as __pycache__
    assert not (support_path / "dir1/a_file1.txt").exists()
    assert (support_path / "dir1/a_file2.doc").exists()
    assert (support_path / "dir1/b_file.txt").exists()
    assert not (support_path / "dir1/__pycache__").exists()
    assert (support_path / "dir2/b_file.txt").exists()
    assert (support_path / "other/deep/b_file.doc").exists()

    # Symlinks still exist
    assert (support_path / "dir1/symlink.txt").is_symlink()
    assert (support_path / "mirror").is_symlink()

    # Console output ends with the done message; the number of other messages depends on
    # whether debug is enabled.
    output = capsys.readouterr().out.split("\n")
    assert output[-3] == "Removing unneeded app bundle content... done"
    assert len(output) == (6 if debug else 4)