Skip to content

Commit

Permalink
Make empty-variable configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz committed Jun 17, 2024
1 parent a0086d1 commit 27dfc4f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/releasenotes/unreleased/rules.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
empty-variable can be now disabled for VAR (#1056)
--------------------------------------------------

I0912 ``empty-variable`` received new parameter ``variable_source`` which allows to enable the rule either only for
variables from ```*** Variables ***``` section or only ``VAR`` statements. By default it works on both.
43 changes: 43 additions & 0 deletions robocop/checkers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@

RULE_CATEGORY_ID = "09"


def comma_separated_list(value: str) -> list[str]:
# TODO validation
return value.split(",")


rules = {
"0901": Rule(
rule_id="0901",
Expand Down Expand Up @@ -207,11 +213,20 @@
added_in_version="1.7.0",
),
"0912": Rule(
RuleParam(
name="variable_source",
default="section,var",
converter=comma_separated_list,
show_type="comma separated list",
desc="Variable sources that will be checked",
),
rule_id="0912",
name="empty-variable",
msg="Use built-in variable {{ var_type }}{EMPTY} instead of leaving variable without value or using backslash",
severity=RuleSeverity.INFO,
docs="""
Variables with placeholder ${EMPTY} values are more explicit.
Example of rule violation::
*** Variables ***
Expand All @@ -222,6 +237,13 @@
... value
${EMPTY_WITH_BACKSLASH} \\ # used backslash
*** Keywords ***
Create Variables
VAR ${var_no_value} # missing value
VAR ${var_with_empty} ${EMPTY}
You can configure ``empty-variable`` rule to run only in ```*** Variables ***``` section or on
``VAR`` statements using ``variable_source`` parameter.
""",
added_in_version="1.10.0",
),
Expand Down Expand Up @@ -942,6 +964,27 @@ class EmptyVariableChecker(VisitorChecker):

reports = ("empty-variable",)

def __init__(self):
self.visit_var_section = False
self.visit_var = False
super().__init__()

def visit_File(self, node): # noqa
variable_source = self.param("empty-variable", "variable_source")
self.visit_var_section = "section" in variable_source
self.visit_var = "var" in variable_source
self.generic_visit(node)

def visit_VariableSection(self, node): # noqa
if self.visit_var_section:
self.generic_visit(node)

def visit_KeywordSection(self, node): # noqa
if self.visit_var:
self.generic_visit(node)

visit_TestCaseSection = visit_KeywordSection

def visit_Variable(self, node): # noqa
if get_errors(node):
return
Expand Down
4 changes: 4 additions & 0 deletions tests/atest/rules/misc/empty_variable/expected_output_var.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test.robot:51:5 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
test.robot:53:9 [I] 0912 Use built-in variable @{EMPTY} instead of leaving variable without value or using backslash
test.robot:54:9 [I] 0912 Use built-in variable &{EMPTY} instead of leaving variable without value or using backslash
test.robot:62:8 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
16 changes: 16 additions & 0 deletions tests/atest/rules/misc/empty_variable/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,21 @@ class TestRuleAcceptance(RuleAcceptance):
def test_rule(self):
self.check_rule(src_files=["test.robot"], expected_file="expected_output.txt", target_version=">=7")

def test_rule_only_var(self):
self.check_rule(
src_files=["test.robot"],
expected_file="expected_output_var.txt",
config="-c empty-variable:variable_source:var",
target_version=">=7",
)

def test_rule_only_var_section(self):
self.check_rule(
src_files=["test.robot"],
expected_file="expected_output_pre_var.txt",
config="-c empty-variable:variable_source:section",
target_version=">=7",
)

def test_rule_pre_var(self):
self.check_rule(src_files=["test.robot"], expected_file="expected_output_pre_var.txt", target_version="<7")

0 comments on commit 27dfc4f

Please sign in to comment.