-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathsetup_venv_test.py
More file actions
executable file
·192 lines (159 loc) · 6.74 KB
/
setup_venv_test.py
File metadata and controls
executable file
·192 lines (159 loc) · 6.74 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
# Copyright Advanced Micro Devices, Inc.
# SPDX-License-Identifier: MIT
from pathlib import Path
import shutil
import sys
import tempfile
import unittest
from unittest.mock import patch
import os
import re
sys.path.insert(0, os.fspath(Path(__file__).parent.parent))
from setup_venv import (
GFX_TARGET_REGEX,
install_packages_into_venv,
)
class InstallPackagesTest(unittest.TestCase):
"""Tests for install_packages_into_venv() command generation."""
def setUp(self):
self.venv_dir = Path(tempfile.mkdtemp())
def tearDown(self):
shutil.rmtree(self.venv_dir, ignore_errors=True)
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_basic_pip_usage(self, mock_run, mock_find_python):
"""The most basic usage should run `python -m pip install [packages]`"""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["rocm"],
)
cmd = mock_run.call_args[0][0]
self.assertEqual(cmd[0], "python")
self.assertEqual(cmd[1], "-m")
self.assertEqual(cmd[2], "pip")
self.assertEqual(cmd[3], "install")
self.assertIn("rocm", cmd)
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_basic_uv_usage(self, mock_run, mock_find_python):
"""Using uv generates a different command structure."""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["rocm"],
use_uv=True,
)
cmd = mock_run.call_args[0][0]
self.assertEqual(cmd[0], "uv")
self.assertEqual(cmd[1], "pip")
self.assertEqual(cmd[2], "install")
self.assertEqual(cmd[3], "--python")
self.assertIn("rocm", cmd)
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_multiple_packages(self, mock_run, mock_find_python):
"""Multiple packages can be installed at once."""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["torch", "torchaudio"],
)
cmd = mock_run.call_args[0][0]
self.assertIn("torch", cmd)
self.assertIn("torchaudio", cmd)
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_pre_flag_pip(self, mock_run, mock_find_python):
"""--pre flag uses pip syntax."""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["rocm"],
pre=True,
)
cmd = mock_run.call_args[0][0]
self.assertIn("--pre", cmd)
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_pre_flag_uv(self, mock_run, mock_find_python):
"""--pre flag uses uv syntax when use_uv=True."""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["rocm"],
use_uv=True,
pre=True,
)
cmd = mock_run.call_args[0][0]
self.assertIn("--prerelease=allow", cmd)
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_index_url_complete(self, mock_run, mock_find_python):
"""Passing index_url without index_subdir uses the URL as-is."""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["rocm"],
index_url="https://example.com/full/path/",
)
cmd = mock_run.call_args[0][0]
self.assertIn("--index-url=https://example.com/full/path/", cmd)
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_index_name_with_subdir(self, mock_run, mock_find_python):
"""Passing index_name with index_subdir constructs full URL."""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["rocm"],
index_name="stable",
index_subdir="gfx110X-all",
)
cmd = mock_run.call_args[0][0]
self.assertIn("--index-url=https://repo.amd.com/rocm/whl/gfx110X-all", cmd)
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_index_url_with_subdir(self, mock_run, mock_find_python):
"""Passing index_url with index_subdir constructs full URL."""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["rocm"],
index_url="https://example.com/base",
index_subdir="gfx94X-dcgpu",
)
cmd = mock_run.call_args[0][0]
self.assertIn("--index-url=https://example.com/base/gfx94X-dcgpu", cmd)
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_find_links_only(self, mock_run, mock_find_python):
"""Passing just find_links uses it."""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["rocm"],
find_links="https://bucket/run-123/index.html",
)
cmd = mock_run.call_args[0][0]
self.assertIn("--find-links=https://bucket/run-123/index.html", cmd)
self.assertFalse(any("--index-url" in str(a) for a in cmd))
@patch("setup_venv.find_venv_python_exe", return_value="python")
@patch("setup_venv.run_command")
def test_index_url_and_find_links(self, mock_run, mock_find_python):
"""Both index_url and find_links can be used together."""
install_packages_into_venv(
venv_dir=self.venv_dir,
packages=["rocm"],
index_url="https://deps/simple/",
find_links="https://bucket/run-123/index.html",
)
cmd = mock_run.call_args[0][0]
self.assertIn("--index-url=https://deps/simple/", cmd)
self.assertIn("--find-links=https://bucket/run-123/index.html", cmd)
class GfxRegexPatternTest(unittest.TestCase):
def test_valid_match(self):
html_snippet = '<a href="relpath/to/wherever/gfx103X-all">gfx103X-all</a><br><a href="/relpath/gfx120X-all">gfx120X-all</a>'
matches = re.findall(GFX_TARGET_REGEX, html_snippet)
self.assertEqual(["gfx103X-all", "gfx120X-all"], matches)
def test_match_without_suffix(self):
html_snippet = "<a>gfx940</a><br><a>gfx1030</a>"
matches = re.findall(GFX_TARGET_REGEX, html_snippet)
self.assertEqual(["gfx940", "gfx1030"], matches)
def test_invalid_match(self):
html_snippet = "<a>gfx94000</a><br><a>gfx1030X-dgpu</a>"
matches = re.findall(GFX_TARGET_REGEX, html_snippet)
self.assertEqual(matches, [])
if __name__ == "__main__":
unittest.main()