File: test_install_image.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 (384 lines) | stat: -rw-r--r-- 12,635 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
import shutil
from unittest import mock


def test_no_source(create_command, tmp_path):
    """If the app doesn't define a source, no image is installed."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Try to install the image from no source.
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source=None,
        variant=None,
        size=None,
        target=out_path,
    )

    # No file was installed.
    create_command.tools.shutil.copy.assert_not_called()


def test_no_source_with_size(create_command, tmp_path):
    """If the app doesn't define a source, and a size is requested, no image is
    installed."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Try to install the image from no source.
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source=None,
        variant=None,
        size="3742",
        target=out_path,
    )

    # No file was installed.
    create_command.tools.shutil.copy.assert_not_called()


def test_no_requested_size(create_command, tmp_path, capsys):
    """If the app specifies a no-size image, an un-annotated image is used."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Create the source image
    source_file = tmp_path / "base_path/input/original.png"
    source_file.parent.mkdir(parents=True, exist_ok=True)
    with source_file.open("w", encoding="utf-8") as f:
        f.write("image")

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source="input/original",
        variant=None,
        size=None,
        target=out_path,
    )

    # The right message was written to output
    expected = (
        "Installing input/original.png as sample image... started\n"
        "Installing input/original.png as sample image... done\n\n"
    )
    assert capsys.readouterr().out == expected

    # The file was copied into position
    create_command.tools.shutil.copy.assert_called_with(
        create_command.base_path / "input/original.png",
        out_path,
    )


def test_no_requested_size_invalid_path(create_command, tmp_path, capsys):
    """If the app specifies a no-size image that doesn't exist, an error is raised."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)
    create_command.tools.shutil.copy.side_effect = FileNotFoundError

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source="input/original",
        variant=None,
        size=None,
        target=out_path,
    )

    # The right message was written to output
    expected = "Unable to find input/original.png for sample image; using default\n"
    assert capsys.readouterr().out == expected

    # The file was not copied
    assert create_command.tools.shutil.copy.call_count == 0


def test_requested_size(create_command, tmp_path, capsys):
    """If the app specifies a sized image, an annotated image filename is used."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Create the source image
    source_file = tmp_path / "base_path/input/original-3742.png"
    source_file.parent.mkdir(parents=True, exist_ok=True)
    with source_file.open("w", encoding="utf-8") as f:
        f.write("image")

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source="input/original",
        variant=None,
        size="3742",
        target=out_path,
    )

    # The right message was written to output
    expected = (
        "Installing input/original-3742.png as 3742px sample image... started\n"
        "Installing input/original-3742.png as 3742px sample image... done\n\n"
    )
    assert capsys.readouterr().out == expected

    # The file was copied into position
    create_command.tools.shutil.copy.assert_called_with(
        create_command.base_path / "input/original-3742.png",
        out_path,
    )


def test_requested_size_invalid_path(create_command, tmp_path, capsys):
    """If the app specifies a sized image that doesn't exist, an error is raised."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)
    create_command.tools.shutil.copy.side_effect = FileNotFoundError

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source="input/original",
        variant=None,
        size="3742",
        target=out_path,
    )

    # The right message was written to output
    expected = "Unable to find input/original-3742.png for 3742px sample image; using default\n"
    assert capsys.readouterr().out == expected

    # The file was not copied
    assert create_command.tools.shutil.copy.call_count == 0


def test_variant_with_no_requested_size(create_command, tmp_path, capsys):
    """If the app specifies a variant with no size, the variant is used unsized."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Create the source image
    source_file = tmp_path / "base_path/input/original.png"
    source_file.parent.mkdir(parents=True, exist_ok=True)
    with source_file.open("w", encoding="utf-8") as f:
        f.write("image")

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source={
            "round": "input/original",
        },
        variant="round",
        size=None,
        target=out_path,
    )

    # The right message was written to output
    expected = (
        "Installing input/original.png as round sample image... started\n"
        "Installing input/original.png as round sample image... done\n\n"
    )
    assert capsys.readouterr().out == expected

    # The file was copied into position
    create_command.tools.shutil.copy.assert_called_with(
        create_command.base_path / "input/original.png",
        out_path,
    )


def test_variant_without_variant_source_and_no_requested_size(
    create_command, tmp_path, capsys
):
    """If the template specifies a variant with no size, but app doesn't define
    variants, the default construction is used."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Create the source image
    source_file = tmp_path / "base_path/input/original.png"
    source_file.parent.mkdir(parents=True, exist_ok=True)
    with source_file.open("w", encoding="utf-8") as f:
        f.write("image")

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source="input/original",
        variant="round",
        size=None,
        target=out_path,
    )

    # The right message was written to output
    expected = "Unable to find input/original-round.png for round sample image; using default\n"
    assert capsys.readouterr().out == expected

    # No file was installed.
    create_command.tools.shutil.copy.assert_not_called()


def test_unknown_variant_with_no_requested_size(create_command, tmp_path, capsys):
    """If the app specifies an unknown variant, a message is reported."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Create the source image
    source_file = tmp_path / "base_path/input/original.png"
    source_file.parent.mkdir(parents=True, exist_ok=True)
    with source_file.open("w", encoding="utf-8") as f:
        f.write("image")

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source={
            "round": "input/original",
        },
        variant="unknown",
        size=None,
        target=out_path,
    )

    # The right message was written to output
    expected = "Unknown variant 'unknown' for sample image; using default\n"
    assert capsys.readouterr().out == expected

    # No file was installed.
    create_command.tools.shutil.copy.assert_not_called()


def test_variant_with_size(create_command, tmp_path, capsys):
    """If the app specifies a variant with a size, the sized variant is used."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Create the source image
    source_file = tmp_path / "base_path/input/original-3742.png"
    source_file.parent.mkdir(parents=True, exist_ok=True)
    with source_file.open("w", encoding="utf-8") as f:
        f.write("image")

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source={
            "round": "input/original",
        },
        variant="round",
        size="3742",
        target=out_path,
    )

    # The right message was written to output
    expected = (
        "Installing input/original-3742.png as 3742px round sample image... started\n"
        "Installing input/original-3742.png as 3742px round sample image... done\n\n"
    )
    assert capsys.readouterr().out == expected

    # The file was copied into position
    create_command.tools.shutil.copy.assert_called_with(
        create_command.base_path / "input/original-3742.png",
        out_path,
    )


def test_variant_with_size_without_variants(create_command, tmp_path, capsys):
    """If the app specifies a variant with a size, but no variants are specified, a
    message is output."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Create the source image
    source_file = tmp_path / "base_path/input/original-3742.png"
    source_file.parent.mkdir(parents=True, exist_ok=True)
    with source_file.open("w", encoding="utf-8") as f:
        f.write("image")

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source="input/original",
        variant="round",
        size="3742",
        target=out_path,
    )

    # The right message was written to output
    expected = "Unable to find input/original-round-3742.png for 3742px round sample image; using default\n"
    assert capsys.readouterr().out == expected

    # No file was installed.
    create_command.tools.shutil.copy.assert_not_called()


def test_unknown_variant_with_size(create_command, tmp_path, capsys):
    """If the app specifies an unknown variant with a size, but no variants are
    specified, a message is output."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Create the source image
    source_file = tmp_path / "base_path/input/original-3742.png"
    source_file.parent.mkdir(parents=True, exist_ok=True)
    with source_file.open("w", encoding="utf-8") as f:
        f.write("image")

    # Try to install the image
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source={
            "round": "input/original",
        },
        variant="unknown",
        size="3742",
        target=out_path,
    )

    # The right message was written to output
    expected = "Unknown variant 'unknown' for 3742px sample image; using default\n"
    assert capsys.readouterr().out == expected

    # No file was installed.
    create_command.tools.shutil.copy.assert_not_called()


def test_unsized_variant(create_command, tmp_path, capsys):
    """If the app specifies an unsized variant, it is used."""
    create_command.tools.shutil = mock.MagicMock(spec_set=shutil)

    # Create the source image
    source_file = tmp_path / "base_path/input/original.png"
    source_file.parent.mkdir(parents=True, exist_ok=True)
    with source_file.open("w", encoding="utf-8") as f:
        f.write("image")

    # Try to install the image
    # Unsized variants are an annoying edge case; they get the *variant*
    # as the *size*.
    out_path = tmp_path / "output.png"
    create_command.install_image(
        "sample image",
        source={
            "round": "input/original",
        },
        variant=None,
        size="round",
        target=out_path,
    )

    # The right message was written to output
    expected = (
        "Installing input/original.png as round sample image... started\n"
        "Installing input/original.png as round sample image... done\n\n"
    )
    assert capsys.readouterr().out == expected

    # The file was copied into position
    create_command.tools.shutil.copy.assert_called_with(
        create_command.base_path / "input/original.png",
        out_path,
    )