Skip to content

Commit

Permalink
Add test case to verify that no changes are made when modules are uns…
Browse files Browse the repository at this point in the history
…afe to rewrite.

Signed-off-by: Pedro Algarvio <[email protected]>
  • Loading branch information
s0undt3ch committed Jun 8, 2023
1 parent 58cb3e8 commit 550883e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 30 deletions.
54 changes: 28 additions & 26 deletions src/saltrewrite/salt/fix_dunder_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,41 @@
from fissix.fixer_util import touch_import


SALT_DUNDERS = (
"__active_provider_name__",
"__context__",
"__env__",
"__events__",
"__executors__",
"__grains__",
"__instance_id__",
"__jid_event__",
"__low__",
"__lowstate__",
"__master_opts__",
"__opts__",
"__pillar__",
"__proxy__",
"__reg__",
"__ret__",
"__runner__",
"__running__",
"__salt__",
"__salt_system_encoding__",
"__serializers__",
"__states__",
"__utils__",
)


class DunderParser(ast.NodeTransformer): # pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring,invalid-name
def __init__(self):
self.virtualname = None
self.uses_salt_dunders = False

def visit_Name(self, node):
salt_dunders = (
"__active_provider_name__",
"__context__",
"__env__",
"__events__",
"__executors__",
"__grains__",
"__instance_id__",
"__jid_event__",
"__low__",
"__lowstate__",
"__master_opts__",
"__opts__",
"__pillar__",
"__proxy__",
"__reg__",
"__ret__",
"__runner__",
"__running__",
"__salt__",
"__salt_system_encoding__",
"__serializers__",
"__states__",
"__utils__",
)
if node.id in salt_dunders:
if node.id in SALT_DUNDERS:
self.uses_salt_dunders = True

def visit_Assign(self, node):
Expand Down
39 changes: 35 additions & 4 deletions tests/salt/test_dunder_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# pylint: disable=missing-module-docstring,missing-function-docstring,too-many-lines
# pylint: disable=missing-module-docstring,missing-function-docstring,too-many-lines,redefined-outer-name
import logging
import textwrap
from unittest.mock import patch

import pytest
from saltrewrite.salt import fix_dunder_utils

log = logging.getLogger(__name__)


@pytest.fixture(autouse=True)
def _salt_utils_modules(tmp_path):
modpath = tmp_path / "salt" / "utils" / "foo.py"
def salt_utils_package(tmp_path):
package_path = tmp_path / "salt" / "utils"
modpath = package_path / "foo.py"
modpath.parent.mkdir(parents=True, exist_ok=True)
modpath.parent.joinpath("__init__.py").touch()
code = textwrap.dedent(
Expand All @@ -19,7 +23,7 @@ def bar():
)
modpath.write_text(code)
with patch("saltrewrite.salt.fix_dunder_utils._get_salt_code_root", return_value=tmp_path):
yield
yield package_path


def test_fix_call_one_arg(tempfiles):
Expand Down Expand Up @@ -160,3 +164,30 @@ def one():
with open(fpath) as rfh:
new_code = rfh.read()
assert new_code == expected_code


@pytest.mark.parametrize("salt_dunder", fix_dunder_utils.SALT_DUNDERS)
def test_unsafe_utils_call_not_rewritten(tempfiles, salt_utils_package, salt_dunder):
unsafe_modpath = salt_utils_package / "unsafe.py"
modcode = textwrap.dedent(
f"""
def bar(one, two=None):
return {salt_dunder}["bar"](two=two)
"""
)
log.info("Generated utils module:\n>>>>>%s<<<<<", modcode)
unsafe_modpath.write_text(modcode)
code = textwrap.dedent(
"""
import one.two
def one():
one.two.three("four")
__utils__["unsafe.bar"]("one", two="two")
"""
)
fpath = tempfiles.makepyfile(code, prefix="test_")
fix_dunder_utils.rewrite(fpath)
with open(fpath) as rfh:
new_code = rfh.read()
assert new_code == code

0 comments on commit 550883e

Please sign in to comment.