|
| 1 | +"""Unit tests for opt-in rich console/traceback behavior in sagemaker.core.utils.utils. |
| 2 | +
|
| 3 | +These lock in that importing the SDK does not override sys.excepthook or restyle the |
| 4 | +process-global rich console unless the user explicitly opts in via |
| 5 | +SAGEMAKER_ENABLE_RICH_LOGGING (or a force=True call). |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from sagemaker.core.utils import utils |
| 14 | +from sagemaker.core.utils.utils import ( |
| 15 | + RICH_LOGGING_OPT_IN_ENV_VAR, |
| 16 | + enable_textual_rich_console_and_traceback, |
| 17 | + is_rich_logging_enabled, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(autouse=True) |
| 22 | +def _reset_latch(): |
| 23 | + """Reset the one-shot 'already enabled' latch around every test.""" |
| 24 | + saved = utils.textual_rich_console_and_traceback_enabled |
| 25 | + utils.textual_rich_console_and_traceback_enabled = False |
| 26 | + try: |
| 27 | + yield |
| 28 | + finally: |
| 29 | + utils.textual_rich_console_and_traceback_enabled = saved |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize("value", ["1", "true", "TRUE", "Yes", "on", " on "]) |
| 33 | +def test_is_rich_logging_enabled_truthy(value): |
| 34 | + with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: value}): |
| 35 | + assert is_rich_logging_enabled() is True |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("value", ["", "0", "false", "no", "off", "nope"]) |
| 39 | +def test_is_rich_logging_enabled_falsey(value): |
| 40 | + with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: value}): |
| 41 | + assert is_rich_logging_enabled() is False |
| 42 | + |
| 43 | + |
| 44 | +def test_disabled_by_default_is_noop(): |
| 45 | + # Env var absent -> importing/using the SDK must not touch the global console |
| 46 | + # or install rich tracebacks (no sys.excepthook override). |
| 47 | + with patch.dict(os.environ, {}, clear=False): |
| 48 | + os.environ.pop(RICH_LOGGING_OPT_IN_ENV_VAR, None) |
| 49 | + with ( |
| 50 | + patch.object(utils, "reconfigure") as mock_reconfigure, |
| 51 | + patch.object(utils, "install") as mock_install, |
| 52 | + ): |
| 53 | + enable_textual_rich_console_and_traceback() |
| 54 | + mock_reconfigure.assert_not_called() |
| 55 | + mock_install.assert_not_called() |
| 56 | + assert utils.textual_rich_console_and_traceback_enabled is False |
| 57 | + |
| 58 | + |
| 59 | +def test_enabled_when_opted_in_via_env(): |
| 60 | + with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: "true"}): |
| 61 | + with ( |
| 62 | + patch.object(utils, "reconfigure") as mock_reconfigure, |
| 63 | + patch.object(utils, "install") as mock_install, |
| 64 | + ): |
| 65 | + enable_textual_rich_console_and_traceback() |
| 66 | + mock_reconfigure.assert_called_once() |
| 67 | + mock_install.assert_called_once() |
| 68 | + assert utils.textual_rich_console_and_traceback_enabled is True |
| 69 | + |
| 70 | + |
| 71 | +def test_force_enables_regardless_of_env(): |
| 72 | + with patch.dict(os.environ, {}, clear=False): |
| 73 | + os.environ.pop(RICH_LOGGING_OPT_IN_ENV_VAR, None) |
| 74 | + with ( |
| 75 | + patch.object(utils, "reconfigure") as mock_reconfigure, |
| 76 | + patch.object(utils, "install") as mock_install, |
| 77 | + ): |
| 78 | + enable_textual_rich_console_and_traceback(force=True) |
| 79 | + mock_reconfigure.assert_called_once() |
| 80 | + mock_install.assert_called_once() |
| 81 | + |
| 82 | + |
| 83 | +def test_enable_is_idempotent_when_opted_in(): |
| 84 | + with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: "1"}): |
| 85 | + with ( |
| 86 | + patch.object(utils, "reconfigure") as mock_reconfigure, |
| 87 | + patch.object(utils, "install") as mock_install, |
| 88 | + ): |
| 89 | + enable_textual_rich_console_and_traceback() |
| 90 | + enable_textual_rich_console_and_traceback() |
| 91 | + # The one-shot latch prevents re-installing on the second call. |
| 92 | + mock_reconfigure.assert_called_once() |
| 93 | + mock_install.assert_called_once() |
| 94 | + |
| 95 | + |
| 96 | +def test_get_logger_does_not_call_basicconfig_when_opted_out(): |
| 97 | + # Getting a module logger must not reconfigure the root logger by default. |
| 98 | + with patch.dict(os.environ, {}, clear=False): |
| 99 | + os.environ.pop(RICH_LOGGING_OPT_IN_ENV_VAR, None) |
| 100 | + with patch.object(utils.logging, "basicConfig") as mock_basic_config: |
| 101 | + returned = utils.get_textual_rich_logger("sagemaker.core.test.optout") |
| 102 | + mock_basic_config.assert_not_called() |
| 103 | + assert returned is utils.logging.getLogger("sagemaker.core.test.optout") |
| 104 | + |
| 105 | + |
| 106 | +def test_get_logger_calls_basicconfig_when_opted_in(): |
| 107 | + with patch.dict(os.environ, {RICH_LOGGING_OPT_IN_ENV_VAR: "1"}): |
| 108 | + with ( |
| 109 | + patch.object(utils, "reconfigure"), |
| 110 | + patch.object(utils, "install"), |
| 111 | + patch.object(utils.logging, "basicConfig") as mock_basic_config, |
| 112 | + ): |
| 113 | + utils.get_textual_rich_logger("sagemaker.core.test.optin") |
| 114 | + mock_basic_config.assert_called_once() |
0 commit comments