-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_vcpkg_ref.py
More file actions
174 lines (136 loc) · 7.2 KB
/
test_vcpkg_ref.py
File metadata and controls
174 lines (136 loc) · 7.2 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
"""Tests for vcpkg ref/branch checkout support."""
from __future__ import annotations
from pathlib import Path
from hatch_cpp.toolchains.vcpkg import (
HatchCppVcpkgConfiguration,
_read_vcpkg_ref_from_gitmodules,
)
class TestReadVcpkgRefFromGitmodules:
"""Tests for the _read_vcpkg_ref_from_gitmodules helper."""
def test_no_gitmodules_file(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None
def test_gitmodules_without_vcpkg_submodule(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
(tmp_path / ".gitmodules").write_text('[submodule "other"]\n\tpath = other\n\turl = https://github.com/example/other.git\n')
assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None
def test_gitmodules_vcpkg_without_branch(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
(tmp_path / ".gitmodules").write_text('[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n')
assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None
def test_gitmodules_vcpkg_with_branch(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
(tmp_path / ".gitmodules").write_text(
'[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.01.12\n'
)
assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) == "2024.01.12"
def test_gitmodules_vcpkg_with_commit_sha_branch(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
sha = "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"
(tmp_path / ".gitmodules").write_text(
f'[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = {sha}\n'
)
assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) == sha
def test_gitmodules_custom_vcpkg_root(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
(tmp_path / ".gitmodules").write_text(
'[submodule "deps/vcpkg"]\n\tpath = deps/vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.06.15\n'
)
# Default vcpkg root won't match
assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None
# Custom root matches
assert _read_vcpkg_ref_from_gitmodules(Path("deps/vcpkg")) == "2024.06.15"
def test_gitmodules_multiple_submodules(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
(tmp_path / ".gitmodules").write_text(
'[submodule "other"]\n'
"\tpath = other\n"
"\turl = https://github.com/example/other.git\n"
"\tbranch = main\n"
'[submodule "vcpkg"]\n'
"\tpath = vcpkg\n"
"\turl = https://github.com/microsoft/vcpkg.git\n"
"\tbranch = 2024.01.12\n"
)
assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) == "2024.01.12"
class TestVcpkgRefConfig:
"""Tests for vcpkg_ref configuration field."""
def test_default_vcpkg_ref_is_none(self):
cfg = HatchCppVcpkgConfiguration()
assert cfg.vcpkg_ref is None
def test_explicit_vcpkg_ref(self):
cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12")
assert cfg.vcpkg_ref == "2024.01.12"
def test_explicit_vcpkg_ref_commit_sha(self):
sha = "a1b2c3d4e5f6"
cfg = HatchCppVcpkgConfiguration(vcpkg_ref=sha)
assert cfg.vcpkg_ref == sha
class TestResolveVcpkgRef:
"""Tests for _resolve_vcpkg_ref priority logic."""
def test_explicit_ref_takes_priority_over_gitmodules(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
(tmp_path / ".gitmodules").write_text(
'[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.01.12\n'
)
cfg = HatchCppVcpkgConfiguration(vcpkg_ref="my-custom-tag")
assert cfg._resolve_vcpkg_ref() == "my-custom-tag"
def test_falls_back_to_gitmodules(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
(tmp_path / ".gitmodules").write_text(
'[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.01.12\n'
)
cfg = HatchCppVcpkgConfiguration()
assert cfg._resolve_vcpkg_ref() == "2024.01.12"
def test_returns_none_when_no_ref(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
cfg = HatchCppVcpkgConfiguration()
assert cfg._resolve_vcpkg_ref() is None
class TestVcpkgGenerate:
"""Tests that generate() includes the checkout command when a ref is set."""
def _make_vcpkg_env(self, tmp_path):
"""Create a minimal vcpkg.json so generate() produces commands."""
(tmp_path / "vcpkg.json").write_text("{}")
def test_generate_with_explicit_ref(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
self._make_vcpkg_env(tmp_path)
cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12")
commands = cfg.generate(None)
assert any("git clone" in cmd for cmd in commands)
assert any("git -C vcpkg checkout 2024.01.12" in cmd for cmd in commands)
# checkout must come after clone but before bootstrap
clone_idx = next(i for i, c in enumerate(commands) if "git clone" in c)
checkout_idx = next(i for i, c in enumerate(commands) if "checkout" in c)
bootstrap_idx = next(i for i, c in enumerate(commands) if "bootstrap" in c)
assert clone_idx < checkout_idx < bootstrap_idx
def test_generate_with_gitmodules_ref(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
self._make_vcpkg_env(tmp_path)
(tmp_path / ".gitmodules").write_text(
'[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.06.15\n'
)
cfg = HatchCppVcpkgConfiguration()
commands = cfg.generate(None)
assert any("git -C vcpkg checkout 2024.06.15" in cmd for cmd in commands)
def test_generate_without_ref(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
self._make_vcpkg_env(tmp_path)
cfg = HatchCppVcpkgConfiguration()
commands = cfg.generate(None)
assert not any("checkout" in cmd for cmd in commands)
assert any("git clone" in cmd for cmd in commands)
def test_generate_skips_clone_when_vcpkg_root_exists(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
self._make_vcpkg_env(tmp_path)
(tmp_path / "vcpkg").mkdir()
cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12")
commands = cfg.generate(None)
# When vcpkg_root already exists, no clone or checkout happens
assert not any("git clone" in cmd for cmd in commands)
assert not any("checkout" in cmd for cmd in commands)
assert any("vcpkg" in cmd and "install" in cmd for cmd in commands)
def test_generate_no_vcpkg_json(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
# No vcpkg.json => no commands at all
cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12")
commands = cfg.generate(None)
assert commands == []