-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_uv_environment.py
More file actions
559 lines (471 loc) · 20.7 KB
/
Copy pathtest_uv_environment.py
File metadata and controls
559 lines (471 loc) · 20.7 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/usr/bin/env python3
"""
Comprehensive UV Environment Management Test Suite.
This test suite verifies:
1. UV availability and proper installation
2. Virtual environment creation and management
3. Dependency synchronization via uv sync
4. Package version tracking and validation
5. Environment health checks
6. All UV-related setup functions fully succeed with real methods
Following the project's Zero Simulated policy - all tests use real methods.
"""
import json
import logging
import shutil
import subprocess # nosec B404
import sys
from pathlib import Path
from typing import Any
import pytest
# Test markers
pytestmark: list[Any] = [pytest.mark.integration, pytest.mark.uv]
# Get project root
PROJECT_ROOT = Path(__file__).parent.parent.parent.absolute()
VENV_PATH = PROJECT_ROOT / ".venv"
VENV_PYTHON = (
VENV_PATH / "bin" / "python"
if sys.platform != "win32"
else VENV_PATH / "Scripts" / "python.exe"
)
# Resolve 'uv' binary
UV_BIN = shutil.which("uv") or str(Path.home() / ".local" / "bin" / "uv")
# Import setup module
try:
from setup import (
FEATURES,
OPTIONAL_GROUPS,
add_uv_dependency,
check_environment_health,
check_system_requirements,
check_uv_availability,
get_environment_info,
get_installed_package_versions,
get_uv_setup_info,
get_uv_status,
lock_uv_dependencies,
remove_uv_dependency,
setup_uv_environment,
update_uv_dependencies,
validate_system,
validate_uv_setup,
)
SETUP_AVAILABLE = True
except ImportError as e:
SETUP_AVAILABLE = False
IMPORT_ERROR = str(e)
logger = logging.getLogger(__name__)
class TestUVAvailability:
"""Test UV CLI availability and version."""
def test_uv_cli_available(self) -> Any:
"""Test that UV CLI is available in PATH."""
result = subprocess.run( # nosec B607 B603
[UV_BIN, "--version"], capture_output=True, text=True, timeout=10
)
assert result.returncode == 0, f"UV CLI not available: {result.stderr}"
assert "uv" in result.stdout.lower(), "Unexpected UV version output"
def test_check_uv_availability_function(self) -> Any:
"""Test the check_uv_availability function."""
available = check_uv_availability(verbose=False)
assert available is True, (
"check_uv_availability should return True when UV is installed"
)
def test_uv_version_compatible(self) -> Any:
"""Test that UV version is compatible (0.9.x or higher)."""
result = subprocess.run( # nosec B607 B603
[UV_BIN, "--version"], capture_output=True, text=True, timeout=10
)
version_str = result.stdout.strip()
# Extract version number
parts = version_str.split()
if len(parts) >= 2:
version = parts[1]
major_minor = version.split(".")[:2]
if len(major_minor) >= 2:
major = int(major_minor[0])
minor = int(major_minor[1])
assert major >= 0, "UV major version should be >= 0"
if major == 0:
assert minor >= 9, f"UV minor version should be >= 9, got {minor}"
class TestVirtualEnvironment:
"""Test virtual environment management."""
def test_venv_exists(self) -> Any:
"""Test that virtual environment exists."""
assert VENV_PATH.exists(), f"Virtual environment not found at {VENV_PATH}"
def test_venv_python_exists(self) -> Any:
"""Test that virtual environment Python exists."""
assert VENV_PYTHON.exists(), f"Venv Python not found at {VENV_PYTHON}"
def test_venv_python_executable(self) -> Any:
"""Test that virtual environment Python is executable."""
result = subprocess.run( # nosec B603
[str(VENV_PYTHON), "--version"], capture_output=True, text=True, timeout=10
)
assert result.returncode == 0, f"Venv Python not executable: {result.stderr}"
assert "Python" in result.stdout, "Unexpected Python version output"
def test_venv_python_version_compatible(self) -> Any:
"""Test that Python version is >= 3.11 as per pyproject.toml."""
result = subprocess.run( # nosec B603
[
str(VENV_PYTHON),
"-c",
"import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')",
],
capture_output=True,
text=True,
timeout=10,
)
assert result.returncode == 0
version = result.stdout.strip()
major, minor = map(int, version.split("."))
assert major >= 3 and minor >= 11, (
f"Python version should be >= 3.11, got {version}"
)
class TestProjectFiles:
"""Test UV project files exist and are valid."""
def test_pyproject_toml_exists(self) -> Any:
"""Test that pyproject.toml exists."""
pyproject_path = PROJECT_ROOT / "pyproject.toml"
assert pyproject_path.exists(), f"pyproject.toml not found at {pyproject_path}"
def test_uv_lock_exists(self) -> Any:
"""Test that uv.lock exists."""
lock_path = PROJECT_ROOT / "uv.lock"
assert lock_path.exists(), f"uv.lock not found at {lock_path}"
def test_pyproject_toml_valid(self) -> Any:
"""Test that pyproject.toml is valid TOML."""
import tomllib
pyproject_path = PROJECT_ROOT / "pyproject.toml"
with open(pyproject_path, "rb") as f:
config = tomllib.load(f)
assert "project" in config, "pyproject.toml missing [project] section"
assert "name" in config["project"], "pyproject.toml missing project name"
assert "version" in config["project"], "pyproject.toml missing version"
assert "dependencies" in config["project"], (
"pyproject.toml missing dependencies"
)
def test_uv_lock_not_empty(self) -> Any:
"""Test that uv.lock is not empty."""
lock_path = PROJECT_ROOT / "uv.lock"
assert lock_path.stat().st_size > 0, "uv.lock is empty"
class TestDependencyManagement:
"""Test dependency installation and management."""
def test_get_installed_package_versions(self) -> Any:
"""Test getting installed package versions."""
packages = get_installed_package_versions(verbose=False)
assert isinstance(packages, dict), (
"get_installed_package_versions should return a dict"
)
assert len(packages) > 0, "Should have installed packages"
# Check for core packages (pytest is dev-extra; guarded above)
core_packages: list[Any] = ["numpy", "pytest", "matplotlib", "scipy"]
for pkg in core_packages:
assert pkg in packages, f"Core package {pkg} not found"
def test_uv_tree_works(self) -> Any:
"""``uv tree`` is the modern replacement for ``uv pip list``.
The ``uv pip`` interface is rejected by newer uv releases and
by toolchain shims (e.g. trailofbits modern-python). ``uv tree``
prints the project's dependency tree and is the facade-compatible
way to verify uv is functional against the current project.
"""
result = subprocess.run( # nosec B607 B603
[UV_BIN, "tree", "--depth", "1"],
capture_output=True,
text=True,
cwd=str(PROJECT_ROOT),
timeout=30,
)
assert result.returncode == 0, f"uv tree failed: {result.stderr}"
# Output is human-readable; just check it's non-empty.
assert result.stdout.strip(), "uv tree produced no output"
def test_uv_sync_check(self) -> Any:
"""Test that uv sync --dry-run reports no changes needed for dev tests."""
result = subprocess.run( # nosec B607 B603
[UV_BIN, "sync", "--frozen", "--check", "--extra", "dev"],
capture_output=True,
text=True,
cwd=str(PROJECT_ROOT),
timeout=60,
)
# If returncode is 0, environment is in sync
# If returncode is 1, there are pending changes
# Both are acceptable - we just want the command to work
assert result.returncode in [0, 1], (
f"uv sync --check failed unexpectedly: {result.stderr}"
)
class TestSystemRequirements:
"""Test system requirements validation."""
def test_check_system_requirements(self) -> Any:
"""Test system requirements check function."""
result = check_system_requirements(verbose=False)
assert result is True, "System requirements should pass"
def test_validate_system_function(self) -> Any:
"""Test validate_system function from validator."""
result = validate_system()
assert isinstance(result, dict), "validate_system should return a dict"
assert "success" in result, "Result should have 'success' key"
assert result["success"] is True, (
f"validate_system failed: {result.get('error', 'unknown')}"
)
class TestUVSetupValidation:
"""Test UV setup validation functions."""
def test_validate_uv_setup(self) -> Any:
"""Test validate_uv_setup function."""
result = validate_uv_setup()
assert isinstance(result, dict), "validate_uv_setup should return a dict"
# Check expected keys
expected_keys: list[Any] = [
"system_requirements",
"uv_environment",
"dependencies",
"jax_installation",
"jax_stack_functional",
"overall_status",
"python_version",
]
for key in expected_keys:
assert key in result, f"Result missing key: {key}"
# Verify all checks pass
assert result["system_requirements"] is True, (
"System requirements check should pass"
)
assert result["uv_environment"] is True, "UV environment check should pass"
assert result["dependencies"] is True, "Dependencies check should pass"
assert result["jax_stack_functional"] is True, (
"JAX + pymdp stack must be functional (core deps)"
)
assert result["jax_installation"] is True, (
"JAX installation should be reported OK"
)
assert result["overall_status"] is True, "Overall status should be True"
def test_get_uv_setup_info(self) -> Any:
"""Test get_uv_setup_info function."""
info = get_uv_setup_info()
assert isinstance(info, dict), "get_uv_setup_info should return a dict"
# Check expected keys
expected_keys: list[Any] = [
"project_root",
"uv_environment_path",
"python_version",
"platform",
"uv_setup_status",
]
for key in expected_keys:
assert key in info, f"Info missing key: {key}"
# Verify values
assert Path(info["project_root"]) == PROJECT_ROOT, "Project root mismatch"
assert Path(info["uv_environment_path"]) == VENV_PATH, "Venv path mismatch"
def test_get_uv_status(self) -> Any:
"""Test get_uv_status function."""
result = get_uv_status()
assert isinstance(result, dict), "get_uv_status should return a dict"
assert "uv_available" in result, "Result should have 'uv_available' key"
assert result["uv_available"] is True, "UV should be available"
assert result["status"] == "healthy", "Status should be 'healthy'"
class TestEnvironmentHealth:
"""Test environment health check functionality."""
def test_check_environment_health(self) -> Any:
"""Test comprehensive environment health check."""
health = check_environment_health(verbose=False)
assert isinstance(health, dict), "check_environment_health should return a dict"
# Check expected keys
expected_keys: list[Any] = [
"overall_healthy",
"uv_available",
"uv_version",
"venv_exists",
"venv_python_works",
"lock_file_exists",
"pyproject_exists",
"core_packages",
"optional_packages",
"issues",
"suggestions",
]
for key in expected_keys:
assert key in health, f"Health check missing key: {key}"
# Verify critical checks pass
assert health["overall_healthy"] is True, (
f"Environment not healthy. Issues: {health.get('issues', [])}"
)
assert health["uv_available"] is True, "UV should be available"
assert health["venv_exists"] is True, "Venv should exist"
assert health["venv_python_works"] is True, "Venv Python should work"
assert health["lock_file_exists"] is True, "Lock file should exist"
assert health["pyproject_exists"] is True, "pyproject.toml should exist"
# Check core packages
assert isinstance(health["core_packages"], dict), (
"core_packages should be a dict"
)
core_expected: list[Any] = [
"numpy",
"matplotlib",
"networkx",
"pandas",
"scipy",
"pytest",
]
for pkg in core_expected:
assert pkg in health["core_packages"], f"Core package {pkg} missing"
assert health["core_packages"][pkg] is not None, (
f"Core package {pkg} version is None"
)
# No critical issues
assert len(health["issues"]) == 0, f"Unexpected issues: {health['issues']}"
def test_get_environment_info(self) -> Any:
"""Test get_environment_info function."""
info = get_environment_info()
assert isinstance(info, dict), "get_environment_info should return a dict"
assert "status" in info, "Info should have 'status' key"
assert info["status"] == "healthy" or "error" not in info, (
f"Environment not healthy: {info.get('error')}"
)
class TestOptionalGroups:
"""Test optional dependency groups configuration."""
def test_optional_groups_defined(self) -> Any:
"""Test that OPTIONAL_GROUPS constant is defined."""
assert isinstance(OPTIONAL_GROUPS, dict), "OPTIONAL_GROUPS should be a dict"
assert len(OPTIONAL_GROUPS) > 0, "OPTIONAL_GROUPS should not be empty"
def test_optional_groups_have_descriptions(self) -> Any:
"""Test that all optional groups have descriptions."""
for group, description in OPTIONAL_GROUPS.items():
assert isinstance(group, str), f"Group name should be string: {group}"
assert isinstance(description, str), (
f"Group description should be string: {description}"
)
assert len(description) > 0, f"Group {group} has empty description"
def test_expected_optional_groups_exist(self) -> Any:
"""Test that expected optional groups are defined."""
expected_groups: list[Any] = [
"dev",
"api",
"ml-ai",
"audio",
"gui",
"graphs",
"research",
"scaling",
"all",
]
for group in expected_groups:
assert group in OPTIONAL_GROUPS, (
f"Expected optional group '{group}' not found"
)
class TestFeatureFlags:
"""Test feature flags configuration."""
def test_features_defined(self) -> Any:
"""Test that FEATURES constant is defined."""
assert isinstance(FEATURES, dict), "FEATURES should be a dict"
assert len(FEATURES) > 0, "FEATURES should not be empty"
def test_critical_features_enabled(self) -> Any:
"""Test that critical features are enabled."""
critical_features: list[Any] = [
"uv_environment_setup",
"uv_dependency_management",
"uv_virtual_environment",
"system_validation",
"native_uv_sync",
]
for feature in critical_features:
assert feature in FEATURES, f"Critical feature '{feature}' not defined"
assert FEATURES[feature] is True, (
f"Critical feature '{feature}' should be enabled"
)
class TestNativeUVFunctions:
"""Test native UV functions (add, remove, update, lock)."""
def test_add_uv_dependency_function_exists(self) -> Any:
"""Test that add_uv_dependency function exists and is callable."""
assert callable(add_uv_dependency), "add_uv_dependency should be callable"
def test_remove_uv_dependency_function_exists(self) -> Any:
"""Test that remove_uv_dependency function exists and is callable."""
assert callable(remove_uv_dependency), "remove_uv_dependency should be callable"
def test_update_uv_dependencies_function_exists(self) -> Any:
"""Test that update_uv_dependencies function exists and is callable."""
assert callable(update_uv_dependencies), (
"update_uv_dependencies should be callable"
)
def test_lock_uv_dependencies_function_exists(self) -> Any:
"""Test that lock_uv_dependencies function exists and is callable."""
assert callable(lock_uv_dependencies), "lock_uv_dependencies should be callable"
class TestUVRunIntegration:
"""Test UV run command integration."""
def test_uv_run_python(self) -> Any:
"""Test that uv run python works."""
result = subprocess.run( # nosec B607 B603
[UV_BIN, "run", "python", "-c", "print('Hello from UV')"],
capture_output=True,
text=True,
cwd=str(PROJECT_ROOT),
timeout=30,
)
assert result.returncode == 0, f"uv run python failed: {result.stderr}"
assert "Hello from UV" in result.stdout, "Unexpected output"
def test_uv_run_module_import(self) -> Any:
"""Test that uv run can import project modules."""
result = subprocess.run( # nosec B607 B603
[
UV_BIN,
"run",
"python",
"-c",
"from src.setup import FEATURES; print('Import OK')",
],
capture_output=True,
text=True,
cwd=str(PROJECT_ROOT),
timeout=30,
)
assert result.returncode == 0, f"Module import failed: {result.stderr}"
assert "Import OK" in result.stdout, "Module import verification failed"
def test_uv_run_pytest(self) -> Any:
"""Test that pytest is available in the UV-managed environment.
The documented default gate runs through ``uv run --extra dev``.
Validate that exact contract rather than relying on tool-cache details.
"""
result = subprocess.run( # nosec B603
[UV_BIN, "run", "--extra", "dev", "python", "-m", "pytest", "--version"],
capture_output=True,
text=True,
cwd=str(PROJECT_ROOT),
timeout=30,
)
assert result.returncode == 0, (
f"pytest not available via sys.executable: {result.stderr}"
)
assert "pytest" in result.stdout.lower(), "pytest version not found in output"
class TestUVCacheAndPerformance:
"""Test UV cache and performance features."""
def test_uv_cache_dir_accessible(self) -> Any:
"""Test that UV cache directory is accessible."""
result = subprocess.run( # nosec B607 B603
[UV_BIN, "cache", "dir"], capture_output=True, text=True, timeout=10
)
assert result.returncode == 0, f"uv cache dir failed: {result.stderr}"
Path(result.stdout.strip())
# Cache dir may not exist if nothing has been cached
# Just verify the command works
def test_uv_sync_fast(self) -> Any:
"""Test that ``uv sync --frozen --extra dev`` is fast and non-pruning."""
import time
# Do not use ``--all-extras`` here: it pulls large optional groups (e.g. gui) and
# can fail on low-disk systems during wheel extraction. Keep ``--extra dev`` so
# this default-suite test does not prune pytest, LSP, API, or websocket deps.
start = time.time()
result = subprocess.run( # nosec B607 B603
[UV_BIN, "sync", "--frozen", "--extra", "dev"],
capture_output=True,
text=True,
cwd=str(PROJECT_ROOT),
timeout=120,
)
elapsed = time.time() - start
err = (result.stderr or "") + (result.stdout or "")
if result.returncode != 0 and (
"No space" in err
or "No space left on device" in err
or "os error 28" in err
):
pytest.fail("Insufficient disk for uv cache / venv (errno 28)")
assert result.returncode == 0, f"uv sync failed: {result.stderr}"
# Cached sync is usually a few seconds; allow slow CI and cold cache.
assert elapsed < 120, f"uv sync took too long: {elapsed}s"
if __name__ == "__main__":
pytest.main([__file__, "-v", "--timeout=120"])