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

Ouverte Adaptée aux débutants
#7,844 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@zeyadrabie y travaille déjà.

Depuis le 15/11/2026.

  • #7901 par @zeyadrabie — ouverte

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
82/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
python

Piste de recherche

Commencez par la fixture autouse dans tests/units/conftest.py et les exemples de réinitialisation du logging dans tests/units/reflex_base/utils/test_log.py. Vérifiez comment tests/units/reflex_cli/v2/conftest.py active le logging géré afin que ces tests conservent leur configuration. Le résultat est atteint lorsque chaque test commence sans état résiduel de REFLEX_MANAGED_LOGGING ni du sink de logging ; exécutez les tests unitaires concernés, y compris un appel CLI suivi d’un test de migration.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.
Langage dominant
Python
Étoiles
29k
Forks
1.8k
Merge moyen
2 j 22 h
PR mergées (30 j)
154

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de reflex-dev/reflex

Toutes les issues de reflex-dev/reflex

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.