From c2c6a11893c3df7842640b9a58e1213ac6c637ba Mon Sep 17 00:00:00 2001 From: Bartlomiej Hirsz Date: Sun, 10 Mar 2024 15:21:05 +0100 Subject: [PATCH 1/2] Feat: Acknowledge file disablers in the whole comments section instead of only first line --- docs/releasenotes/unreleased/other.1.rst | 21 +++++++++++++++ robotidy/disablers.py | 12 ++++++--- .../NormalizeComments/expected/test.robot | 2 +- .../NormalizeComments/source/test.robot | 2 +- tests/utest/test_disablers.py | 2 ++ .../disablers/file_disabled_in_comments.robot | 26 +++++++++++++++++++ .../file_disabled_in_comments_no_header.robot | 26 +++++++++++++++++++ 7 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 docs/releasenotes/unreleased/other.1.rst create mode 100644 tests/utest/testdata/disablers/file_disabled_in_comments.robot create mode 100644 tests/utest/testdata/disablers/file_disabled_in_comments_no_header.robot 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..042516d8 100644 --- a/tests/utest/test_disablers.py +++ b/tests/utest/test_disablers.py @@ -52,6 +52,8 @@ 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_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_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 From 6a122cc7b2a10cd7e1f48327eff209b0123ab407 Mon Sep 17 00:00:00 2001 From: Bartlomiej Hirsz Date: Sun, 10 Mar 2024 15:23:54 +0100 Subject: [PATCH 2/2] Test: Add test for re-enabling file disabler --- tests/utest/test_disablers.py | 1 + .../file_disabled_enabled_in_comments.robot | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/utest/testdata/disablers/file_disabled_enabled_in_comments.robot diff --git a/tests/utest/test_disablers.py b/tests/utest/test_disablers.py index 042516d8..daa3c1e6 100644 --- a/tests/utest/test_disablers.py +++ b/tests/utest/test_disablers.py @@ -54,6 +54,7 @@ def test_is_node_disabled(check_start, check_end, start_line, end_line, lines, f ("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