-
Notifications
You must be signed in to change notification settings - Fork 55
feat: Enable processing of empty values in multiple sections (variables, keywords, test cases) #1719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MobyNL
wants to merge
3
commits into
MarketSquare:main
Choose a base branch
from
MobyNL:enable-empty-values-in-multiple-sections
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: Enable processing of empty values in multiple sections (variables, keywords, test cases) #1719
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,12 +2,19 @@ | |
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from robot.api.parsing import Token | ||
| from robot.api.parsing import Keyword, KeywordSection, TestCase, TestCaseSection, Token | ||
|
|
||
| try: # RF7+ | ||
| from robot.api.parsing import Var | ||
| except ImportError: | ||
| Var = None | ||
|
|
||
| from robocop.formatter.disablers import skip_if_disabled, skip_section_if_disabled | ||
| from robocop.formatter.formatters import Formatter | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Sequence | ||
|
|
||
| from robot.parsing.model.blocks import VariableSection | ||
| from robot.parsing.model.statements import Variable | ||
|
|
||
|
|
@@ -41,35 +48,132 @@ class ReplaceEmptyValues(Formatter): | |
| ... ${EMPTY} | ||
| ... value3 | ||
| ``` | ||
|
|
||
| By default, this formatter only processes the Variables section. You can configure | ||
| which sections to process using the ``sections`` parameter: | ||
| - ``variables`` (default) - only Variables section | ||
| - ``keywords`` - only Keywords section | ||
| - ``testcases`` - only Test Cases section | ||
| - ``all`` - all sections | ||
| - Comma-separated list - e.g., ``variables,keywords`` | ||
|
|
||
| Configuration example in pyproject.toml: | ||
| ```toml | ||
| [tool.robocop.format] | ||
| configure = [ | ||
| "ReplaceEmptyValues.sections=all", | ||
| # or | ||
| "ReplaceEmptyValues.sections=variables,keywords", | ||
| ] | ||
| ``` | ||
| """ | ||
|
|
||
| HANDLES_SKIP = frozenset({"skip_sections"}) | ||
|
|
||
| def _replace_empty_args( | ||
| self, tokens: Sequence[Token], empty_value: str, *, trim_eol: bool = False | ||
| ) -> tuple[Token, ...]: | ||
| """Replace empty argument tokens while preserving continuation alignment.""" | ||
| new_tokens = [] | ||
| continuation_sep = Token(Token.SEPARATOR, self.formatting_config.continuation_indent) | ||
| prev_token = None | ||
| for token in tokens: | ||
| token_value = token.value or "" | ||
| if token.type == Token.ARGUMENT and not token_value.strip(): | ||
| if not prev_token or prev_token.type != Token.SEPARATOR: | ||
| new_tokens.append(continuation_sep) | ||
| new_tokens.append(Token(Token.ARGUMENT, empty_value)) | ||
| else: | ||
| if trim_eol and token.type == Token.EOL and token.value: | ||
| token.value = token.value.lstrip(" ") | ||
| new_tokens.append(token) | ||
| prev_token = token | ||
| return tuple(new_tokens) | ||
|
|
||
| def _insert_arg_after_token(self, tokens: Sequence[Token], anchor: Token, value: str) -> tuple[Token, ...]: | ||
| separator = Token(Token.SEPARATOR, self.formatting_config.separator) | ||
| new_tokens = [] | ||
| for token in tokens: | ||
| new_tokens.append(token) | ||
| if token == anchor: | ||
| new_tokens.append(separator) | ||
| new_tokens.append(Token(Token.ARGUMENT, value)) | ||
| return tuple(new_tokens) | ||
|
|
||
| @staticmethod | ||
| def _get_empty_value(var_name: str) -> str | None: | ||
| if var_name.startswith("${"): | ||
| return "${EMPTY}" | ||
| if var_name.startswith("@{"): | ||
| return "@{EMPTY}" | ||
| if var_name.startswith("&{"): | ||
| return "&{EMPTY}" | ||
| return None | ||
|
|
||
| def __init__(self, sections: str = "variables") -> None: | ||
| super().__init__() | ||
| if sections == "all": | ||
| self.enabled_sections = {"variables", "keywords", "testcases"} | ||
| else: | ||
| self.enabled_sections = {s.strip().lower() for s in sections.split(",")} | ||
|
|
||
| @skip_section_if_disabled | ||
| def visit_VariableSection(self, node: VariableSection) -> VariableSection: # noqa: N802 | ||
| if "variables" not in self.enabled_sections: | ||
| return node | ||
| return self.generic_visit(node) | ||
|
|
||
| @skip_section_if_disabled | ||
| def visit_TestCaseSection(self, node: TestCaseSection) -> TestCaseSection: # noqa: N802 | ||
| if "testcases" not in self.enabled_sections: | ||
| return node | ||
| return self.generic_visit(node) | ||
|
|
||
| @skip_section_if_disabled | ||
| def visit_KeywordSection(self, node: KeywordSection) -> KeywordSection: # noqa: N802 | ||
| if "keywords" not in self.enabled_sections: | ||
| return node | ||
| return self.generic_visit(node) | ||
|
|
||
| @skip_if_disabled | ||
| def visit_TestCase(self, node: TestCase) -> TestCase: # noqa: N802 | ||
| return self.generic_visit(node) | ||
|
|
||
| @skip_if_disabled | ||
| def visit_Keyword(self, node: Keyword) -> Keyword: # noqa: N802 | ||
| return self.generic_visit(node) | ||
|
|
||
| @skip_if_disabled | ||
| def visit_Variable(self, node: Variable) -> Variable: # noqa: N802 | ||
| if node.errors or not node.name: | ||
| return node | ||
| args = node.get_tokens(Token.ARGUMENT) | ||
| sep = Token(Token.SEPARATOR, self.formatting_config.separator) | ||
| new_line_sep = Token(Token.SEPARATOR, self.formatting_config.continuation_indent) | ||
| if args: | ||
| tokens = [] | ||
| prev_token = None | ||
| for token in node.tokens: | ||
| if token.type == Token.ARGUMENT and not token.value: | ||
| if not prev_token or prev_token.type != Token.SEPARATOR: | ||
| tokens.append(new_line_sep) | ||
| tokens.append(Token(Token.ARGUMENT, "${EMPTY}")) | ||
| else: | ||
| if token.type == Token.EOL: | ||
| token.value = token.value.lstrip(" ") | ||
| tokens.append(token) | ||
| prev_token = token | ||
| node.tokens = self._replace_empty_args(node.tokens, "${EMPTY}", trim_eol=True) | ||
| else: | ||
| node.tokens = self._insert_arg_after_token(node.tokens, node.tokens[0], node.name[0] + "{EMPTY}") | ||
| return node | ||
|
|
||
| @skip_if_disabled | ||
| def visit_Var(self, node: Var) -> Var: # noqa: N802 | ||
| """Handle inline VAR statements to replace empty values with proper EMPTY variables.""" | ||
| if Var is None or node.errors: | ||
| return node | ||
|
|
||
| variable_token = node.get_token(Token.VARIABLE) | ||
| if not variable_token: | ||
| return node | ||
|
|
||
| args = node.get_tokens(Token.ARGUMENT) | ||
| if any((arg.value or "").strip() for arg in args): | ||
| return node | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about: There is arg, but it's empty.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good one. I didn't think of multilines. It is now implemented! |
||
|
|
||
| empty_value = self._get_empty_value(variable_token.value) | ||
| if empty_value is None: | ||
| return node | ||
|
|
||
| if args: | ||
| node.tokens = self._replace_empty_args(node.tokens, empty_value) | ||
| else: | ||
| tokens = [node.tokens[0], sep, Token(Token.ARGUMENT, node.name[0] + "{EMPTY}"), *node.tokens[1:]] | ||
| node.tokens = tokens | ||
| node.tokens = self._insert_arg_after_token(node.tokens, variable_token, empty_value) | ||
| return node | ||
18 changes: 18 additions & 0 deletions
18
tests/formatter/formatters/ReplaceEmptyValues/expected/all_sections.robot
This file contains hidden or 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,18 @@ | ||
| *** Variables *** | ||
| ${EMPTY_SCALAR} ${EMPTY} | ||
| @{EMPTY_LIST} @{EMPTY} | ||
| &{EMPTY_DICT} &{EMPTY} | ||
|
|
||
|
|
||
| *** Test Cases *** | ||
| Test With Empty Vars | ||
| VAR ${empty_in_test} ${EMPTY} | ||
| VAR @{empty_list_test} @{EMPTY} | ||
| Log ${empty_in_test} | ||
|
|
||
|
|
||
| *** Keywords *** | ||
| Keyword With Empty Vars | ||
| VAR ${empty_in_kw} ${EMPTY} | ||
| VAR &{empty_dict_kw} &{EMPTY} | ||
| Log ${empty_in_kw} |
21 changes: 21 additions & 0 deletions
21
tests/formatter/formatters/ReplaceEmptyValues/expected/keywords.robot
This file contains hidden or 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,21 @@ | ||
| *** Keywords *** | ||
| Keyword With Empty Vars | ||
| VAR ${empty_scalar} ${EMPTY} | ||
| VAR @{empty_list} @{EMPTY} | ||
| VAR &{empty_dict} &{EMPTY} | ||
| VAR ${empty_scalar_cont} | ||
| ... ${EMPTY} | ||
| VAR ${scalar} value | ||
| VAR @{list} item1 item2 | ||
| VAR &{dict} key=value | ||
| Log ${empty_scalar} | ||
|
|
||
| Keyword With Traditional VAR | ||
| [Documentation] Test with traditional Set Variable | ||
| ${empty} Set Variable | ||
| ${filled} Set Variable value | ||
| RETURN ${empty} | ||
|
|
||
| Keyword With Empty Assignment | ||
| ${var1} ${var2} ${var3} Get Multiple Values | ||
| Log Many ${var1} ${var2} ${var3} |
15 changes: 15 additions & 0 deletions
15
tests/formatter/formatters/ReplaceEmptyValues/expected/mixed_sections.robot
This file contains hidden or 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,15 @@ | ||
| *** Variables *** | ||
| ${VAR_EMPTY} ${EMPTY} | ||
| @{VAR_LIST} @{EMPTY} | ||
|
|
||
|
|
||
| *** Test Cases *** | ||
| Test Should Not Be Modified | ||
| VAR ${empty_in_test} | ||
| Log ${empty_in_test} | ||
|
|
||
|
|
||
| *** Keywords *** | ||
| Keyword With Empty Vars | ||
| VAR ${empty_in_kw} ${EMPTY} | ||
| Log ${empty_in_kw} |
21 changes: 21 additions & 0 deletions
21
tests/formatter/formatters/ReplaceEmptyValues/expected/testcases.robot
This file contains hidden or 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,21 @@ | ||
| *** Test Cases *** | ||
| Test With Empty Vars | ||
| VAR ${empty_scalar} ${EMPTY} | ||
| VAR @{empty_list} @{EMPTY} | ||
| VAR &{empty_dict} &{EMPTY} | ||
| VAR ${empty_scalar_cont} | ||
| ... ${EMPTY} | ||
| VAR ${scalar} value | ||
| Log ${empty_scalar} | ||
|
|
||
| Test With Scoped VAR | ||
| VAR ${empty_test} ${EMPTY} scope=TEST | ||
| VAR ${empty_suite} ${EMPTY} scope=SUITE | ||
| VAR ${empty_global} ${EMPTY} scope=GLOBAL | ||
| VAR @{empty_list} @{EMPTY} scope=TEST | ||
| Log ${empty_test} | ||
|
|
||
| Test Traditional Assignment | ||
| ${empty} Set Variable | ||
| ${filled} Set Variable value | ||
| Log ${empty} |
18 changes: 18 additions & 0 deletions
18
tests/formatter/formatters/ReplaceEmptyValues/source/all_sections.robot
This file contains hidden or 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,18 @@ | ||
| *** Variables *** | ||
| ${EMPTY_SCALAR} ${EMPTY} | ||
| @{EMPTY_LIST} @{EMPTY} | ||
| &{EMPTY_DICT} &{EMPTY} | ||
|
|
||
|
|
||
| *** Test Cases *** | ||
| Test With Empty Vars | ||
| VAR ${empty_in_test} | ||
| VAR @{empty_list_test} | ||
| Log ${empty_in_test} | ||
|
|
||
|
|
||
| *** Keywords *** | ||
| Keyword With Empty Vars | ||
| VAR ${empty_in_kw} | ||
| VAR &{empty_dict_kw} | ||
| Log ${empty_in_kw} |
21 changes: 21 additions & 0 deletions
21
tests/formatter/formatters/ReplaceEmptyValues/source/keywords.robot
This file contains hidden or 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,21 @@ | ||
| *** Keywords *** | ||
| Keyword With Empty Vars | ||
| VAR ${empty_scalar} | ||
| VAR @{empty_list} | ||
| VAR &{empty_dict} | ||
| VAR ${empty_scalar_cont} | ||
| ... | ||
| VAR ${scalar} value | ||
| VAR @{list} item1 item2 | ||
| VAR &{dict} key=value | ||
| Log ${empty_scalar} | ||
|
|
||
| Keyword With Traditional VAR | ||
| [Documentation] Test with traditional Set Variable | ||
| ${empty} Set Variable | ||
| ${filled} Set Variable value | ||
| RETURN ${empty} | ||
|
|
||
| Keyword With Empty Assignment | ||
| ${var1} ${var2} ${var3} Get Multiple Values | ||
| Log Many ${var1} ${var2} ${var3} |
15 changes: 15 additions & 0 deletions
15
tests/formatter/formatters/ReplaceEmptyValues/source/mixed_sections.robot
This file contains hidden or 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,15 @@ | ||
| *** Variables *** | ||
| ${VAR_EMPTY} ${EMPTY} | ||
| @{VAR_LIST} @{EMPTY} | ||
|
|
||
|
|
||
| *** Test Cases *** | ||
| Test Should Not Be Modified | ||
| VAR ${empty_in_test} | ||
| Log ${empty_in_test} | ||
|
|
||
|
|
||
| *** Keywords *** | ||
| Keyword With Empty Vars | ||
| VAR ${empty_in_kw} | ||
| Log ${empty_in_kw} |
21 changes: 21 additions & 0 deletions
21
tests/formatter/formatters/ReplaceEmptyValues/source/testcases.robot
This file contains hidden or 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,21 @@ | ||
| *** Test Cases *** | ||
| Test With Empty Vars | ||
| VAR ${empty_scalar} | ||
| VAR @{empty_list} | ||
| VAR &{empty_dict} | ||
| VAR ${empty_scalar_cont} | ||
| ... | ||
| VAR ${scalar} value | ||
| Log ${empty_scalar} | ||
|
|
||
| Test With Scoped VAR | ||
| VAR ${empty_test} scope=TEST | ||
| VAR ${empty_suite} scope=SUITE | ||
| VAR ${empty_global} scope=GLOBAL | ||
| VAR @{empty_list} scope=TEST | ||
| Log ${empty_test} | ||
|
|
||
| Test Traditional Assignment | ||
| ${empty} Set Variable | ||
| ${filled} Set Variable value | ||
| Log ${empty} |
4 changes: 4 additions & 0 deletions
4
tests/formatter/formatters/ReplaceEmptyValues/source/variables_only.robot
This file contains hidden or 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,4 @@ | ||
| *** Variables *** | ||
| ${EMPTY_VAR} ${EMPTY} | ||
| @{EMPTY_LIST} @{EMPTY} | ||
| &{EMPTY_DICT} &{EMPTY} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Var is None probably will never be true (because visit_Var is only handled by version which supports Var) but it may be required for mypy checker.