Coverage for rust2rpm/tests/test_conf.py: 100%
118 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-21 23:36 +0100
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-21 23:36 +0100
1import textwrap
2from importlib import resources
3from pathlib import Path
5import pytest
7from rust2rpm.conf import (
8 ConfError,
9 FileInEx,
10 IniConf,
11 TomlConf,
12 load_config_ini,
13 load_config_toml,
14)
15from rust2rpm.conf.ini import to_list
16from rust2rpm.conf.toml import conf_comments_to_spec_comments
19@pytest.mark.parametrize(
20 ("string", "expected"),
21 [
22 ("1\n2", ["1", "2"]),
23 ("1\n\n 2", ["1", "2"]),
24 (" 2 ", ["2"]),
25 ("\n\n", []),
26 ],
27 ids=repr,
28)
29def test_to_list(string: str, expected: list[str]):
30 assert to_list(string) == expected
33@pytest.mark.parametrize(
34 ("filename", "features", "expected"),
35 [
36 (
37 "askalono-cli-0.4.6.rust2rpm.conf",
38 {"diagnostics"},
39 IniConf(enabled_features=["diagnostics"]),
40 ),
41 (
42 "chrono-0.4.23.rust2rpm.conf",
43 {"__doctest"},
44 IniConf(all_features=True, unwanted_features=["__doctest"]),
45 ),
46 (
47 "glib-sys-0.17.2.rust2rpm.conf",
48 {"v2_58", "v2_60", "v2_62", "v2_64", "v2_66", "v2_68", "v2_70", "v2_72", "v2_74", "v2_76"},
49 IniConf(
50 unwanted_features=["v2_76"],
51 buildrequires=["pkgconfig(glib-2.0) >= 2.56"],
52 lib_requires={
53 None: ["pkgconfig(glib-2.0) >= 2.56"],
54 "v2_58": ["pkgconfig(glib-2.0) >= 2.58"],
55 "v2_60": ["pkgconfig(glib-2.0) >= 2.60"],
56 "v2_62": ["pkgconfig(glib-2.0) >= 2.62"],
57 "v2_64": ["pkgconfig(glib-2.0) >= 2.64"],
58 "v2_66": ["pkgconfig(glib-2.0) >= 2.66"],
59 "v2_68": ["pkgconfig(glib-2.0) >= 2.68"],
60 "v2_70": ["pkgconfig(glib-2.0) >= 2.70"],
61 "v2_72": ["pkgconfig(glib-2.0) >= 2.72"],
62 "v2_74": ["pkgconfig(glib-2.0) >= 2.74"],
63 },
64 ),
65 ),
66 (
67 "libsqlite3-sys-0.25.2.rust2rpm.conf",
68 {"sqlcipher"},
69 IniConf(
70 buildrequires=["pkgconfig(sqlcipher)", "pkgconfig(sqlite3) >= 3.7.16"],
71 lib_requires={None: ["pkgconfig(sqlite3) >= 3.7.16"], "sqlcipher": ["pkgconfig(sqlcipher)"]},
72 ),
73 ),
74 ],
75)
76def test_ini_conf_load(
77 filename: str,
78 features: set[str],
79 expected: IniConf,
80):
81 path = str(resources.files("rust2rpm.tests.data.config").joinpath(filename))
82 conf = IniConf.load([path], "fedora", features)
83 assert conf == expected
85 path = str(resources.files("rust2rpm.tests.data.config").joinpath(filename))
86 conf = IniConf.load([path], "fedora", None)
87 assert conf == expected
90@pytest.mark.parametrize(
91 ("filename", "features", "expected"),
92 [
93 (
94 "askalono-cli-0.4.6.rust2rpm.toml",
95 {"diagnostics"},
96 TomlConf.from_data(
97 {
98 "features": {
99 "enable": [
100 "diagnostics",
101 ],
102 },
103 },
104 None,
105 ),
106 ),
107 (
108 "chrono-0.4.23.rust2rpm.toml",
109 {"__doctest"},
110 TomlConf.from_data(
111 {
112 "features": {
113 "enable-all": True,
114 "hide": [
115 "__doctest",
116 ],
117 },
118 },
119 None,
120 ),
121 ),
122 (
123 "dotenvy-0.15.7.rust2rpm.toml",
124 set(),
125 TomlConf.from_data(
126 {
127 "tests": {
128 "run": [
129 "lib",
130 "tests",
131 ],
132 "comments": [
133 "files required by doctests are not included in published crates",
134 ],
135 },
136 },
137 None,
138 ),
139 ),
140 (
141 "glib-sys-0.17.2.rust2rpm.toml",
142 {"v2_58", "v2_60", "v2_62", "v2_64", "v2_66", "v2_68", "v2_70", "v2_72", "v2_74", "v2_76"},
143 TomlConf.from_data(
144 {
145 "tests": {
146 "run": ["none"],
147 "comments": [
148 "tests are known to be broken upstream: https://github.com/gtk-rs/gtk-rs-core/issues/64",
149 ],
150 },
151 "features": {
152 "hide": ["v2_76"],
153 },
154 "requires": {
155 "build": [
156 "pkgconfig(glib-2.0) >= 2.56",
157 ],
158 "lib": [
159 "pkgconfig(glib-2.0) >= 2.56",
160 ],
161 "features": {
162 "v2_58": ["pkgconfig(glib-2.0) >= 2.58"],
163 "v2_60": ["pkgconfig(glib-2.0) >= 2.60"],
164 "v2_62": ["pkgconfig(glib-2.0) >= 2.62"],
165 "v2_64": ["pkgconfig(glib-2.0) >= 2.64"],
166 "v2_66": ["pkgconfig(glib-2.0) >= 2.66"],
167 "v2_68": ["pkgconfig(glib-2.0) >= 2.68"],
168 "v2_70": ["pkgconfig(glib-2.0) >= 2.70"],
169 "v2_72": ["pkgconfig(glib-2.0) >= 2.72"],
170 "v2_74": ["pkgconfig(glib-2.0) >= 2.74"],
171 },
172 },
173 },
174 None,
175 ),
176 ),
177 (
178 "libsqlite3-sys-0.25.2.rust2rpm.toml",
179 {"sqlcipher"},
180 TomlConf.from_data(
181 {
182 "requires": {
183 "build": [
184 "pkgconfig(sqlcipher)",
185 "pkgconfig(sqlite3) >= 3.7.16",
186 ],
187 "lib": [
188 "pkgconfig(sqlite3) >= 3.7.16",
189 ],
190 "features": {
191 "sqlcipher": [
192 "pkgconfig(sqlcipher)",
193 ],
194 },
195 },
196 },
197 None,
198 ),
199 ),
200 (
201 "nix-0.24.1.rust2rpm.toml",
202 set(),
203 TomlConf.from_data(
204 {
205 "tests": {
206 "run": ["none"],
207 "comments": ["tests are not reliable in containerized environments"],
208 },
209 },
210 None,
211 ),
212 ),
213 (
214 "reqwest-0.11.20.rust2rpm.toml",
215 set(),
216 TomlConf.from_data(
217 {
218 "tests": {
219 "skip": [
220 "test_allowed_methods",
221 "test_badssl_modern",
222 "test_badssl_self_signed",
223 ],
224 "skip-exact": True,
225 "comments": [
226 "skip tests which require internet access",
227 ],
228 },
229 },
230 None,
231 ),
232 ),
233 ],
234)
235def test_toml_conf_load(
236 filename: str,
237 features: set[str],
238 expected: TomlConf,
239):
240 path = Path(str(resources.files("rust2rpm.tests.data.config").joinpath(filename)))
241 conf = TomlConf.load(path, features)
242 assert conf == expected
245@pytest.mark.parametrize(
246 ("filename", "features", "expected"),
247 [
248 (
249 "askalono-cli-0.4.6.rust2rpm.conf",
250 {"diagnostics"},
251 TomlConf.from_data(
252 {
253 "features": {
254 "enable": [
255 "diagnostics",
256 ],
257 },
258 },
259 None,
260 ),
261 ),
262 (
263 "chrono-0.4.23.rust2rpm.conf",
264 {"__doctest"},
265 TomlConf.from_data(
266 {
267 "features": {
268 "enable-all": True,
269 "hide": [
270 "__doctest",
271 ],
272 },
273 },
274 None,
275 ),
276 ),
277 (
278 "glib-sys-0.17.2.rust2rpm.conf",
279 {"v2_58", "v2_60", "v2_62", "v2_64", "v2_66", "v2_68", "v2_70", "v2_72", "v2_74", "v2_76"},
280 TomlConf.from_data(
281 {
282 "features": {
283 "hide": ["v2_76"],
284 },
285 "requires": {
286 "build": [
287 "pkgconfig(glib-2.0) >= 2.56",
288 ],
289 "lib": [
290 "pkgconfig(glib-2.0) >= 2.56",
291 ],
292 "features": {
293 "v2_58": ["pkgconfig(glib-2.0) >= 2.58"],
294 "v2_60": ["pkgconfig(glib-2.0) >= 2.60"],
295 "v2_62": ["pkgconfig(glib-2.0) >= 2.62"],
296 "v2_64": ["pkgconfig(glib-2.0) >= 2.64"],
297 "v2_66": ["pkgconfig(glib-2.0) >= 2.66"],
298 "v2_68": ["pkgconfig(glib-2.0) >= 2.68"],
299 "v2_70": ["pkgconfig(glib-2.0) >= 2.70"],
300 "v2_72": ["pkgconfig(glib-2.0) >= 2.72"],
301 "v2_74": ["pkgconfig(glib-2.0) >= 2.74"],
302 },
303 },
304 },
305 None,
306 ),
307 ),
308 (
309 "libsqlite3-sys-0.25.2.rust2rpm.conf",
310 {"sqlcipher"},
311 TomlConf.from_data(
312 {
313 "requires": {
314 "build": [
315 "pkgconfig(sqlcipher)",
316 "pkgconfig(sqlite3) >= 3.7.16",
317 ],
318 "lib": [
319 "pkgconfig(sqlite3) >= 3.7.16",
320 ],
321 "features": {
322 "sqlcipher": [
323 "pkgconfig(sqlcipher)",
324 ],
325 },
326 },
327 },
328 None,
329 ),
330 ),
331 ],
332)
333def test_conf_toml_from_ini(
334 filename: str,
335 features: set[str],
336 expected: TomlConf,
337):
338 path = str(resources.files("rust2rpm.tests.data.config").joinpath(filename))
339 conf = IniConf.load([path], "fedora", features)
340 toml = conf.upgrade()
341 assert toml == expected
344@pytest.mark.parametrize(
345 ("filename", "features", "expected"),
346 [
347 (
348 "difftastic-0.61.0.rust2rpm.toml",
349 set(),
350 [
351 "# * drop crossterm/windows",
352 "# * unpin libmimalloc-sys",
353 "# * relax upper bounds on:",
354 "# - assert_cmd",
355 "# - bumpalo",
356 "# - home",
357 "# - ignore",
358 ],
359 ),
360 ],
361 ids=repr,
362)
363def test_conf_to_patch_comments(filename: str, features: set[str], expected: str):
364 path = Path(str(resources.files("rust2rpm.tests.data.config").joinpath(filename)))
365 conf = TomlConf.load(path, features)
366 assert conf_comments_to_spec_comments(conf.package.cargo_toml_patch_comments) == expected
369@pytest.mark.parametrize(
370 ("filename", "features"),
371 [
372 ("askalono-cli-0.4.6.rust2rpm.conf", {"diagnostics"}),
373 ("chrono-0.4.23.rust2rpm.conf", {"__doctest"}),
374 ("example-full.rust2rpm.conf", {"foo", "bar", "baz"}),
375 ("example-most.rust2rpm.conf", {"foo", "bar", "baz"}),
376 ("example-some.rust2rpm.conf", set()),
377 (
378 "glib-sys-0.17.2.rust2rpm.conf",
379 {"v2_58", "v2_60", "v2_62", "v2_64", "v2_66", "v2_68", "v2_70", "v2_72", "v2_74", "v2_76"},
380 ),
381 ("libsqlite3-sys-0.25.2.rust2rpm.conf", {"sqlcipher"}),
382 ],
383)
384def test_load_config_ini(filename: str, features: set[str]):
385 path = str(resources.files("rust2rpm.tests.data.config").joinpath(filename))
386 load_config_ini([path], "fedora", features)
389@pytest.mark.parametrize(
390 ("filename", "features"),
391 [
392 ("askalono-cli-0.4.6.rust2rpm.toml", {"diagnostics"}),
393 ("bindgen-cli-0.69.4.rust2rpm.toml", set()),
394 ("chrono-0.4.23.rust2rpm.toml", {"__doctest"}),
395 ("difftastic-0.61.0.rust2rpm.toml", set()),
396 ("dotenvy-0.15.7.rust2rpm.toml", set()),
397 (
398 "glib-sys-0.17.2.rust2rpm.toml",
399 {"v2_58", "v2_60", "v2_62", "v2_64", "v2_66", "v2_68", "v2_70", "v2_72", "v2_74", "v2_76"},
400 ),
401 ("libsqlite3-sys-0.25.2.rust2rpm.toml", {"sqlcipher"}),
402 ("nix-0.24.1.rust2rpm.toml", set()),
403 ("reqwest-0.11.20.rust2rpm.toml", set()),
404 ("tokio-1.19.2.rust2rpm.toml", set()),
405 ],
406)
407def test_load_config_toml(filename: str, features: set[str]):
408 path = Path(str(resources.files("rust2rpm.tests.data.config").joinpath(filename)))
409 load_config_toml(path, features)
412@pytest.mark.parametrize(
413 ("filename", "features"),
414 [
415 ("askalono-cli-0.4.6.rust2rpm.conf", {"diagnostics"}),
416 ("chrono-0.4.23.rust2rpm.conf", {"__doctest"}),
417 ("example-full.rust2rpm.conf", {"foo", "bar", "baz"}),
418 ("example-most.rust2rpm.conf", {"foo", "bar", "baz"}),
419 ("example-some.rust2rpm.conf", set()),
420 (
421 "glib-sys-0.17.2.rust2rpm.conf",
422 {"v2_58", "v2_60", "v2_62", "v2_64", "v2_66", "v2_68", "v2_70", "v2_72", "v2_74", "v2_76"},
423 ),
424 ("libsqlite3-sys-0.25.2.rust2rpm.conf", {"sqlcipher"}),
425 ],
426)
427def test_conf_upgrade(filename: str, features: set[str]):
428 path = str(resources.files("rust2rpm.tests.data.config").joinpath(filename))
429 assert Path(path).exists()
430 IniConf.load([path], "fedora", features).migrate(features)
433@pytest.mark.parametrize(
434 "features",
435 [{"foobar", "barbaz", "bazbar", "barbar"}, None],
436 ids=repr,
437)
438def test_load_full_example(features: set[str] | None): # noqa: PLR0915
439 path = Path(str(resources.files("rust2rpm.tests.data.config").joinpath("example-full.rust2rpm.toml")))
440 conf = TomlConf.load(path, features)
442 # [package] table
443 assert conf.package.summary == "Summary"
444 assert conf.package.description == "Description Line 1\nDescription Line 2"
445 assert conf.package.url == "https://example.com/rust2rpm/example-full"
446 assert conf.package.source_url == "https://example.com/rust2rpm/example-full/archive/0.1.0.tar.gz"
447 assert conf.package.supported_arches == ["x86_64", "aarch64"]
448 assert conf.package.bin_package_name == "hello"
449 assert conf.package.cargo_install_bin is True
450 assert conf.package.cargo_install_lib is True
451 assert conf.package.suppress_cdylib_install_fixme is True
452 assert conf.package.debuginfo_level == 2
453 assert conf.package.cargo_toml_patch_comments == ["hello", "world"]
455 assert isinstance(conf.package.license_files, FileInEx)
456 assert conf.package.license_files.include == ["LICENXE-MIT", "LICENXE-Apache"]
457 assert conf.package.license_files.exclude == ["COPYRIGHTIE", "COPYWRONGIE"]
459 assert isinstance(conf.package.doc_files, list)
460 assert conf.package.doc_files == ["README.rstuvwxyz", "NEWSIES"]
462 assert isinstance(conf.package.extra_sources, list)
463 assert conf.package.extra_sources[0].number == 3
464 assert conf.package.extra_sources[0].file == "man.1"
465 assert conf.package.extra_sources[0].comments == ["a man page"]
466 assert conf.package.extra_sources[0].comment_lines == ["# * a man page"]
468 assert isinstance(conf.package.extra_patches, list)
469 assert conf.package.extra_patches[0].number == 3
470 assert conf.package.extra_patches[0].file == "a.patch"
471 assert conf.package.extra_patches[0].comments == ["a patch"]
472 assert conf.package.extra_patches[0].comment_lines == ["# * a patch"]
474 assert conf.package.extra_files == ["%{_mandir}/man1/man.1*"]
475 assert conf.package.exclude_crate_files == ["assets/"]
476 assert conf.package.bin_renames == {"hello": "world"}
478 # [scripts] table
479 assert conf.scripts.prep.pre == ['echo "HELLO!"']
480 assert conf.scripts.prep.post == ['echo "GOODBYE!"']
481 assert conf.scripts.build.pre == ['echo "HELLO!"']
482 assert conf.scripts.build.post == ['echo "GOODBYE!"']
483 assert conf.scripts.install.pre == ['echo "HELLO!"']
484 assert conf.scripts.install.post == ['echo "GOODBYE!"']
485 assert conf.scripts.check.pre == ['echo "HELLO!"']
486 assert conf.scripts.check.post == ['echo "GOODBYE!"']
488 # [tests] table
489 assert conf.tests.run == ["bin", "lib", "doc", "tests"]
490 assert conf.tests.skip == ["foo", "bar"]
491 assert conf.tests.skip_exact is True
492 assert conf.tests.comments == ["run most but not all tests"]
494 # [features] table
495 assert conf.features.enable_all is False
496 assert conf.features.enable == ["foobar", "barbaz"]
497 assert conf.features.disable_default is False
498 assert conf.features.hide == ["bazbar"]
500 # [requires] table
501 assert conf.requires.build == ["echo"]
502 assert conf.requires.test == ["echo"]
503 assert conf.requires.bin == ["echo"]
504 assert conf.requires.lib == ["echo"]
505 assert conf.requires.features == {"barbar": ["echo"]}
508def test_load_some_example():
509 path = Path(str(resources.files("rust2rpm.tests.data.config").joinpath("example-some.rust2rpm.toml")))
510 conf = TomlConf.load(path, set())
512 assert isinstance(conf.package.license_files, list)
513 assert conf.package.license_files == ["LICENXE-MIT", "LICENXE-Apache"]
515 assert isinstance(conf.package.doc_files, FileInEx)
516 assert conf.package.doc_files.include == ["README.rstuvwxyz", "NEWSIES"]
517 assert conf.package.doc_files.exclude == ["HELLO"]
519 assert conf.package.extra_sources == []
520 assert conf.package.extra_patches == []
522 assert conf.tests.run == ["none"]
523 assert conf.tests.comments == ["run no tests"]
526@pytest.mark.parametrize(
527 ("string", "error"),
528 [
529 (
530 textwrap.dedent(
531 """\
532 [tests]
533 run = ["none", "lib"]
534 comments = []
535 """,
536 ),
537 "Invalid set of tests to run:",
538 ),
539 (
540 textwrap.dedent(
541 """\
542 [tests]
543 run = ["all", "lib"]
544 comments = []
545 """,
546 ),
547 "Invalid set of tests to run:",
548 ),
549 (
550 textwrap.dedent(
551 """\
552 [features]
553 enable-all = true
554 disable-default = true
555 """,
556 ),
557 "Conflicting settings for features: 'enable-all' and 'disable-default'",
558 ),
559 (
560 textwrap.dedent(
561 """\
562 [features]
563 enable = ["foo"]
564 """,
565 ),
566 "Unrecognized enabled feature: foo",
567 ),
568 (
569 textwrap.dedent(
570 """\
571 [features]
572 hide = ["foo"]
573 """,
574 ),
575 "Unrecognized hidden feature: foo",
576 ),
577 (
578 textwrap.dedent(
579 """\
580 [requires]
581 features.foo = ["bar"]
582 """,
583 ),
584 "Unrecognized Requires for feature: foo",
585 ),
586 ],
587 ids=repr,
588)
589def test_load_fail(string: str, error: str):
590 with pytest.raises(ConfError) as exc:
591 TomlConf.from_str(string, set())
592 assert error in str(exc)