Repository navigation
Expand file tree
/
Copy pathsetup_test.py
More file actions
270 lines (238 loc) · 11 KB
/
Copy pathsetup_test.py
File metadata and controls
270 lines (238 loc) · 11 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
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
# Copyright 2026 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
from __future__ import annotations
import re
import tempfile
import unittest
import venv
from pathlib import Path
from unittest import mock
from build_tools.devtools.command_plan import CommandStep, WriteFileStep
from build_tools.devtools.environment import REPO_ROOT, ToolEnvironment, ToolMode
from build_tools.devtools.setup import (
common_setup_plan,
setup_plan,
setup_python_command,
)
def locked_versions(path: Path) -> dict[str, str]:
versions = {}
for line in path.read_text().splitlines():
match = re.match(r"^([A-Za-z0-9_.-]+)==([^ ]+)", line)
if match:
normalized_name = re.sub(r"[-_.]+", "-", match.group(1).lower())
versions[normalized_name] = match.group(2)
return versions
class SetupPlanTest(unittest.TestCase):
def test_setup_reuses_the_populated_environment_interpreter(self):
with tempfile.TemporaryDirectory() as temporary_directory:
tool_env = ToolEnvironment(
ToolMode.VENV, Path(temporary_directory) / "venv"
)
venv.EnvBuilder(with_pip=False).create(tool_env.root)
plan = common_setup_plan(tool_env)
self.assertFalse(any("venv" in step.argv for step in plan.steps))
def test_setup_rejects_populated_directory_without_an_interpreter(self):
with tempfile.TemporaryDirectory() as temporary_directory:
root = Path(temporary_directory)
(root / "retained.txt").write_text("caller data")
with self.assertRaisesRegex(ValueError, "no usable interpreter"):
common_setup_plan(ToolEnvironment(ToolMode.TOOL_ROOT, root))
self.assertEqual((root / "retained.txt").read_text(), "caller data")
self.assertEqual([path.name for path in root.iterdir()], ["retained.txt"])
def test_setup_selects_exact_managed_python_for_new_environment(self):
with tempfile.TemporaryDirectory() as temporary_directory:
tool_env = ToolEnvironment(
ToolMode.VENV, Path(temporary_directory) / "venv"
)
with mock.patch(
"build_tools.devtools.setup.resolve_python_interpreter",
return_value=("/tools/python3.12",),
) as resolve_python:
command = setup_python_command(tool_env)
self.assertEqual(command, ("/tools/python3.12",))
resolve_python.assert_called_once_with(
"3.12",
tool_env,
)
def test_setup_rejects_populated_environment_without_usable_python(self):
with tempfile.TemporaryDirectory() as temporary_directory:
venv_root = Path(temporary_directory) / "venv"
venv_root.mkdir()
(venv_root / "pyvenv.cfg").write_text("broken\n")
tool_env = ToolEnvironment(ToolMode.VENV, venv_root)
with (
mock.patch(
"build_tools.devtools.setup.interpreter_version",
return_value=None,
),
self.assertRaisesRegex(ValueError, "has no usable interpreter"),
):
setup_python_command(tool_env)
def test_system_mode_does_not_create_venv_or_aliases_by_default(self):
plan = setup_plan("bazel", ToolEnvironment(ToolMode.SYSTEM, None), None)
commands = [step for step in plan.steps if isinstance(step, CommandStep)]
self.assertFalse(
any("-m" in step.argv and "venv" in step.argv for step in commands)
)
self.assertFalse(any(isinstance(step, WriteFileStep) for step in plan.steps))
def test_system_mode_can_write_explicit_alias_dir(self):
with tempfile.TemporaryDirectory() as temporary_directory:
alias_dir = Path(temporary_directory) / "aliases"
plan = setup_plan(
"bazel", ToolEnvironment(ToolMode.SYSTEM, None), alias_dir
)
self.assertTrue(any(isinstance(step, WriteFileStep) for step in plan.steps))
self.assertIn(str(alias_dir), plan.describe())
def test_venv_mode_schedules_python_tool_install(self):
with tempfile.TemporaryDirectory() as temporary_directory:
venv_root = Path(temporary_directory) / "venv"
# A linked checkout has a .git file rather than a directory.
(Path(temporary_directory) / ".git").write_text("gitdir: ../repo.git")
with mock.patch(
"build_tools.devtools.setup.REPO_ROOT", Path(temporary_directory)
):
plan = setup_plan(
"bazel",
ToolEnvironment(ToolMode.VENV, venv_root),
None,
platform_name="linux",
)
commands = [step for step in plan.steps if isinstance(step, CommandStep)]
self.assertTrue(any("-m venv" in step.describe() for step in commands))
self.assertTrue(
any("requirements-dev.lock.txt" in step.describe() for step in commands)
)
self.assertTrue(
any(
"requirements-analysis.lock.txt" in step.describe()
for step in commands
)
)
self.assertTrue(
any("--only-binary=:all:" in step.describe() for step in commands)
)
semgrep_probe = next(
step for step in plan.steps if step.label == "check semgrep"
)
self.assertIn("--disable-version-check", semgrep_probe.argv)
self.assertEqual(semgrep_probe.expected_pattern, r"\b1\.96\.0\b")
self.assertTrue(
any("--group bazel" in step.describe() for step in commands)
)
install_index = next(
index
for index, step in enumerate(commands)
if any(
argument.endswith("requirements-dev.lock.txt")
for argument in step.argv
)
)
probe_index, probe = next(
(index, step)
for index, step in enumerate(commands)
if step.label == "check Lefthook CLI compatibility"
)
self.assertLess(install_index, probe_index)
self.assertIn("--file", probe.argv)
self.assertNotIn("--files", probe.argv)
self.assertEqual(probe.argv[probe.argv.index("--file") + 1], "dev.py")
self.assertIn("__iree_cli_compatibility_probe__", probe.argv)
self.assertIn("--no-fail-on-changes", probe.argv)
def test_windows_venv_does_not_install_unusable_semgrep_package(self):
with tempfile.TemporaryDirectory() as temporary_directory:
venv_root = Path(temporary_directory) / "venv"
plan = setup_plan(
"bazel",
ToolEnvironment(ToolMode.VENV, venv_root),
None,
platform_name="win32",
)
description = plan.describe()
self.assertIn("requirements-dev.lock.txt", description)
self.assertNotIn("requirements-analysis.lock.txt", description)
def test_source_archive_installs_tools_without_git_hook_probe(self):
with tempfile.TemporaryDirectory() as temporary_directory:
source_root = Path(temporary_directory)
with mock.patch("build_tools.devtools.setup.REPO_ROOT", source_root):
plan = setup_plan(
"bazel",
ToolEnvironment(ToolMode.VENV, source_root / "venv"),
None,
)
commands = [step for step in plan.steps if isinstance(step, CommandStep)]
self.assertTrue(
any("requirements-dev.lock.txt" in step.describe() for step in commands)
)
self.assertTrue(
any("--group bazel" in step.describe() for step in commands)
)
self.assertFalse(
any(
step.label == "check Lefthook CLI compatibility"
for step in commands
)
)
def test_optional_docs_setup_uses_locked_python_and_standalone_tools(self):
with tempfile.TemporaryDirectory() as temporary_directory:
venv_root = Path(temporary_directory) / "venv"
plan = common_setup_plan(
ToolEnvironment(ToolMode.VENV, venv_root),
include_docs=True,
platform_name="linux",
)
description = plan.describe()
docs_requirements = REPO_ROOT / "loom" / "docs" / "requirements.lock.txt"
self.assertIn(str(docs_requirements), description)
self.assertEqual(description.count("--group docs"), 2)
self.assertIn("--check", description)
python_requirements_step = next(
step
for step in plan.steps
if isinstance(step, CommandStep)
and any(
argument.endswith("requirements-analysis.lock.txt")
for argument in step.argv
)
)
self.assertTrue(
any(
argument == str(docs_requirements)
for argument in python_requirements_step.argv
)
)
def test_windows_optional_docs_setup_skips_analysis_environment(self):
with tempfile.TemporaryDirectory() as temporary_directory:
venv_root = Path(temporary_directory) / "venv"
plan = common_setup_plan(
ToolEnvironment(ToolMode.VENV, venv_root),
include_docs=True,
platform_name="win32",
)
description = plan.describe()
docs_requirements = REPO_ROOT / "loom" / "docs" / "requirements.lock.txt"
self.assertIn(str(docs_requirements), description)
self.assertNotIn("requirements-analysis.lock.txt", description)
self.assertIn("--group docs", description)
def test_optional_docs_setup_rejects_system_toolchain(self):
with self.assertRaisesRegex(
ValueError, "documentation setup requires a managed tool environment"
):
common_setup_plan(ToolEnvironment(ToolMode.SYSTEM, None), include_docs=True)
def test_optional_docs_lock_matches_shared_analysis_packages(self):
analysis_versions = locked_versions(
REPO_ROOT / "requirements-analysis.lock.txt"
)
docs_versions = locked_versions(
REPO_ROOT / "loom" / "docs" / "requirements.lock.txt"
)
shared_names = analysis_versions.keys() & docs_versions.keys()
self.assertTrue(shared_names)
self.assertEqual(
{name: analysis_versions[name] for name in shared_names},
{name: docs_versions[name] for name in shared_names},
)
if __name__ == "__main__":
unittest.main()