diff --git a/docs/releasenotes/unreleased/other.1.rst b/docs/releasenotes/unreleased/other.1.rst new file mode 100644 index 00000000..01dc597b --- /dev/null +++ b/docs/releasenotes/unreleased/other.1.rst @@ -0,0 +1,21 @@ +File disablers in Comments section (#587) +----------------------------------------- + +Previously file formatting disablers were only recognized if they were placed in the first line of file:: + + # robotidy: off + *** Settings *** + +Now Robotidy will acknowledge any disabler in the first comment section (with or without header):: + + Following line disables formatting of this file with Robotidy + # robotidy: off + + *** Settings *** + +Or:: + + *** Comments *** + # robotidy: off + + *** Test Cases *** diff --git a/robotidy/disablers.py b/robotidy/disablers.py index 3014fb5e..786f800f 100644 --- a/robotidy/disablers.py +++ b/robotidy/disablers.py @@ -1,7 +1,7 @@ import functools import re -from robot.api.parsing import Comment, ModelVisitor, Token +from robot.api.parsing import Comment, CommentSection, ModelVisitor, Token def skip_if_disabled(func): @@ -120,6 +120,7 @@ def __init__(self, start_line, end_line): self.disabler_pattern = re.compile(r"\s*#\s?robotidy:\s?(?Pon|off)") self.stack = [] self.file_disabled = False + self.file_level_disablers = False def any_disabler_open(self): return any(disabler for disabler in self.stack) @@ -132,15 +133,20 @@ def get_disabler(self, comment): def close_disabler(self, end_line): disabler = self.stack.pop() if disabler: + if self.file_level_disablers: + self.file_disabled = True self.disablers.add_disabler(disabler, end_line) def visit_File(self, node): # noqa + self.file_level_disablers = False self.disablers = DisabledLines(self.start_line, self.end_line, node.end_lineno) self.disablers.parse_global_disablers() self.stack = [] - self.generic_visit(node) + for index, section in enumerate(node.sections): + self.file_level_disablers = index == 0 and isinstance(section, CommentSection) + self.visit_Section(section) self.disablers.sort_disablers() - self.file_disabled = self.disablers.file_disabled + self.file_disabled = self.file_disabled or self.disablers.file_disabled def visit_SectionHeader(self, node): # noqa for comment in node.get_tokens(Token.COMMENT): diff --git a/tests/atest/transformers/NormalizeComments/expected/test.robot b/tests/atest/transformers/NormalizeComments/expected/test.robot index c848f744..62d2e7b7 100644 --- a/tests/atest/transformers/NormalizeComments/expected/test.robot +++ b/tests/atest/transformers/NormalizeComments/expected/test.robot @@ -4,7 +4,7 @@ # comment # comment # robotidy: off -# robotidy: on +# robotidy: on *** Settings *** diff --git a/tests/atest/transformers/NormalizeComments/source/test.robot b/tests/atest/transformers/NormalizeComments/source/test.robot index 5e490fe6..9aed47a8 100644 --- a/tests/atest/transformers/NormalizeComments/source/test.robot +++ b/tests/atest/transformers/NormalizeComments/source/test.robot @@ -4,7 +4,7 @@ # comment # comment # robotidy: off -#robotidy: on +# robotidy: on *** Settings *** diff --git a/tests/utest/test_disablers.py b/tests/utest/test_disablers.py index d158faed..daa3c1e6 100644 --- a/tests/utest/test_disablers.py +++ b/tests/utest/test_disablers.py @@ -52,6 +52,9 @@ def test_is_node_disabled(check_start, check_end, start_line, end_line, lines, f "test_file, expected_lines, file_disabled, rf_version", [ ("file_disabled.robot", [(1, 1), (14, 15)], True, 4), + ("file_disabled_in_comments.robot", [(2, 4), (17, 18)], True, 4), + ("file_disabled_in_comments_no_header.robot", [(2, 4), (17, 18)], True, 4), + ("file_disabled_enabled_in_comments.robot", [(2, 6), (21, 22)], False, 4), ("file_disabled_and_enabled.robot", [(1, 2), (15, 16)], False, 4), ("test.robot", [(13, 14), (25, 37), (30, 33), (40, 41), (46, 48), (57, 58), (67, 67)], False, 5), ("open_disabler_in_section.robot", [(5, 8), (13, 15), (20, 23)], False, 4), diff --git a/tests/utest/testdata/disablers/file_disabled_enabled_in_comments.robot b/tests/utest/testdata/disablers/file_disabled_enabled_in_comments.robot new file mode 100644 index 00000000..706aa460 --- /dev/null +++ b/tests/utest/testdata/disablers/file_disabled_enabled_in_comments.robot @@ -0,0 +1,30 @@ +*** Comments *** +# robotidy: off + +Some comments that should not be formatted + +# robotidy: on + + +*** Settings *** +Library SeleniumLibrary + +Metadata Key Value + +*** Test Cases *** +Test + [Documentation] This is doc + Step + FOR ${var} IN RANGE 10 + IF $condition + WHILE ${arg} + ${return} Keyword ${value} + ... ${other_value} # robotidy: off + END + ELSE IF + Step ${arg} + ... value + Step 2 + # comment + END + END diff --git a/tests/utest/testdata/disablers/file_disabled_in_comments.robot b/tests/utest/testdata/disablers/file_disabled_in_comments.robot new file mode 100644 index 00000000..02a0936f --- /dev/null +++ b/tests/utest/testdata/disablers/file_disabled_in_comments.robot @@ -0,0 +1,26 @@ +*** Comments *** +# robotidy: off + + +*** Settings *** +Library SeleniumLibrary + +Metadata Key Value + +*** Test Cases *** +Test + [Documentation] This is doc + Step + FOR ${var} IN RANGE 10 + IF $condition + WHILE ${arg} + ${return} Keyword ${value} + ... ${other_value} # robotidy: off + END + ELSE IF + Step ${arg} + ... value + Step 2 + # comment + END + END diff --git a/tests/utest/testdata/disablers/file_disabled_in_comments_no_header.robot b/tests/utest/testdata/disablers/file_disabled_in_comments_no_header.robot new file mode 100644 index 00000000..eefc6c88 --- /dev/null +++ b/tests/utest/testdata/disablers/file_disabled_in_comments_no_header.robot @@ -0,0 +1,26 @@ + +# robotidy: off + + +*** Settings *** +Library SeleniumLibrary + +Metadata Key Value + +*** Test Cases *** +Test + [Documentation] This is doc + Step + FOR ${var} IN RANGE 10 + IF $condition + WHILE ${arg} + ${return} Keyword ${value} + ... ${other_value} # robotidy: off + END + ELSE IF + Step ${arg} + ... value + Step 2 + # comment + END + END