FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hatch-cpp/hatch_cpp/tests/test_vcpkg_ref.py at main · python-project-templates/hatch-cpp · GitHub
python-project-templates
hatch-cpp
Repository navigation
Code
Issues
4
(4)
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
hatch-cpp
/
hatch_cpp
/
tests
/
test_vcpkg_ref.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
277 lines (216 loc) · 11.8 KB
Breadcrumbs
hatch-cpp
/
hatch_cpp
/
tests
/
test_vcpkg_ref.py
Copy path
File metadata and controls
277 lines (216 loc) · 11.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""Tests for vcpkg ref/branch checkout support."""
from
__future__
import
annotations
from
pathlib
import
Path
from
types
import
SimpleNamespace
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
\t
path = other
\n
\t
url = 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
\t
path = vcpkg
\n
\t
url = 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
\t
path = vcpkg
\n
\t
url = https://github.com/microsoft/vcpkg.git
\n
\t
branch = 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
\t
path = vcpkg
\n
\t
url = https://github.com/microsoft/vcpkg.git
\n
\t
branch =
{
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
\t
path = deps/vcpkg
\n
\t
url = https://github.com/microsoft/vcpkg.git
\n
\t
branch = 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
'
"
\t
path = other
\n
"
"
\t
url = https://github.com/example/other.git
\n
"
"
\t
branch = main
\n
"
'[submodule "vcpkg"]
\n
'
"
\t
path = vcpkg
\n
"
"
\t
url = https://github.com/microsoft/vcpkg.git
\n
"
"
\t
branch = 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
def
test_linux_arm64_triplet
(
self
):
cfg
=
HatchCppVcpkgConfiguration
(
vcpkg_triplet
=
"arm64-linux"
)
assert
cfg
.
vcpkg_triplet
==
"arm64-linux"
def
test_emscripten_triplet
(
self
):
cfg
=
HatchCppVcpkgConfiguration
(
vcpkg_triplet
=
"wasm32-emscripten"
)
assert
cfg
.
vcpkg_triplet
==
"wasm32-emscripten"
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
\t
path = vcpkg
\n
\t
url = https://github.com/microsoft/vcpkg.git
\n
\t
branch = 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
\t
path = vcpkg
\n
\t
url = https://github.com/microsoft/vcpkg.git
\n
\t
branch = 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
\t
path = vcpkg
\n
\t
url = https://github.com/microsoft/vcpkg.git
\n
\t
branch = 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_uses_posix_command_paths
(
self
,
tmp_path
,
monkeypatch
):
monkeypatch
.
chdir
(
tmp_path
)
self
.
_make_vcpkg_env
(
tmp_path
)
cfg
=
HatchCppVcpkgConfiguration
(
vcpkg_triplet
=
"x64-linux"
)
commands
=
cfg
.
generate
(
None
)
assert
"./vcpkg/bootstrap-vcpkg.sh"
in
commands
assert
"./vcpkg/vcpkg install --triplet x64-linux"
in
commands
def
test_generate_detects_linux_arm64_triplet
(
self
,
tmp_path
,
monkeypatch
):
monkeypatch
.
chdir
(
tmp_path
)
monkeypatch
.
setattr
(
"hatch_cpp.toolchains.vcpkg.sys_platform"
,
"linux"
)
monkeypatch
.
setattr
(
"hatch_cpp.toolchains.vcpkg.platform_machine"
,
lambda
:
"aarch64"
)
self
.
_make_vcpkg_env
(
tmp_path
)
cfg
=
HatchCppVcpkgConfiguration
()
commands
=
cfg
.
generate
(
None
)
assert
"./vcpkg/vcpkg install --triplet arm64-linux"
in
commands
def
test_generate_detects_emscripten_triplet
(
self
,
tmp_path
,
monkeypatch
):
monkeypatch
.
chdir
(
tmp_path
)
self
.
_make_vcpkg_env
(
tmp_path
)
build_plan
=
SimpleNamespace
(
platform
=
SimpleNamespace
(
platform
=
"emscripten"
))
cfg
=
HatchCppVcpkgConfiguration
()
commands
=
cfg
.
generate
(
build_plan
)
assert
"./vcpkg/vcpkg install --triplet wasm32-emscripten"
in
commands
def
test_generate_uses_windows_command_paths
(
self
,
tmp_path
,
monkeypatch
):
monkeypatch
.
chdir
(
tmp_path
)
monkeypatch
.
setattr
(
"hatch_cpp.toolchains.vcpkg.sys_platform"
,
"win32"
)
self
.
_make_vcpkg_env
(
tmp_path
)
cfg
=
HatchCppVcpkgConfiguration
(
vcpkg_triplet
=
"x64-windows-static-md"
)
commands
=
cfg
.
generate
(
None
)
assert
r"vcpkg\bootstrap-vcpkg.bat"
in
commands
assert
r"vcpkg\vcpkg.exe install --triplet x64-windows-static-md"
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
)
vcpkg_root
=
tmp_path
/
"vcpkg"
vcpkg_root
.
mkdir
()
# Existing bootstrap script and executable mean no clone/bootstrap is needed.
(
vcpkg_root
/
"bootstrap-vcpkg.sh"
).
write_text
(
"#!/bin/sh
\n
exit 0
\n
"
)
(
vcpkg_root
/
"vcpkg"
).
write_text
(
"#!/bin/sh
\n
exit 0
\n
"
)
cfg
=
HatchCppVcpkgConfiguration
(
vcpkg_ref
=
"2024.01.12"
)
monkeypatch
.
setattr
(
cfg
,
"_is_vcpkg_working"
,
lambda
:
True
)
commands
=
cfg
.
generate
(
None
)
# When vcpkg_root already exists with a working executable, no clone or bootstrap happens.
assert
not
any
(
"git clone"
in
cmd
for
cmd
in
commands
)
assert
not
any
(
"checkout"
in
cmd
for
cmd
in
commands
)
assert
not
any
(
"bootstrap-vcpkg"
in
cmd
for
cmd
in
commands
)
assert
any
(
"vcpkg"
in
cmd
and
"install"
in
cmd
for
cmd
in
commands
)
def
test_generate_reclones_when_vcpkg_root_exists_but_empty
(
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
)
assert
any
(
cmd
.
startswith
(
'rm -rf "vcpkg"'
)
for
cmd
in
commands
)
assert
any
(
"git clone"
in
cmd
for
cmd
in
commands
)
assert
any
(
"checkout 2024.01.12"
in
cmd
for
cmd
in
commands
)
assert
any
(
"bootstrap-vcpkg"
in
cmd
for
cmd
in
commands
)
assert
any
(
"vcpkg"
in
cmd
and
"install"
in
cmd
for
cmd
in
commands
)
def
test_generate_bootstraps_when_vcpkg_executable_missing
(
self
,
tmp_path
,
monkeypatch
):
monkeypatch
.
chdir
(
tmp_path
)
self
.
_make_vcpkg_env
(
tmp_path
)
vcpkg_root
=
tmp_path
/
"vcpkg"
vcpkg_root
.
mkdir
()
(
vcpkg_root
/
"bootstrap-vcpkg.sh"
).
write_text
(
"#!/bin/sh
\n
exit 0
\n
"
)
cfg
=
HatchCppVcpkgConfiguration
(
vcpkg_ref
=
"2024.01.12"
)
commands
=
cfg
.
generate
(
None
)
assert
not
any
(
"git clone"
in
cmd
for
cmd
in
commands
)
assert
any
(
"bootstrap-vcpkg"
in
cmd
for
cmd
in
commands
)
assert
any
(
"vcpkg"
in
cmd
and
"install"
in
cmd
for
cmd
in
commands
)
def
test_generate_reclones_when_vcpkg_exists_but_not_working
(
self
,
tmp_path
,
monkeypatch
):
monkeypatch
.
chdir
(
tmp_path
)
self
.
_make_vcpkg_env
(
tmp_path
)
vcpkg_root
=
tmp_path
/
"vcpkg"
vcpkg_root
.
mkdir
()
(
vcpkg_root
/
"bootstrap-vcpkg.sh"
).
write_text
(
"#!/bin/sh
\n
exit 0
\n
"
)
(
vcpkg_root
/
"vcpkg"
).
write_text
(
"#!/bin/sh
\n
exit 1
\n
"
)
cfg
=
HatchCppVcpkgConfiguration
(
vcpkg_ref
=
"2024.01.12"
)
monkeypatch
.
setattr
(
cfg
,
"_is_vcpkg_working"
,
lambda
:
False
)
commands
=
cfg
.
generate
(
None
)
assert
any
(
cmd
.
startswith
(
'rm -rf "vcpkg"'
)
for
cmd
in
commands
)
assert
any
(
"git clone"
in
cmd
for
cmd
in
commands
)
assert
any
(
"checkout 2024.01.12"
in
cmd
for
cmd
in
commands
)
assert
any
(
"bootstrap-vcpkg"
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
==
[]
Back
|
FazBrowse Home
|
New Git URL