-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathtest_rust.py
More file actions
119 lines (94 loc) · 3.39 KB
/
Copy pathtest_rust.py
File metadata and controls
119 lines (94 loc) · 3.39 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
# SPDX-License-Identifier: GPL-3.0-only
from pathlib import Path
import pytest
import tomlkit
from hermeto.core.models.output import ProjectFile
from hermeto.core.package_managers.python.pip.rust import (
_get_rust_root_dir,
_merge_cargo_config_files,
_shortest_path_parent,
)
@pytest.mark.parametrize(
"cargo_files,expected_rust_root_dir",
[
pytest.param(
(Path("/tmp/foo/Cargo.toml"), Path("/tmp/bar/baz/Cargo.toml")),
Path("/tmp/foo"),
id="simple_ordering",
),
pytest.param(
(Path("/tmp/bar/baz/Cargo.toml"), Path("/tmp/foo/Cargo.toml")),
Path("/tmp/foo"),
id="reversed_simple_ordering",
),
pytest.param(
(
Path("/tmp/bar/baz/Cargo.toml"),
Path("/tmp/foo/Cargo.toml"),
Path("/tmp/foo/quux/Cargo.toml"),
),
Path("/tmp/foo"),
id="tricky_ordering",
),
],
)
def test_the_shortest_path_in_cargo_package_is_inferred_as_root(
cargo_files: tuple, expected_rust_root_dir: Path
) -> None:
inferred_rust_root_dir = _shortest_path_parent(cargo_files)
assert inferred_rust_root_dir == expected_rust_root_dir
def test_get_rust_root_dir_returns_none_if_no_rust_files_exist(tmp_path: Path) -> None:
assert _get_rust_root_dir(tmp_path) is None
def test_get_rust_root_dir_falls_back_to_cargo_toml(tmp_path: Path) -> None:
(tmp_path / "Cargo.toml").touch()
assert _get_rust_root_dir(tmp_path) == tmp_path
def test_get_rust_root_dir_prefers_cargo_lock_over_cargo_toml(tmp_path: Path) -> None:
(tmp_path / "Cargo.toml").touch()
subdir = tmp_path / "workspace-package"
subdir.mkdir()
(subdir / "Cargo.lock").touch()
(subdir / "Cargo.toml").touch()
assert _get_rust_root_dir(tmp_path) == subdir
def test_merge_cargo_config_files() -> None:
config1 = """
[source.crates-io]
replace-with = "vendored-sources"
[source."git+https://github.com/org1/repo1.git?tag=0.1.0"]
git = "https://github.com/org1/repo1.git"
tag = "0.1.0"
replace-with = "vendored-sources"
[source.vendored-sources]
directory = "${output_dir}/deps/cargo"
"""
config2 = """
[source.crates-io]
replace-with = "vendored-sources"
[source."git+https://github.com/org2/repo2.git?tag=0.2.0"]
git = "https://github.com/org2/repo2.git"
tag = "0.2.0"
replace-with = "vendored-sources"
[source.vendored-sources]
directory = "${output_dir}/deps/cargo"
"""
expected_config = """
[source.crates-io]
replace-with = "vendored-sources"
[source."git+https://github.com/org1/repo1.git?tag=0.1.0"]
git = "https://github.com/org1/repo1.git"
tag = "0.1.0"
replace-with = "vendored-sources"
[source."git+https://github.com/org2/repo2.git?tag=0.2.0"]
git = "https://github.com/org2/repo2.git"
tag = "0.2.0"
replace-with = "vendored-sources"
[source.vendored-sources]
directory = "${output_dir}/deps/cargo"
"""
expected = tomlkit.parse(expected_config)
# Make sure the order of the project files does not matter.
for variation in ((config1, config2), (config2, config1)):
pfs = [
ProjectFile(abspath=Path("/does/not/matter"), template=template)
for template in variation
]
assert tomlkit.parse(_merge_cargo_config_files(pfs)) == expected