-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_detection.py
More file actions
executable file
·186 lines (146 loc) · 6.48 KB
/
test_detection.py
File metadata and controls
executable file
·186 lines (146 loc) · 6.48 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
#!/usr/bin/env python3
"""
Comprehensive test script for uvhow detection logic.
Can be run locally or in CI to verify detection accuracy.
"""
import sys
import unittest.mock
from pathlib import Path
# Add src to path to import uvhow
sys.path.insert(0, 'src')
try:
from uvhow import detect_uv_installation, is_uv_installed_via_pip
except ImportError:
print("❌ Could not import uvhow. Make sure you're running from the project root.")
sys.exit(1)
def test_detection_logic():
"""Test the detection logic with various path and pip combinations."""
test_cases = [
# (path, is_pip_installed, expected_method)
('/opt/homebrew/Cellar/uv/0.7.21/bin/uv', False, 'Homebrew'),
('/opt/homebrew/bin/uv', False, 'Homebrew'),
('/usr/local/Cellar/uv/0.7.21/bin/uv', False, 'Homebrew'),
('/home/user/.local/bin/uv', True, 'pip (user)'),
('/home/user/.local/bin/uv', False, 'Standalone installer'),
('/Users/user/.local/bin/uv', True, 'pip (user)'),
('/Users/user/.local/bin/uv', False, 'Standalone installer'),
('/usr/local/bin/uv', True, 'pip (system)'),
('/usr/bin/uv', True, 'pip (system)'),
('/usr/local/bin/uv', False, 'Unknown'),
('/home/user/.cargo/bin/uv', False, 'Cargo'),
('/Users/user/.cargo/bin/uv', False, 'Cargo'),
('/home/user/.local/share/pipx/venvs/uv/bin/uv', False, 'pipx'),
('/Users/user/.local/share/pipx/venvs/uv/bin/uv', False, 'pipx'),
('/opt/pipx/venvs/uv/bin/uv', False, 'pipx'),
('/project/.venv/bin/uv', False, 'pip (virtual environment)'),
('/home/user/project/venv/bin/uv', False, 'pip (virtual environment)'),
('/workspace/.env/bin/uv', False, 'pip (virtual environment)'),
('/opt/local/bin/uv', True, 'pip (user)'),
('/some/random/path/bin/uv', True, 'pip (user)'),
('/some/random/path/bin/uv', False, 'Unknown'),
]
print('🧪 Testing detection logic with various scenarios...')
print()
failed = 0
passed = 0
for path, mock_pip_installed, expected in test_cases:
with unittest.mock.patch('shutil.which', return_value=path):
with unittest.mock.patch('uvhow.get_uv_version', return_value='uv 0.7.21'):
with unittest.mock.patch('uvhow.is_uv_installed_via_pip', return_value=mock_pip_installed):
install = detect_uv_installation()
if install and install.method == expected:
print(f'✅ {path} (pip={mock_pip_installed}) -> {install.method}')
passed += 1
else:
actual = install.method if install else 'None'
print(f'❌ {path} (pip={mock_pip_installed}) -> {actual} (expected: {expected})')
failed += 1
print()
print(f'📊 Test Results: {passed} passed, {failed} failed')
if failed > 0:
print('❌ Some tests failed!')
return False
else:
print('✅ All detection tests passed!')
return True
def test_pip_detection():
"""Test the pip detection functionality."""
print('🔍 Testing pip detection function...')
# This will test the actual pip detection in the current environment
try:
pip_detected = is_uv_installed_via_pip()
print(f'📦 Current environment - uv installed via pip: {pip_detected}')
# Try to determine if this result makes sense by checking if uv is available
import shutil
uv_path = shutil.which('uv')
if uv_path:
print(f'📍 UV found at: {uv_path}')
else:
print('📍 UV not found in PATH')
return True
except Exception as e:
print(f'❌ Error testing pip detection: {e}')
return False
def test_edge_cases():
"""Test edge cases and error conditions."""
print('🔬 Testing edge cases...')
test_cases = [
# Edge cases that should return Unknown or specific behaviors
('/some/weird/path/uv', False, 'Unknown'), # Not ending in /bin/uv
('/home/user/project/venv/somewhere/uv', False, 'Unknown'), # venv but not in bin
]
failed = 0
passed = 0
for path, mock_pip_installed, expected in test_cases:
with unittest.mock.patch('shutil.which', return_value=path):
with unittest.mock.patch('uvhow.get_uv_version', return_value='uv 0.7.21'):
with unittest.mock.patch('uvhow.is_uv_installed_via_pip', return_value=mock_pip_installed):
install = detect_uv_installation()
if install and install.method == expected:
print(f'✅ {path} -> {install.method}')
passed += 1
else:
actual = install.method if install else 'None'
print(f'❌ {path} -> {actual} (expected: {expected})')
failed += 1
# Test when uv is not found
print('Testing when uv is not installed...')
with unittest.mock.patch('shutil.which', return_value=None):
install = detect_uv_installation()
if install is None:
print('✅ Correctly returns None when uv not found')
passed += 1
else:
print('❌ Should return None when uv not found')
failed += 1
print(f'📊 Edge case results: {passed} passed, {failed} failed')
return failed == 0
def main():
"""Run all tests."""
print('🚀 Starting uvhow detection tests...')
print('=' * 50)
tests = [
('Detection Logic', test_detection_logic),
('Pip Detection', test_pip_detection),
('Edge Cases', test_edge_cases),
]
all_passed = True
for test_name, test_func in tests:
print(f'\\n📋 Running {test_name} tests...')
print('-' * 30)
try:
result = test_func()
if not result:
all_passed = False
except Exception as e:
print(f'❌ {test_name} test failed with exception: {e}')
all_passed = False
print('\\n' + '=' * 50)
if all_passed:
print('🎉 All tests passed! uvhow detection is working correctly.')
sys.exit(0)
else:
print('💥 Some tests failed. Please check the detection logic.')
sys.exit(1)
if __name__ == '__main__':
main()