Unit tests: invoking the reflex CLI in-process leaves REFLEX_MANAGED_LOGGING=true in os.environ for every later test

Abierto Apto para principiantes
#7,844 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@zeyadrabie ya está trabajando en esto.

Desde el 15/11/2026.

  • #7901 de @zeyadrabie — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
82/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python

Línea de trabajo

Empieza por el fixture autouse de tests/units/conftest.py y los ejemplos de restablecimiento del logging en tests/units/reflex_base/utils/test_log.py. Comprueba cómo tests/units/reflex_cli/v2/conftest.py habilita el logging gestionado para que esas pruebas conserven su configuración. El objetivo se cumple cuando cada prueba comienza sin estado residual de REFLEX_MANAGED_LOGGING ni del sink de logging; ejecuta las pruebas unitarias afectadas, incluida una invocación de CLI seguida de una prueba de migración.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

release-train

Problem

The reflex click group callback (cli() in reflex/reflex.py) calls log.enable_managed_logging(), which sets os.environ["REFLEX_MANAGED_LOGGING"] = "true" and configures the logging sinks. That is correct for the real CLI process, but unit tests that run the CLI in-process with click.testing.CliRunner().invoke(cli, [...]) (about a dozen of them across tests/units/test_reflex.py, tests/units/test_prerequisites.py, tests/units/reflex_cli/v2/test_deploy.py, tests/units/reflex_bench/test_cli.py) never undo it. CliRunner does not isolate os.environ, so every test that runs later in the same pytest process sees managed logging switched on.

This makes test outcomes order-dependent: while working on #7797, a migrations test failed on unmodified main when it ran after one of these CLI tests (it passes on its own). Tests that assert on log output or on whether logging is configured are the ones affected.

Not user-facing; it affects contributors and CI stability.

Repro

Minimal demonstration of the leak (run with the repo's dev environment):

import os
from click.testing import CliRunner
from reflex.reflex import cli

print("before:", os.environ.get("REFLEX_MANAGED_LOGGING"))   # None
CliRunner().invoke(cli, ["db", "--help"])
print("after:", os.environ.get("REFLEX_MANAGED_LOGGING"))    # 'true'

To find the affected test pair, run the unit suite with a random or reversed order (for example uv run pytest tests/units -p randomly with pytest-randomly, or pick one CliRunner test followed by tests/units/utils/test_migrations.py).

Expected

Each test starts with the same environment. A test that invokes the CLI in-process does not change os.environ or the global logging configuration for later tests.

Fix direction

  • Add an autouse fixture in tests/units/conftest.py that removes REFLEX_MANAGED_LOGGING before each test (monkeypatch.delenv(log._MANAGED_ENV_VAR, raising=False), from reflex_base.utils.log) so monkeypatch restores it afterwards, and resets the logging sinks the same way the existing tests in tests/units/reflex_base/utils/test_log.py do.
  • Tests that need managed logging already set it with monkeypatch.setenv (e.g. tests/units/reflex_cli/v2/conftest.py); they keep working.
  • Non-goal: do not change cli() itself; setting the variable there is how worker subprocesses inherit managed logging.
Lenguaje dominante
Python
Estrellas
29k
Forks
1.8k
Merge medio
2 d 22 h
PR fusionados (30 d)
154

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de reflex-dev/reflex

Todos los issues de reflex-dev/reflex

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.