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

未关闭 适合新手
#7,844 2 条评论 0 个 reaction 已指派 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 再运行迁移测试。

由索引模型根据 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 小时
30 天内合并 PR
154

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

reflex-dev/reflex 的其他 Issue

查看 reflex-dev/reflex 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。