85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
"""Regression tests for the _debug_enabled helper.
|
|
|
|
Both app.cm_api and app.cm_web_view define a private _debug_enabled()
|
|
function that parses the CM_DEBUG environment variable. They are
|
|
intentionally duplicated (only two call sites; no shared utility module
|
|
exists). This test runs the same parametrized cases against every copy
|
|
to catch drift if one is updated without the other.
|
|
"""
|
|
|
|
import os
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
# Import the modules at top-level (before any mock.patch.dict with
|
|
# clear=True), so module-load-time os.getenv() reads see the real
|
|
# environment. The patches inside individual tests then only affect the
|
|
# helper's runtime read of CM_DEBUG.
|
|
import app.cm_api
|
|
import app.cm_web_view
|
|
|
|
|
|
# Modules expected to expose a private _debug_enabled() helper.
|
|
# Add new entries here if more Flask entrypoints adopt the same toggle.
|
|
HELPER_MODULES = (
|
|
app.cm_web_view,
|
|
app.cm_api,
|
|
)
|
|
|
|
|
|
# (env_value, expected_result). env_value=None means CM_DEBUG is unset.
|
|
CASES = (
|
|
(None, False),
|
|
("", False),
|
|
("false", False),
|
|
("False", False),
|
|
("FALSE", False),
|
|
("0", False),
|
|
("no", False),
|
|
("anything-else", False),
|
|
("true", True),
|
|
("True", True),
|
|
("TRUE", True),
|
|
("1", True),
|
|
("yes", True),
|
|
("YES", True),
|
|
(" true ", True),
|
|
)
|
|
|
|
|
|
class DebugEnabledTests(unittest.TestCase):
|
|
def _resolve(self, module):
|
|
return getattr(module, "_debug_enabled", None)
|
|
|
|
def test_helper_exists_on_every_module(self):
|
|
for module in HELPER_MODULES:
|
|
with self.subTest(module=module.__name__):
|
|
helper = self._resolve(module)
|
|
self.assertTrue(
|
|
callable(helper),
|
|
f"{module.__name__}._debug_enabled must be callable",
|
|
)
|
|
|
|
def test_parses_cm_debug_consistently(self):
|
|
for module in HELPER_MODULES:
|
|
helper = self._resolve(module)
|
|
if helper is None:
|
|
self.fail(
|
|
f"{module.__name__}._debug_enabled is missing — "
|
|
"make test_helper_exists_on_every_module pass first"
|
|
)
|
|
for env_value, expected in CASES:
|
|
with self.subTest(module=module.__name__, env=env_value):
|
|
env = {} if env_value is None else {"CM_DEBUG": env_value}
|
|
with mock.patch.dict(os.environ, env, clear=True):
|
|
self.assertEqual(
|
|
helper(),
|
|
expected,
|
|
f"{module.__name__}._debug_enabled() should be "
|
|
f"{expected!r} for CM_DEBUG={env_value!r}",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|