diff --git a/mcoplib/profiler.py b/mcoplib/profiler.py index adfba41..8b319aa 100644 --- a/mcoplib/profiler.py +++ b/mcoplib/profiler.py @@ -6,13 +6,10 @@ def _is_profiler_enabled() -> bool: """ - Env switch: PROFILER_ENABLED==0 -> disabled, else enabled. + Env switch: PROFILER_ENABLED=false/0/off/no -> disabled, else enabled. """ v = os.getenv("PROFILER_ENABLED", "1") - try: - return not (str(v).strip() == "0") - except Exception: - return True + return v.strip().lower() not in {"0", "false", "off", "no"} def _timestamp() -> str: diff --git a/unit_test/test_profiler_env.py b/unit_test/test_profiler_env.py new file mode 100644 index 0000000..343bf4a --- /dev/null +++ b/unit_test/test_profiler_env.py @@ -0,0 +1,12 @@ +from mcoplib.profiler import _is_profiler_enabled + + +def test_profiler_disabled_by_common_false_values(monkeypatch): + for value in ("0", "false", "False", "off", "NO"): + monkeypatch.setenv("PROFILER_ENABLED", value) + assert not _is_profiler_enabled() + + +def test_profiler_enabled_by_default(monkeypatch): + monkeypatch.delenv("PROFILER_ENABLED", raising=False) + assert _is_profiler_enabled()