Skip to content

Commit b663ce2

Browse files
bhirszmnojek
andauthored
Allow to disable and enable file starting from the first line (#499)
* Allow to disable and enable file starting from the first line Co-authored-by: Mateusz Nojek <matnojek@gmail.com>
1 parent 47c97e1 commit b663ce2

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

docs/releasenotes/4.0.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,8 @@ Bugs
274274

275275
- Keyword name will no longer be prefixed with continuation marks (``...``) if name was longer than line length limit (#494)
276276
- It is now safe to use Tasks sections with ``MergeAndOrderSections`` transformer with Robot Framework 6.0+ (#490)
277+
- It is now possible to use disabler in the first line and enable it later (previously the whole file was always ignored) (#479)::
278+
279+
# robotidy: off
280+
# robotidy: on
281+
*** Settings ***

robotidy/disablers.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ def __init__(self, start_line, end_line, file_end):
6868
self.lines = []
6969
self.disabled_headers = set()
7070

71+
@property
72+
def file_disabled(self):
73+
"""Check if file is disabled. Whole file is only disabled if the first line contains one line disabler."""
74+
if not self.lines:
75+
return False
76+
return self.lines[0] == (1, 1)
77+
7178
def add_disabler(self, start_line, end_line):
7279
self.lines.append((start_line, end_line))
7380

@@ -128,9 +135,9 @@ def visit_File(self, node): # noqa
128135
self.disablers = DisabledLines(self.start_line, self.end_line, node.end_lineno)
129136
self.disablers.parse_global_disablers()
130137
self.stack = []
131-
self.file_disabled = False
132138
self.generic_visit(node)
133139
self.disablers.sort_disablers()
140+
self.file_disabled = self.disablers.file_disabled
134141

135142
def visit_SectionHeader(self, node): # noqa
136143
for comment in node.get_tokens(Token.COMMENT):
@@ -143,26 +150,20 @@ def visit_SectionHeader(self, node): # noqa
143150
def visit_TestCase(self, node): # noqa
144151
self.stack.append(0)
145152
self.generic_visit(node)
146-
if self.file_disabled: # stop visiting if whole file is disabled
147-
return
148153
self.close_disabler(node.end_lineno)
149154

150155
def visit_Try(self, node): # noqa
151156
self.generic_visit(node.header)
152157
self.stack.append(0)
153158
for statement in node.body:
154159
self.visit(statement)
155-
if self.file_disabled: # stop visiting if whole file is disabled
156-
return
157160
self.close_disabler(node.end_lineno)
158161
tail = node
159162
while tail.next:
160163
self.generic_visit(tail.header)
161164
self.stack.append(0)
162165
for statement in tail.body:
163166
self.visit(statement)
164-
if self.file_disabled: # stop visiting if whole file is disabled
165-
return
166167
end_line = tail.next.lineno - 1 if tail.next else tail.end_lineno
167168
self.close_disabler(end_line=end_line)
168169
tail = tail.next
@@ -181,11 +182,8 @@ def visit_Statement(self, node): # noqa
181182
return
182183
self.disablers.add_disabler(self.stack[index], node.lineno)
183184
self.stack[index] = 0
184-
else:
185-
if node.lineno == 1 and index == 0:
186-
self.file_disabled = True
187-
elif not self.stack[index]:
188-
self.stack[index] = node.lineno
185+
elif not self.stack[index]:
186+
self.stack[index] = node.lineno
189187
else:
190188
# inline disabler
191189
if self.any_disabler_open():

tests/utest/test_disablers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import re
21
from pathlib import Path
32
from unittest.mock import Mock
43

@@ -52,7 +51,8 @@ def test_is_node_disabled(check_start, check_end, start_line, end_line, lines, f
5251
@pytest.mark.parametrize(
5352
"test_file, expected_lines, file_disabled, rf_version",
5453
[
55-
("file_disabled.robot", [(14, 15)], True, 4),
54+
("file_disabled.robot", [(1, 1), (14, 15)], True, 4),
55+
("file_disabled_and_enabled.robot", [(1, 2), (15, 16)], False, 4),
5656
("test.robot", [(13, 14), (25, 37), (30, 33), (40, 41), (46, 48), (57, 58), (67, 67)], False, 5),
5757
("open_disabler_in_section.robot", [(5, 8), (13, 15), (20, 23)], False, 4),
5858
("empty.robot", [], False, 4),
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# robotidy: off
2+
# robotidy: on
3+
*** Settings ***
4+
Library SeleniumLibrary
5+
6+
Metadata Key Value
7+
8+
*** Test Cases ***
9+
Test
10+
[Documentation] This is doc
11+
Step
12+
FOR ${var} IN RANGE 10
13+
IF $condition
14+
WHILE ${arg}
15+
${return} Keyword ${value}
16+
... ${other_value} # robotidy: off
17+
END
18+
ELSE IF
19+
Step ${arg}
20+
... value
21+
Step 2
22+
# comment
23+
END
24+
END

0 commit comments

Comments
 (0)