forked from rapidsai/rapids-build-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_impls.py
306 lines (288 loc) · 9.7 KB
/
test_impls.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Copyright (c) 2024, NVIDIA CORPORATION.
import os.path
from contextlib import contextmanager
from textwrap import dedent
from unittest.mock import Mock, patch
import pytest
from rapids_build_backend.impls import (
_edit_pyproject,
_get_cuda_suffix,
_write_git_commits,
)
@contextmanager
def set_cwd(cwd):
old_cwd = os.getcwd()
os.chdir(cwd)
try:
yield
finally:
os.chdir(old_cwd)
@pytest.mark.parametrize(
("project_name", "directories", "commit_files_config", "expected_commit_files"),
[
("test-project", ["test_project"], None, ["test_project/GIT_COMMIT"]),
(
"test-project",
["_test_project"],
["_test_project/GIT_COMMIT"],
["_test_project/GIT_COMMIT"],
),
(
"test-project",
["_test_project_1", "_test_project_2"],
["_test_project_1/GIT_COMMIT", "_test_project_2/GIT_COMMIT"],
["_test_project_1/GIT_COMMIT", "_test_project_2/GIT_COMMIT"],
),
(
"test-project",
[],
[],
[],
),
],
)
@patch("rapids_build_backend.impls._get_git_commit", Mock(return_value="abc123"))
def test_write_git_commits(
tmp_path, project_name, directories, commit_files_config, expected_commit_files
):
with set_cwd(tmp_path):
for directory in directories:
os.mkdir(directory)
config = Mock(
commit_files=commit_files_config,
)
with _write_git_commits(config, project_name):
for expected_commit_file in expected_commit_files:
with open(expected_commit_file) as f:
assert f.read() == "abc123\n"
if not directories:
assert list(os.walk(".")) == [(".", [], [])]
for directory in directories:
os.rmdir(directory)
assert list(os.walk(".")) == [(".", [], [])]
@pytest.mark.parametrize(
[
"pyproject_dir",
"dependencies_file",
"write_dependencies_file",
"disable_cuda",
"cuda_version",
"cuda_suffix",
"cuda_python_requirement",
"matrix",
"arch_requirement",
],
[
(
".",
"dependencies.yaml",
True,
False,
("11", "5"),
"-cu11",
"cuda-python>=11.5,<11.6.dev0",
"",
"some-x86-package",
),
(
".",
"dependencies.yaml",
True,
False,
("11", "5"),
"-cu11",
"cuda-python>=11.5,<11.6.dev0",
"arch=aarch64",
"some-arm-package",
),
(
"python",
"../dependencies.yaml",
True,
False,
("12", "1"),
"-cu12",
"cuda-python>=12.1,<12.2.dev0",
"",
"some-x86-package",
),
(
".",
"dependencies.yaml",
False,
False,
("11", "5"),
"-cu11",
None,
"",
None,
),
(
".",
"dependencies.yaml",
True,
True,
None, # Ensure _get_cuda_version() isn't called and unpacked
"",
"cuda-python",
"",
"some-x86-package",
),
],
)
def test_edit_pyproject(
tmp_path,
pyproject_dir,
dependencies_file,
write_dependencies_file,
disable_cuda,
cuda_version,
cuda_suffix,
cuda_python_requirement,
matrix,
arch_requirement,
):
original_contents = dedent(
"""\
[project]
name = "test-project"
dependencies = []
[build-system]
requires = []
"""
)
full_pyproject_dir = os.path.join(tmp_path, pyproject_dir)
if not os.path.exists(full_pyproject_dir):
os.mkdir(full_pyproject_dir)
with set_cwd(full_pyproject_dir):
with open("pyproject.toml", "w") as f:
f.write(original_contents)
if write_dependencies_file:
with open(dependencies_file, "w") as f:
f.write(
dedent(
f"""\
files:
project:
output: pyproject
includes:
- project
- arch
pyproject_dir: {pyproject_dir}
matrix:
arch: ["x86_64"]
extras:
table: project
build_system:
output: pyproject
includes:
- build_system
pyproject_dir: {pyproject_dir}
extras:
table: build-system
other_project:
output: pyproject
includes:
- bad
pyproject_dir: python_bad
extras:
table: project
conda:
output: conda
includes:
- bad
dependencies:
project:
common:
- output_types: [pyproject]
packages:
- tomli
specific:
- output_types: [pyproject]
matrices:
- matrix:
cuda: "11.5"
packages:
- cuda-python>=11.5,<11.6.dev0
- matrix:
cuda: "12.1"
packages:
- cuda-python>=12.1,<12.2.dev0
- matrix:
packages:
- cuda-python
build_system:
common:
- output_types: [pyproject]
packages:
- scikit-build-core
arch:
specific:
- output_types: [pyproject]
matrices:
- matrix:
arch: x86_64
packages:
- some-x86-package
- matrix:
arch: aarch64
packages:
- some-arm-package
bad:
common:
- output_types: [pyproject, conda]
packages:
- bad-package
"""
)
)
config = Mock(
disable_cuda=disable_cuda,
dependencies_file=dependencies_file,
matrix_entry=matrix,
)
with patch(
"rapids_build_backend.impls._get_cuda_version",
Mock(return_value=cuda_version),
), patch(
"rapids_build_backend.impls._get_cuda_suffix",
_get_cuda_suffix.__wrapped__,
):
with _edit_pyproject(config):
with open("pyproject.toml") as f:
if write_dependencies_file:
assert f.read() == dedent(
f"""\
[project]
name = "test-project{cuda_suffix}"
dependencies = [
"{cuda_python_requirement}",
"{arch_requirement}",
"tomli",
] # This list was generated by """
"""`rapids-dependency-file-generator`. To make """
f"""changes, edit {dependencies_file} and run """
"""`rapids-dependency-file-generator`.
[build-system]
requires = [
"scikit-build-core",
] # This list was generated by """
"""`rapids-dependency-file-generator`. To make """
f"""changes, edit {dependencies_file} and run """
"""`rapids-dependency-file-generator`.
"""
)
else:
assert f.read() == dedent(
f"""\
[project]
name = "test-project{cuda_suffix}"
dependencies = []
[build-system]
requires = []
"""
)
with open(".pyproject.toml.rapids-build-backend.bak") as f:
assert f.read() == original_contents
with open("pyproject.toml") as f:
assert f.read() == original_contents