Skip to content

Commit 6f2582c

Browse files
authored
Fix variables in tag renamed with NormalizeTags (#642)
1 parent eed10fb commit 6f2582c

File tree

7 files changed

+91
-7
lines changed

7 files changed

+91
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Variables in tags are no longer renamed with NormalizeTags (#638)
2+
-----------------------------------------------------------------
3+
4+
``NormalizeTags`` transformer incorrectly affected variables used in tags::
5+
6+
*** Settings ***
7+
Test Tags tag with ${var}
8+
9+
Such variables will be now ignored.

robotidy/transformers/NormalizeTags.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from robotidy.disablers import skip_section_if_disabled
44
from robotidy.exceptions import InvalidParameterValueError
55
from robotidy.transformers import Transformer
6+
from robotidy.utils import variable_matcher
67

78

89
class NormalizeTags(Transformer):
@@ -39,14 +40,15 @@ class NormalizeTags(Transformer):
3940

4041
def __init__(self, case: str = "lowercase", normalize_case: bool = True, preserve_format: bool = False):
4142
super().__init__()
42-
self.case = case.lower()
43+
self.case_function = case.lower()
4344
self.normalize_case = normalize_case
4445
self.preserve_format = preserve_format
45-
try:
46-
self.case_function = self.CASE_FUNCTIONS[self.case]
47-
except KeyError:
46+
self.validate_case_function()
47+
48+
def validate_case_function(self):
49+
if self.case_function not in self.CASE_FUNCTIONS:
4850
raise InvalidParameterValueError(
49-
self.__class__.__name__, "case", case, "Supported cases: lowercase, uppercase, titlecase."
51+
self.__class__.__name__, "case", self.case_function, "Supported cases: lowercase, uppercase, titlecase."
5052
)
5153

5254
@skip_section_if_disabled
@@ -69,13 +71,28 @@ def normalize_tags(self, node, indent=False):
6971
return self.normalize_tags_tokens_preserve_formatting(node)
7072
return self.normalize_tags_tokens_ignore_formatting(node, indent)
7173

74+
def format_with_case_function(self, string: str) -> str:
75+
if "{" not in string:
76+
return self.CASE_FUNCTIONS[self.case_function](string)
77+
tag = ""
78+
var_found = False
79+
for match in variable_matcher.VariableMatches(string, ignore_errors=True):
80+
var_found = True
81+
tag += self.CASE_FUNCTIONS[self.case_function](match.before)
82+
tag += match.match
83+
tag += self.CASE_FUNCTIONS[self.case_function](match.after)
84+
if var_found:
85+
return tag
86+
else:
87+
return self.CASE_FUNCTIONS[self.case_function](string)
88+
7289
def normalize_tags_tokens_preserve_formatting(self, node):
7390
if not self.normalize_case:
7491
return node
7592
for token in node.tokens:
7693
if token.type != Token.ARGUMENT:
7794
continue
78-
token.value = self.case_function(token.value)
95+
token.value = self.format_with_case_function(token.value)
7996
return node
8097

8198
def normalize_tags_tokens_ignore_formatting(self, node, indent):
@@ -99,7 +116,7 @@ def normalize_tags_tokens_ignore_formatting(self, node, indent):
99116
return node
100117

101118
def convert_case(self, tags):
102-
return [self.case_function(item) for item in tags]
119+
return [self.format_with_case_function(item) for item in tags]
103120

104121
@staticmethod
105122
def remove_duplicates(tags):
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*** Settings ***
2+
Test Tags simple with ${VAR}
3+
Default Tags simple with ${VAR lower}
4+
5+
6+
*** Test Cases ***
7+
No tags
8+
Keyword no tags
9+
10+
Tags
11+
[Tags] simple with ${VAR} ${tag} ${tag} after
12+
One Tag Keyword
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*** Settings ***
2+
Test Tags Simple With ${VAR}
3+
Default Tags Simple With ${VAR lower}
4+
5+
6+
*** Test Cases ***
7+
No tags
8+
Keyword no tags
9+
10+
Tags
11+
[Tags] Simple With ${VAR} ${tag} ${tag} After
12+
One Tag Keyword
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*** Settings ***
2+
Test Tags SIMPLE WITH ${VAR}
3+
Default Tags SIMPLE WITH ${VAR lower}
4+
5+
6+
*** Test Cases ***
7+
No tags
8+
Keyword no tags
9+
10+
Tags
11+
[Tags] SIMPLE WITH ${VAR} ${tag} ${tag} AFTER
12+
One Tag Keyword
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*** Settings ***
2+
Test Tags simple WITH ${VAR}
3+
Default Tags SIMPLE with ${VAR lower}
4+
5+
6+
*** Test Cases ***
7+
No tags
8+
Keyword no tags
9+
10+
Tags
11+
[Tags] SIMPLE with ${VAR} ${tag}
12+
... ${tag} after
13+
One Tag Keyword

tests/atest/transformers/NormalizeTags/test_transformer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ def test_preserve_format_do_not_normalize_case(self):
6464

6565
def test_ignore_format(self):
6666
self.compare(source="preserve_format.robot", expected="preserve_format_default.robot")
67+
68+
@pytest.mark.parametrize("case_function", ["lowercase", "uppercase", "titlecase"])
69+
def test_variable_in_tag(self, case_function: str):
70+
self.compare(
71+
source="variables_in_tags.robot",
72+
expected=f"variables_in_tags_{case_function}.robot",
73+
config=f":case={case_function}",
74+
target_version=">=6",
75+
)

0 commit comments

Comments
 (0)