Coverage for rust2rpm/tests/test_project.py: 100%
39 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-26 13:52 +0100
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-26 13:52 +0100
1from importlib import resources
2from pathlib import Path
4import pytest
5from cargo2rpm.semver import Version
7from rust2rpm.cli import Options
8from rust2rpm.conf import TomlConf
9from rust2rpm.project.crate import CrateProject, package_name_compat, package_name_suffixed, resolve_version
10from rust2rpm.project.local import LocalProject, guess_local_project_version_from_dir
13@pytest.mark.parametrize(
14 ("version", "available", "expected"),
15 [
16 (
17 "1.2.3",
18 [Version(1, 2, 3)],
19 Version.parse("1.2.3"),
20 ),
21 (
22 "1",
23 [Version(1, 2, 3), Version(1, 3, 5)],
24 Version.parse("1.3.5"),
25 ),
26 (
27 "^0.1",
28 [Version(1, 2, 3), Version(1, 3, 5)],
29 None,
30 ),
31 (
32 "^0.9.32",
33 [
34 Version(0, 10, 0, None, "cargo-0.80.0"),
35 Version(0, 9, 32, None, "cargo-0.79.0"),
36 Version(0, 9, 31, None, "cargo-0.78.0"),
37 ],
38 Version.parse("0.9.32+cargo-0.79.0"),
39 ),
40 ],
41 ids=repr,
42)
43def test_resolve_version(version: str, available: list[Version], expected: str | None):
44 assert resolve_version(version, lambda: available) == expected
47@pytest.mark.parametrize(
48 ("crate", "suffix", "expected"),
49 [
50 ("syn", "0.4", "rust-syn0.4"),
51 ("syn", "1", "rust-syn1"),
52 ("socket2", "0.3", "rust-socket2_0.3"),
53 ("version-compare", "0.0.11", "rust-version-compare0.0.11"),
54 ],
55 ids=repr,
56)
57def test_package_name_suffixed(crate: str, suffix: str, expected: str):
58 assert package_name_suffixed(crate, suffix) == expected
61@pytest.mark.parametrize(
62 ("crate", "version", "expected"),
63 [
64 ("syn", "0.4.0", "rust-syn0.4"),
65 ("syn", "1.0.109", "rust-syn1"),
66 ("socket2", "0.3.4", "rust-socket2_0.3"),
67 ("version-compare", "0.0.11", "rust-version-compare0.0.11"),
68 ],
69 ids=repr,
70)
71def test_package_name_compat(crate: str, version: str, expected: str):
72 assert package_name_compat(crate, version) == expected
75@pytest.mark.parametrize(
76 ("path", "project", "version"),
77 [
78 ("project-1.2.3", "project", "1.2.3"),
79 ("test-project-1.2.3", "test-project", "1.2.3"),
80 ],
81 ids=repr,
82)
83def test_guess_local_project_version_from_dir(path: str, project: str, version: str):
84 p, v = guess_local_project_version_from_dir(path)
85 assert (p, v) == (project, version)
88@pytest.mark.parametrize(
89 "path",
90 [
91 "helix-23.10-source",
92 ],
93 ids=repr,
94)
95def test_guess_local_project_version_from_dir_fail(path: str):
96 assert guess_local_project_version_from_dir(path) == (None, None)
99@pytest.mark.parametrize(
100 ("filename", "compat", "rpm_name"),
101 [
102 ("a-lib-0.1.0.crate", False, "rust-a-lib"),
103 ("a-lib-0.1.0.crate", True, "rust-a-lib0.1"),
104 ("a-bin-0.1.0.crate", False, "rust-a-bin"),
105 ],
106 ids=repr,
107)
108def test_project_from_local_crate(filename: str, compat: bool, rpm_name: str): # noqa: FBT001
109 path = Path(str(resources.files("rust2rpm.tests.data.fixtures").joinpath(filename)))
110 project = CrateProject.from_local_crate(path, None, TomlConf(), Options(compat=compat))
111 assert project.rpm_name == rpm_name
114@pytest.mark.parametrize(
115 ("folder", "name_hint", "version_hint", "rpm_name"),
116 [
117 ("a-bin", None, None, "a-bin"),
118 ("a-workspace", None, "0.1.0", "a-workspace"),
119 ],
120 ids=repr,
121)
122def test_project_from_cargo_toml_path(folder: str, name_hint: str | None, version_hint: str | None, rpm_name: str):
123 path = Path(str(resources.files("rust2rpm.tests.data.fixtures").joinpath(folder).joinpath("Cargo.toml")))
124 project = LocalProject.from_cargo_toml_path(path, name_hint, version_hint, None, TomlConf(), Options())
125 assert project.rpm_name == rpm_name
128@pytest.mark.parametrize(
129 ("folder", "name_hint", "version_hint", "rpm_name"),
130 [
131 ("a-bin", None, None, "a-bin"),
132 ("a-workspace", None, "0.1.0", "a-workspace"),
133 ],
134 ids=repr,
135)
136def test_project_from_project_folder(folder: str, name_hint: str | None, version_hint: str | None, rpm_name: str):
137 path = Path(str(resources.files("rust2rpm.tests.data.fixtures").joinpath(folder)))
138 project = LocalProject.from_project_folder(path, name_hint, version_hint, None, TomlConf(), Options())
139 assert project.rpm_name == rpm_name