forked from conda/constructor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_briefcase.py
More file actions
277 lines (232 loc) · 10.1 KB
/
test_briefcase.py
File metadata and controls
277 lines (232 loc) · 10.1 KB
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
import sys
import tarfile
from pathlib import Path
import pytest
from constructor.briefcase import Payload, get_bundle_app_name, get_name_version
from constructor.conda_interface import cc_platform
"""
Here 'mock_info' is simply a 'mock' of the regular 'info' object that is used to create installers.
It contains bare minimum in order to allow simple unit testing.
"""
mock_info = {
"name": "MockInfo",
"version": "1.0.0",
"_conda_exe": str(Path(sys.prefix) / "standalone_conda" / "conda.exe"),
"_download_dir": "",
"_dists": [],
"_platform": cc_platform,
"_urls": [],
}
@pytest.mark.parametrize(
"name_in, version_in, name_expected, version_expected",
[
# Valid versions
("Miniconda", "1", "Miniconda", "1"),
("Miniconda", "1.2", "Miniconda", "1.2"),
("Miniconda", "1.2.3", "Miniconda", "1.2.3"),
("Miniconda", "1.2a1", "Miniconda", "1.2a1"),
("Miniconda", "1.2b2", "Miniconda", "1.2b2"),
("Miniconda", "1.2rc3", "Miniconda", "1.2rc3"),
("Miniconda", "1.2.post4", "Miniconda", "1.2.post4"),
("Miniconda", "1.2.dev5", "Miniconda", "1.2.dev5"),
("Miniconda", "1.2rc3.post4.dev5", "Miniconda", "1.2rc3.post4.dev5"),
# Hyphens are treated as dots
("Miniconda", "1.2-3", "Miniconda", "1.2.3"),
("Miniconda", "1.2-3.4-5.6", "Miniconda", "1.2.3.4.5.6"),
# Additional text before and after the last valid version should be treated as
# part of the name.
("Miniconda", "1.2 3.4 5.6", "Miniconda 1.2 3.4", "5.6"),
("Miniconda", "1.2_3.4_5.6", "Miniconda 1.2_3.4", "5.6"),
("Miniconda", "1.2c3", "Miniconda 1.2c", "3"),
("Miniconda", "1.2rc3.dev5.post4", "Miniconda 1.2rc3.dev5.post", "4"),
("Miniconda", "py313", "Miniconda py", "313"),
("Miniconda", "py.313", "Miniconda py", "313"),
("Miniconda", "py3.13", "Miniconda py", "3.13"),
("Miniconda", "py313_1.2", "Miniconda py313", "1.2"),
("Miniconda", "1.2 and more", "Miniconda and more", "1.2"),
("Miniconda", "1.2! and more", "Miniconda ! and more", "1.2"),
("Miniconda", "py313 1.2 and more", "Miniconda py313 and more", "1.2"),
# Numbers in the name are not added to the version.
("Miniconda3", "1", "Miniconda3", "1"),
],
)
def test_name_version(name_in, version_in, name_expected, version_expected):
name_actual, version_actual = get_name_version(
{"name": name_in, "version": version_in},
)
assert (name_actual, version_actual) == (name_expected, version_expected)
@pytest.mark.parametrize(
"info",
[
{},
{"name": ""},
],
)
def test_name_empty(info):
with pytest.raises(ValueError, match="Name is empty"):
get_name_version(info)
@pytest.mark.parametrize(
"info",
[
{"name": "Miniconda"},
{"name": "Miniconda", "version": ""},
],
)
def test_version_empty(info):
with pytest.raises(ValueError, match="Version is empty"):
get_name_version(info)
@pytest.mark.parametrize("version_in", ["x", ".", " ", "hello"])
def test_version_invalid(version_in, caplog):
name_actual, version_actual = get_name_version(
{"name": "Miniconda3", "version": version_in},
)
assert name_actual == f"Miniconda3 {version_in}"
assert version_actual == "0.0.1"
assert caplog.messages == [
f"Version {version_in!r} contains no valid version numbers; defaulting to 0.0.1"
]
@pytest.mark.parametrize(
"rdi, name, bundle_expected, app_name_expected",
[
# Valid rdi
("org.conda", "ignored", "org", "conda"),
("org.Conda", "ignored", "org", "Conda"),
("org.conda-miniconda", "ignored", "org", "conda-miniconda"),
("org.conda_miniconda", "ignored", "org", "conda_miniconda"),
("org-conda.miniconda", "ignored", "org-conda", "miniconda"),
("org.conda.miniconda", "ignored", "org.conda", "miniconda"),
("org.conda.1", "ignored", "org.conda", "1"),
# Invalid rdi
("org.hello-", "Miniconda", "org", "hello"),
("org.-hello", "Miniconda", "org", "hello"),
("org.hello world", "Miniconda", "org", "hello-world"),
("org.hello!world", "Miniconda", "org", "hello-world"),
# Missing rdi
(None, "x", "io.continuum", "x"),
(None, "X", "io.continuum", "x"),
(None, "1", "io.continuum", "1"),
(None, "Miniconda", "io.continuum", "miniconda"),
(None, "Miniconda3", "io.continuum", "miniconda3"),
(None, "Miniconda3 py313", "io.continuum", "miniconda3-py313"),
(None, "Hello, world!", "io.continuum", "hello-world"),
],
)
def test_bundle_app_name(rdi, name, bundle_expected, app_name_expected):
bundle_actual, app_name_actual = get_bundle_app_name({"reverse_domain_identifier": rdi}, name)
assert (bundle_actual, app_name_actual) == (bundle_expected, app_name_expected)
@pytest.mark.parametrize("rdi", ["", "org"])
def test_rdi_no_dots(rdi):
with pytest.raises(ValueError, match=f"reverse_domain_identifier '{rdi}' contains no dots"):
get_bundle_app_name({"reverse_domain_identifier": rdi}, "ignored")
@pytest.mark.parametrize("rdi", ["org.", "org.hello.", "org.hello.-"])
def test_rdi_invalid_package(rdi):
with pytest.raises(
ValueError,
match=(
f"Last component of reverse_domain_identifier '{rdi}' "
f"contains no alphanumeric characters"
),
):
get_bundle_app_name({"reverse_domain_identifier": rdi}, "ignored")
@pytest.mark.parametrize("name", ["", " ", "!", "-", "---"])
def test_name_no_alphanumeric(name):
with pytest.raises(ValueError, match=f"Name '{name}' contains no alphanumeric characters"):
get_bundle_app_name({}, name)
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
def test_prepare_payload():
"""Test preparing the payload."""
info = mock_info.copy()
payload = Payload(info)
payload.prepare()
assert payload.root.is_dir()
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
def test_payload_layout():
"""Test the layout of the payload and verify that archiving
parts of the payload works as expected.
"""
info = mock_info.copy()
payload = Payload(info)
prepared_payload = payload.prepare()
external_dir = prepared_payload.root / "external"
assert external_dir.is_dir() and external_dir == prepared_payload.external
base_dir = prepared_payload.root / "external" / "base"
pkgs_dir = prepared_payload.root / "external" / "base" / "pkgs"
archive_path = external_dir / payload.archive_name
# Since archiving removes the directory 'base_dir' and its contents
assert not base_dir.exists()
assert not pkgs_dir.exists()
assert archive_path.exists()
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
def test_payload_archive(tmp_path: Path):
"""Test that the payload archive function works as expected."""
info = mock_info.copy()
payload = Payload(info)
foo_dir = tmp_path / "foo"
foo_dir.mkdir()
expected_text = "some test text"
hello_file = foo_dir / "hello.txt"
hello_file.write_text(expected_text, encoding="utf-8")
archive_path = payload.make_archive(foo_dir, tmp_path)
with tarfile.open(archive_path, mode="r:gz") as tar:
member = tar.getmember("foo/hello.txt")
f = tar.extractfile(member)
assert f is not None
assert f.read().decode("utf-8") == expected_text
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
def test_payload_remove():
"""Test removing the payload."""
info = mock_info.copy()
payload = Payload(info)
prepared_payload = payload.prepare()
assert prepared_payload.root.is_dir()
payload.remove()
assert not prepared_payload.root.is_dir()
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
def test_payload_pyproject_toml():
"""Test that the pyproject.toml file is created when the payload is prepared."""
info = mock_info.copy()
payload = Payload(info)
prepared_payload = payload.prepare()
pyproject_toml = prepared_payload.root / "pyproject.toml"
assert pyproject_toml.is_file()
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
def test_payload_conda_exe():
"""Test that conda-standalone is prepared."""
info = mock_info.copy()
payload = Payload(info)
prepared_payload = payload.prepare()
conda_exe = prepared_payload.external / "_conda.exe"
assert conda_exe.is_file()
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
@pytest.mark.parametrize("debug_logging", [True, False])
def test_payload_templates_are_rendered(debug_logging):
"""Test that templates are rendered when the payload is prepared."""
info = mock_info.copy()
payload = Payload(info)
payload.add_debug_logging = debug_logging
rendered_templates = payload.render_templates()
assert len(rendered_templates) == 2 # There should be at least two files
for f in rendered_templates:
assert f.dst.is_file()
text = f.dst.read_text(encoding="utf-8")
assert "{{" not in text and "}}" not in text
assert "{%" not in text and "%}" not in text
assert "{#" not in text and "#}" not in text
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
@pytest.mark.parametrize("debug_logging", [True, False])
def test_templates_debug_mode(debug_logging):
"""Test that debug logging affects template generation."""
info = mock_info.copy()
payload = Payload(info)
payload.add_debug_logging = debug_logging
rendered_templates = payload.render_templates()
assert len(rendered_templates) == 2 # There should be at least two files
for f in rendered_templates:
assert f.dst.is_file()
with open(f.dst) as open_file:
lines = open_file.readlines()
# Check the first line.
expected = "@echo on\n" if debug_logging else "@echo off\n"
assert lines[0] == expected
# If debug_logging is True, we expect to find %LOG%, otherwise not.
assert debug_logging == any("%LOG%" in line for line in lines)