Skip to content

Commit

Permalink
Add fix_warn_until fixer.
Browse files Browse the repository at this point in the history
This rewrites version names into actual version representations.

Signed-off-by: Pedro Algarvio <[email protected]>
  • Loading branch information
s0undt3ch committed Jun 18, 2023
1 parent 17c2f02 commit 1c226f4
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/saltrewrite/salt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=missing-module-docstring
from saltrewrite.salt import fix_docstrings
from saltrewrite.salt import fix_dunder_utils
from saltrewrite.salt import fix_warn_until

__all__ = [modname for modname in list(globals()) if modname.startswith("fix_")]

Expand Down
57 changes: 57 additions & 0 deletions src/saltrewrite/salt/fix_warn_until.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
saltrewrite.salt.fix_warn_until
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fix warn_until calls and replace version names with actual versions.
"""
from bowler import Query
from bowler import TOKEN
from fissix.pytree import Leaf
from saltrewrite.salt.utils import SaltStackVersion


def rewrite(paths, interactive=False, silent=False):
"""
Rewrite the passed in paths
"""
(
Query(paths)
.select(
"""
(
function_call=power<
function_name='warn_until'
function_parameters=trailer< '(' function_arguments=any* ')' >
remainder=any*
>
|
function_call=power<
any*
trailer< any* >*
trailer<
'.' function_name='warn_until'
>
function_parameters=trailer< '(' function_arguments=any* ')' >
any*
>
)
"""
)
.modify(fix_warn_unil_version)
.execute(write=True, interactive=interactive, silent=silent)
)


def fix_warn_unil_version(node, capture, filename):
"""
Automaticaly run fixes against docstrings
"""
arglist = capture["function_arguments"][0]
warn_until_version_argument = arglist.children[0]
if warn_until_version_argument.type == TOKEN.NUMBER:
salt_version = SaltStackVersion.parse(warn_until_version_argument.value)
else:
# Get the value and remove the quotes
salt_version = SaltStackVersion.parse(warn_until_version_argument.value[1:-1])
# Replace it with the SaltStackVersion.major attribute
arglist.children[0] = Leaf(TOKEN.NUMBER, str(salt_version), prefix=arglist.children[0].prefix)
88 changes: 88 additions & 0 deletions tests/salt/test_warn_until.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# pylint: disable=missing-module-docstring,missing-function-docstring,too-many-lines,consider-using-f-string
import textwrap

from saltrewrite.salt import fix_warn_until


def test_warn_until_func(tempfiles):
code = textwrap.dedent(
"""
from salt.utils.versions import warn_until
def one():
warn_until("Argon", "Code deprecated in 3008.0")
print("one")
def two():
warn_until(3008, "Code deprecated in 3008.0")
print("one")
def three():
warn_until(3008.0, "Code deprecated in 3008.0")
print("one")
"""
)
expected_code = textwrap.dedent(
"""
from salt.utils.versions import warn_until
def one():
warn_until(3008.0, "Code deprecated in 3008.0")
print("one")
def two():
warn_until(3008.0, "Code deprecated in 3008.0")
print("one")
def three():
warn_until(3008.0, "Code deprecated in 3008.0")
print("one")
"""
)
fpath = tempfiles.makepyfile(code, prefix="test_")
fix_warn_until.rewrite(fpath)
with open(fpath) as rfh:
new_code = rfh.read()
assert new_code == expected_code


def test_warn_until_func_full_import(tempfiles):
code = textwrap.dedent(
"""
import salt.utils.versions
def one():
salt.utils.versions.warn_until("Argon", "Code deprecated in 3008.0")
print("one")
def two():
salt.utils.versions.warn_until(3008, "Code deprecated in 3008.0")
print("one")
def three():
salt.utils.versions.warn_until(3008.0, "Code deprecated in 3008.0")
print("one")
"""
)
expected_code = textwrap.dedent(
"""
import salt.utils.versions
def one():
salt.utils.versions.warn_until(3008.0, "Code deprecated in 3008.0")
print("one")
def two():
salt.utils.versions.warn_until(3008.0, "Code deprecated in 3008.0")
print("one")
def three():
salt.utils.versions.warn_until(3008.0, "Code deprecated in 3008.0")
print("one")
"""
)
fpath = tempfiles.makepyfile(code, prefix="test_")
fix_warn_until.rewrite(fpath)
with open(fpath) as rfh:
new_code = rfh.read()
assert new_code == expected_code

0 comments on commit 1c226f4

Please sign in to comment.