|
| 1 | +from types import SimpleNamespace |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from packaging.version import parse as parse_python_version |
3 | 5 | from pytest_mock import MockerFixture |
@@ -198,6 +200,131 @@ def test_prepare_event_does_not_mutate_cached_defaults(event_defaults): |
198 | 200 | assert "duration_ms" not in event_defaults["properties"] |
199 | 201 |
|
200 | 202 |
|
| 203 | +@pytest.fixture |
| 204 | +def venv_state(monkeypatch: pytest.MonkeyPatch): |
| 205 | + """Force a deterministic `is_in_virtualenv` reading. |
| 206 | +
|
| 207 | + Returns: |
| 208 | + A callable that overrides `sys.prefix`, `sys.base_prefix`, and the |
| 209 | + `VIRTUAL_ENV` env-var for the duration of the test. |
| 210 | + """ |
| 211 | + |
| 212 | + def configure(*, prefix: str, base_prefix: str, virtual_env: str | None) -> None: |
| 213 | + monkeypatch.setattr(telemetry.sys, "prefix", prefix) |
| 214 | + monkeypatch.setattr(telemetry.sys, "base_prefix", base_prefix) |
| 215 | + if virtual_env is None: |
| 216 | + monkeypatch.delenv("VIRTUAL_ENV", raising=False) |
| 217 | + else: |
| 218 | + monkeypatch.setenv("VIRTUAL_ENV", virtual_env) |
| 219 | + |
| 220 | + return configure |
| 221 | + |
| 222 | + |
| 223 | +def test_is_in_virtualenv_detects_pep_405_venv(venv_state): |
| 224 | + venv_state(prefix="/tmp/venv", base_prefix="/usr", virtual_env=None) |
| 225 | + assert telemetry.is_in_virtualenv() is True |
| 226 | + |
| 227 | + |
| 228 | +def test_is_in_virtualenv_falls_back_to_virtual_env_var(venv_state): |
| 229 | + venv_state(prefix="/usr", base_prefix="/usr", virtual_env="/tmp/venv") |
| 230 | + assert telemetry.is_in_virtualenv() is True |
| 231 | + |
| 232 | + |
| 233 | +def test_is_in_virtualenv_returns_false_for_system_python(venv_state): |
| 234 | + venv_state(prefix="/usr", base_prefix="/usr", virtual_env=None) |
| 235 | + assert telemetry.is_in_virtualenv() is False |
| 236 | + |
| 237 | + |
| 238 | +@pytest.fixture |
| 239 | +def patch_telemetry_config(mocker: MockerFixture): |
| 240 | + """Patch ``telemetry.get_config`` with a stub of a chosen ``telemetry_enabled``. |
| 241 | +
|
| 242 | + Returns: |
| 243 | + A callable ``patch(enabled)`` that installs the mock on demand. |
| 244 | + """ |
| 245 | + |
| 246 | + def patch(*, enabled: bool) -> None: |
| 247 | + mocker.patch( |
| 248 | + "reflex.utils.telemetry.get_config", |
| 249 | + return_value=SimpleNamespace(telemetry_enabled=enabled), |
| 250 | + ) |
| 251 | + |
| 252 | + return patch |
| 253 | + |
| 254 | + |
| 255 | +@pytest.fixture |
| 256 | +def init_environment_cwd( |
| 257 | + tmp_path, monkeypatch: pytest.MonkeyPatch, patch_telemetry_config |
| 258 | +): |
| 259 | + """Chdir into a clean tmp dir and force telemetry-enabled config. |
| 260 | +
|
| 261 | + Returns: |
| 262 | + The temporary directory now serving as the working directory. |
| 263 | + """ |
| 264 | + monkeypatch.chdir(tmp_path) |
| 265 | + patch_telemetry_config(enabled=True) |
| 266 | + return tmp_path |
| 267 | + |
| 268 | + |
| 269 | +def test_get_init_environment_reports_dependency_files( |
| 270 | + init_environment_cwd, venv_state |
| 271 | +): |
| 272 | + (init_environment_cwd / "pyproject.toml").write_text("") |
| 273 | + (init_environment_cwd / "uv.lock").write_text("") |
| 274 | + (init_environment_cwd / "reflex.lock").mkdir() |
| 275 | + venv_state(prefix="/tmp/venv", base_prefix="/usr", virtual_env=None) |
| 276 | + |
| 277 | + assert telemetry.get_init_environment() == { |
| 278 | + "in_virtualenv": True, |
| 279 | + "has_pyproject_toml": True, |
| 280 | + "has_requirements_txt": False, |
| 281 | + "has_uv_lock": True, |
| 282 | + "has_reflex_lock": True, |
| 283 | + } |
| 284 | + |
| 285 | + |
| 286 | +def test_get_init_environment_reports_requirements_txt( |
| 287 | + init_environment_cwd, venv_state |
| 288 | +): |
| 289 | + (init_environment_cwd / "requirements.txt").write_text("") |
| 290 | + venv_state(prefix="/usr", base_prefix="/usr", virtual_env="/tmp/venv") |
| 291 | + |
| 292 | + assert telemetry.get_init_environment() == { |
| 293 | + "in_virtualenv": True, |
| 294 | + "has_pyproject_toml": False, |
| 295 | + "has_requirements_txt": True, |
| 296 | + "has_uv_lock": False, |
| 297 | + "has_reflex_lock": False, |
| 298 | + } |
| 299 | + |
| 300 | + |
| 301 | +def test_get_init_environment_empty_directory(init_environment_cwd, venv_state): |
| 302 | + venv_state(prefix="/usr", base_prefix="/usr", virtual_env=None) |
| 303 | + |
| 304 | + assert telemetry.get_init_environment() == { |
| 305 | + "in_virtualenv": False, |
| 306 | + "has_pyproject_toml": False, |
| 307 | + "has_requirements_txt": False, |
| 308 | + "has_uv_lock": False, |
| 309 | + "has_reflex_lock": False, |
| 310 | + } |
| 311 | + |
| 312 | + |
| 313 | +def test_get_init_environment_short_circuits_when_telemetry_disabled( |
| 314 | + tmp_path, monkeypatch: pytest.MonkeyPatch, patch_telemetry_config |
| 315 | +): |
| 316 | + """When telemetry is disabled the env snapshot is skipped entirely. |
| 317 | +
|
| 318 | + A pyproject.toml is staged so a non-short-circuiting implementation would |
| 319 | + surface ``has_pyproject_toml: True`` instead of an empty dict. |
| 320 | + """ |
| 321 | + monkeypatch.chdir(tmp_path) |
| 322 | + (tmp_path / "pyproject.toml").write_text("") |
| 323 | + patch_telemetry_config(enabled=False) |
| 324 | + |
| 325 | + assert telemetry.get_init_environment() == {} |
| 326 | + |
| 327 | + |
201 | 328 | def test_prepare_event_properties_override_kwargs(event_defaults): |
202 | 329 | """If both kwargs and properties supply the same key, properties wins.""" |
203 | 330 | event = telemetry._prepare_event( |
|
0 commit comments