File size: 10,268 Bytes
a0ea4e3 | 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 | # Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
"""Tests for CLI utility functions used by the uv installation path."""
import os
import subprocess
import sys
from pathlib import Path
from unittest import mock
import pytest
from isaaclab.cli.utils import (
determine_python_version,
extract_isaacsim_path,
extract_python_exe,
get_pip_command,
)
def _python_in_venv(venv: Path) -> Path:
if sys.platform == "win32":
return venv / "Scripts" / "python.exe"
return venv / "bin" / "python"
def _python_for_conda(base: Path) -> Path:
if sys.platform == "win32":
return base / "python.exe"
return base / "bin" / "python"
# ---------------------------------------------------------------------------
# get_pip_command
# ---------------------------------------------------------------------------
class TestGetPipCommand:
"""Tests for :func:`get_pip_command`."""
def test_returns_uv_pip_in_venv_without_pip_module(self, tmp_path):
"""When VIRTUAL_ENV is set, uv is on PATH, and pip module is missing, return uv pip."""
fake_python = str(tmp_path / "python")
with (
mock.patch.dict(os.environ, {"VIRTUAL_ENV": str(tmp_path)}),
mock.patch("isaaclab.cli.utils.shutil.which", return_value="/usr/bin/uv"),
mock.patch(
"isaaclab.cli.utils.subprocess.run",
return_value=subprocess.CompletedProcess(args=[], returncode=1),
),
):
result = get_pip_command(python_exe=fake_python)
assert result == ["uv", "pip"]
def test_returns_uv_pip_in_venv_with_uv(self, tmp_path):
"""When VIRTUAL_ENV is set and uv is on PATH, always return uv pip."""
fake_python = str(tmp_path / "python")
with (
mock.patch.dict(os.environ, {"VIRTUAL_ENV": str(tmp_path)}),
mock.patch("isaaclab.cli.utils.shutil.which", return_value="/usr/bin/uv"),
):
result = get_pip_command(python_exe=fake_python)
assert result == ["uv", "pip"]
def test_returns_python_pip_without_uv(self, tmp_path):
"""When uv is not installed, always return python -m pip."""
fake_python = str(tmp_path / "python")
with (
mock.patch.dict(os.environ, {"VIRTUAL_ENV": str(tmp_path)}),
mock.patch("isaaclab.cli.utils.shutil.which", return_value=None),
):
result = get_pip_command(python_exe=fake_python)
assert result == [fake_python, "-m", "pip"]
def test_returns_python_pip_in_conda_without_uv(self, tmp_path):
"""When in a conda env and uv is not available, return python -m pip."""
fake_python = str(tmp_path / "python")
env = os.environ.copy()
env.pop("VIRTUAL_ENV", None)
env["CONDA_PREFIX"] = str(tmp_path)
with (
mock.patch.dict(os.environ, env, clear=True),
mock.patch("isaaclab.cli.utils.shutil.which", return_value=None),
):
result = get_pip_command(python_exe=fake_python)
assert result == [fake_python, "-m", "pip"]
# ---------------------------------------------------------------------------
# extract_python_exe
# ---------------------------------------------------------------------------
class TestExtractPythonExe:
"""Tests for :func:`extract_python_exe`."""
def test_uses_virtual_env_when_set(self, tmp_path):
"""Should return the venv Python when VIRTUAL_ENV is set."""
venv_python = _python_in_venv(tmp_path)
venv_python.parent.mkdir(parents=True, exist_ok=True)
venv_python.touch()
with mock.patch.dict(os.environ, {"VIRTUAL_ENV": str(tmp_path)}, clear=False):
result = extract_python_exe()
assert Path(result) == venv_python
def test_uses_conda_prefix_when_no_venv(self, tmp_path):
"""Should return conda Python when CONDA_PREFIX is set and no VIRTUAL_ENV."""
conda_python = _python_for_conda(tmp_path)
conda_python.parent.mkdir(parents=True, exist_ok=True)
conda_python.touch()
env = os.environ.copy()
env.pop("VIRTUAL_ENV", None)
env["CONDA_PREFIX"] = str(tmp_path)
with mock.patch.dict(os.environ, env, clear=True):
result = extract_python_exe()
assert Path(result) == conda_python
# ---------------------------------------------------------------------------
# extract_isaacsim_path
# ---------------------------------------------------------------------------
class TestExtractIsaacsimPath:
"""Tests for :func:`extract_isaacsim_path`."""
def test_returns_none_when_not_required(self):
"""When required=False and Isaac Sim is not found, return None."""
with (
mock.patch("isaaclab.cli.utils.DEFAULT_ISAAC_SIM_PATH", Path("/nonexistent/path")),
mock.patch(
"isaaclab.cli.utils.subprocess.run",
return_value=subprocess.CompletedProcess(args=[], returncode=1),
),
):
result = extract_isaacsim_path(required=False)
assert result is None
def test_exits_when_required(self):
"""When required=True and Isaac Sim is not found, sys.exit."""
with (
mock.patch("isaaclab.cli.utils.DEFAULT_ISAAC_SIM_PATH", Path("/nonexistent/path")),
mock.patch(
"isaaclab.cli.utils.subprocess.run",
return_value=subprocess.CompletedProcess(args=[], returncode=1),
),
pytest.raises(SystemExit),
):
extract_isaacsim_path(required=True)
def test_returns_path_when_symlink_exists(self, tmp_path):
"""When the default path exists, return it."""
fake_sim = tmp_path / "_isaac_sim"
fake_sim.mkdir()
with mock.patch("isaaclab.cli.utils.DEFAULT_ISAAC_SIM_PATH", fake_sim):
result = extract_isaacsim_path(required=True)
assert result == fake_sim
# ---------------------------------------------------------------------------
# determine_python_version
# ---------------------------------------------------------------------------
class TestDeterminePythonVersion:
"""Tests for :func:`determine_python_version`."""
def test_defaults_to_3_12_when_no_sim(self):
"""Without Isaac Sim, should default to python 3.12 (Isaac Sim 6.x requirement)."""
with (
mock.patch("isaaclab.cli.utils.extract_isaacsim_path", return_value=None),
mock.patch("importlib.metadata.version", side_effect=Exception("not found")),
):
result = determine_python_version()
assert result == "3.12"
def test_returns_3_11_for_sim_5(self, tmp_path):
"""Isaac Sim 5.x should map to Python 3.11."""
version_file = tmp_path / "VERSION"
version_file.write_text("5.0.0")
with mock.patch("isaaclab.cli.utils.extract_isaacsim_path", return_value=tmp_path):
result = determine_python_version()
assert result == "3.11"
def test_returns_3_12_for_sim_6(self, tmp_path):
"""Isaac Sim 6.x should map to Python 3.12."""
version_file = tmp_path / "VERSION"
version_file.write_text("6.0.0")
with mock.patch("isaaclab.cli.utils.extract_isaacsim_path", return_value=tmp_path):
result = determine_python_version()
assert result == "3.12"
def test_raises_for_unknown_version(self, tmp_path):
"""Unknown Isaac Sim version should raise RuntimeError."""
version_file = tmp_path / "VERSION"
version_file.write_text("99.0.0")
with (
mock.patch("isaaclab.cli.utils.extract_isaacsim_path", return_value=tmp_path),
pytest.raises(RuntimeError, match="Unsupported Isaac Sim version"),
):
determine_python_version()
def test_uses_package_metadata_when_no_version_file(self, tmp_path):
"""Should fall back to importlib.metadata when VERSION file doesn't exist."""
# tmp_path exists but has no VERSION file
with (
mock.patch("isaaclab.cli.utils.extract_isaacsim_path", return_value=tmp_path),
mock.patch("importlib.metadata.version", return_value="5.1.0"),
):
result = determine_python_version()
assert result == "3.11"
# ---------------------------------------------------------------------------
# Prebundled-torch shadowing invariant (regression: nvbugs 6343978)
# ---------------------------------------------------------------------------
def test_no_shadowing_prebundled_torch_in_isaac_sim():
"""A prebundled torch must not shadow the pip-installed torch.
Regression test for nvbugs 6343978: Isaac Sim 6.0 ships a prebundled PyTorch under
the deprecated ``omni.isaac.ml_archive`` extension whose ``libtorch_cuda.so``
requires an NCCL symbol the co-bundled NCCL does not export. Launch paths that do
not import :mod:`isaaclab` (e.g. ``isaac-sim.streaming.sh`` / ``runheadless.sh``)
bypass the ``sys.path`` deprioritization and import this broken copy, crashing with
``undefined symbol: ncclDevCommCreate``. After install/build, every
``pip_prebundle/torch`` under Isaac Sim must therefore be either removed or a
symlink into the active environment, never a real shadowing directory.
"""
isaacsim_path = extract_isaacsim_path(required=False)
if isaacsim_path is None or not isaacsim_path.exists():
pytest.skip("Isaac Sim installation not found; skipping prebundle-shadow invariant check")
shadowing = [
prebundled_torch
for prebundled_torch in isaacsim_path.rglob("pip_prebundle/torch")
if prebundled_torch.is_dir() and not prebundled_torch.is_symlink()
]
assert not shadowing, (
"Found prebundled torch directories that shadow the pip-installed torch (nvbugs 6343978). "
"They must be removed at image build time or repointed to the active environment:\n "
+ "\n ".join(str(p) for p in shadowing)
)
|