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

オープン 初心者向け
#7,844 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@zeyadrabie がすでに取り組んでいます。

2026年11月15日 から。

  • #7901 @zeyadrabie による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

tests/units/conftest.py の autouse fixture と tests/units/reflex_base/utils/test_log.py の logging リセット例から始めてください。tests/units/reflex_cli/v2/conftest.py が managed logging をどのように有効にしているかを確認し、それらのテストがセットアップを維持できるようにしてください。各テストが REFLEX_MANAGED_LOGGING や logging sink の状態を残さずに開始することを完了条件とします。CLI の呼び出しに続けて migration テストを実行するケースを含め、影響を受ける unit test を実行してください。

索引モデルが issue の本文から書いたものです。

説明

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.
主要言語
Python
スター
29k
フォーク
1.8k
平均マージ
2日 22時間
マージ済み PR(30日)
154

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

reflex-dev/reflex のほかの issue

reflex-dev/reflex の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。