From e750a564a4aea02afefad8f741d1ff0fa29bda3b Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Tue, 9 Jan 2024 14:56:12 -0300 Subject: [PATCH] add more testing to logger.warn codemod --- ...ixee_python_fix-deprecated-logging-warn.md | 2 +- .../test_fix_deprecated_logging_warn.py | 46 ++++++++++++++----- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/core_codemods/docs/pixee_python_fix-deprecated-logging-warn.md b/src/core_codemods/docs/pixee_python_fix-deprecated-logging-warn.md index d620f8636..62ad694ac 100644 --- a/src/core_codemods/docs/pixee_python_fix-deprecated-logging-warn.md +++ b/src/core_codemods/docs/pixee_python_fix-deprecated-logging-warn.md @@ -1,4 +1,4 @@ -The `warn` method from `logging` has been [deprecated](https://docs.python.org/3/library/logging.html#logging.Logger.warning) since Python 3.3. +The `warn` method from `logging` has been [deprecated](https://docs.python.org/3/library/logging.html#logging.Logger.warning) in favor of `warning` since Python 3.3. Since the old method `warn` has been retained for a long time, there are a lot of developers that are unaware of this change and consequently a lot of code using the older method. Our changes look like the following: ```diff diff --git a/tests/codemods/test_fix_deprecated_logging_warn.py b/tests/codemods/test_fix_deprecated_logging_warn.py index f368c881d..4b05f87dc 100644 --- a/tests/codemods/test_fix_deprecated_logging_warn.py +++ b/tests/codemods/test_fix_deprecated_logging_warn.py @@ -10,8 +10,12 @@ class TestFixDeprecatedLoggingWarn(BaseSemgrepCodemodTest): "code", [ """ - import logging - logging.{}('something') + import logging + logging.{}('something') + """, + """ + import logging + logging.{}("something %s and %s", "foo", "bar") """, """ import logging @@ -49,18 +53,22 @@ def test_from_import(self, tmpdir, code): "input_code,expected_output", [ ( - """from logging import warn as warn_func -warn_func('something')""", - """from logging import warning -warning('something')""", + """\ + from logging import warn as warn_func + warn_func('something')""", + """\ + from logging import warning + warning('something')""", ), ( - """from logging import getLogger as make_logger -logger = make_logger('anything') -logger.warn('something')""", - """from logging import getLogger as make_logger -logger = make_logger('anything') -logger.warning('something')""", + """\ + from logging import getLogger as make_logger + logger = make_logger('anything') + logger.warn('something')""", + """\ + from logging import getLogger as make_logger + logger = make_logger('anything') + logger.warning('something')""", ), ], ) @@ -85,3 +93,17 @@ def test_import_alias(self, tmpdir, input_code, expected_output): def test_different_warn(self, tmpdir, code): self.run_and_assert(tmpdir, code, code) assert len(self.file_context.codemod_changes) == 0 + + @pytest.mark.xfail(reason="Not currently supported") + def test_log_as_arg(self, tmpdir): + code = """\ + import logging + log = logging.getLogger('foo') + def some_function(logger): + logger.{}("hi") + some_function(log) + """ + original_code = code.format("warn") + new_code = code.format("warning") + self.run_and_assert(tmpdir, original_code, new_code) + assert len(self.file_context.codemod_changes) == 1