From 9c8389a08ed5daf98b89f105ed81007b27abbb3a Mon Sep 17 00:00:00 2001 From: "Josef M. Gallmetzer" <64498081+galjos@users.noreply.github.com> Date: Mon, 24 Aug 2026 09:18:57 +0200 Subject: [PATCH] fix: guard log file env var against missing logging level The log file check dereferenced logging_env_var instead of log_file_env_var, so setting PQANALYSIS_LOG_FILE without PQANALYSIS_LOGGING_LEVEL raised AttributeError on None and made import PQAnalysis fail entirely. The typo also inverted the off sentinel: PQANALYSIS_LOG_FILE=off enabled logging to a file named off. Comparing the correct variable fixes both the crash and the sentinel. --- PQAnalysis/__init__.py | 2 +- tests/test_logging_env_vars.py | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/test_logging_env_vars.py diff --git a/PQAnalysis/__init__.py b/PQAnalysis/__init__.py index c1f84243..55300d32 100644 --- a/PQAnalysis/__init__.py +++ b/PQAnalysis/__init__.py @@ -55,7 +55,7 @@ log_file_env_var = os.getenv("PQANALYSIS_LOG_FILE") -if log_file_env_var and logging_env_var.lower() != "off": +if log_file_env_var and log_file_env_var.lower() != "off": config.use_log_file = True if log_file_env_var.lower() != "on" and len(log_file_env_var) > 0: diff --git a/tests/test_logging_env_vars.py b/tests/test_logging_env_vars.py new file mode 100644 index 00000000..5f4ed2de --- /dev/null +++ b/tests/test_logging_env_vars.py @@ -0,0 +1,57 @@ +import os +import subprocess +import sys + +from pathlib import Path + +import PQAnalysis + +PACKAGE_ROOT = Path(PQAnalysis.__file__).resolve().parents[1] + + +def run_import(log_file_value): + env = os.environ.copy() + env.pop("PQANALYSIS_LOGGING_LEVEL", None) + env["PQANALYSIS_LOG_FILE"] = log_file_value + env["PYTHONPATH"] = str(PACKAGE_ROOT) + + return subprocess.run( + [ + sys.executable, + "-c", + "import PQAnalysis.config as config; import PQAnalysis; " + "print(config.use_log_file, config.log_file_name)", + ], + cwd=PACKAGE_ROOT, + env=env, + capture_output=True, + text=True, + check=False, + ) + + +def test_log_file_env_var_without_logging_level(): + result = run_import("on") + + assert result.returncode == 0, result.stderr + + use_log_file, log_file_name = result.stdout.split() + assert use_log_file == "True" + assert log_file_name.startswith("PQAnalysis_") + + +def test_log_file_env_var_off(): + result = run_import("off") + + assert result.returncode == 0, result.stderr + + use_log_file, log_file_name = result.stdout.split() + assert use_log_file == "False" + assert log_file_name != "off" + + +def test_log_file_env_var_custom_name(): + result = run_import("my_custom.log") + + assert result.returncode == 0, result.stderr + assert result.stdout.split() == ["True", "my_custom.log"]