-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This rewrites version names into actual version representations. Signed-off-by: Pedro Algarvio <[email protected]>
- Loading branch information
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |