Skip to content

Commit 0a88c24

Browse files
authored
Preserve order of imports in OrderSettingsSection (#168)
* preserve order of imports in OrderSettingsSection * update changelog and docs
1 parent 0aee1f6 commit 0a88c24

File tree

11 files changed

+104
-43
lines changed

11 files changed

+104
-43
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66
- Add extra indent for arguments in Suite Setup/Teardown, Test Setup/Teardown in AlignSettingsSection [#155](https://github.com/MarketSquare/robotframework-tidy/issues/155)
7+
- OrderSettingsSection will now preserve order of imports. It can be configured to work as before and other settings order can be also preserved [#167](https://github.com/MarketSquare/robotframework-tidy/issues/167)
78

89
### Fixes
910
- Do not count documentation length when aligning all columns in settings section [#156](https://github.com/MarketSquare/robotframework-tidy/issues/156)

docs/source/transformers/OrderSettingsSection.rst

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ run separately with::
1111
robotidy --transform OrderSettingsSection src
1212

1313
Settings are grouped in following groups:
14+
1415
- documentation (Documentation, Metadata),
1516
- imports (Library, Resource, Variables),
1617
- settings (Suite Setup and Teardown, Test Setup and Teardown, Test Timeout, Test Template),
@@ -19,8 +20,9 @@ Settings are grouped in following groups:
1920
Then ordered by groups (according to ``group_order = documentation,imports,settings,tags`` order). Every
2021
group is separated by ``new_lines_between_groups = 1`` new lines.
2122
Settings are grouped inside group. Default order can be modified through following parameters:
23+
2224
- ``documentation_order = documentation,metadata``
23-
- ``imports_order = library,resource,variables``
25+
- ``imports_order = preserved``
2426
- ``settings_order = suite_setup,suite_teardown,test_setup,test_teardown,test_timeout,test_template``
2527

2628
.. tabs::
@@ -59,11 +61,11 @@ Settings are grouped inside group. Default order can be modified through followi
5961
... another line
6062
Metadata value param
6163

62-
Library Collections
64+
Variables variables.py
6365
Library Stuff
64-
Library stuff.py WITH NAME alias
66+
Library Collections
6567
Resource robot.resource
66-
Variables variables.py
68+
Library stuff.py WITH NAME alias
6769

6870
Suite Setup Keyword
6971
Suite Teardown Keyword2
@@ -121,25 +123,29 @@ Using the same example with non default group order we will move tags from end t
121123

122124
Order of setting inside common group can also be changed::
123125

124-
robotidy --configure OrderSettingsSection:imports_order=variables,library,resource, src
126+
robotidy --configure OrderSettingsSection:settings_order=suite_teardown,suite_setup,test_setup,test_teardown,test_timeout,test_template src
125127

126128
.. tabs::
127129

128130
.. code-tab:: robotframework Default order
129131

130-
Library Collections
131-
Library Stuff
132-
Library stuff.py WITH NAME alias
133-
Resource robot.resource
134-
Variables variables.py
132+
Suite Setup Suite Setup Keyword
133+
Suite Teardown Suite Teardown Keyword
134+
Test Timeout 1min
135135

136136
.. code-tab:: robotframework Configured order
137137

138-
Variables variables.py
139-
Library Collections
140-
Library Stuff
141-
Library stuff.py WITH NAME alias
142-
Resource robot.resource
138+
Suite Teardown Suite Teardown Keyword
139+
Suite Setup Suite Setup Keyword
140+
Test Timeout 1min
141+
142+
By default order of imports is preserved. You can overwrite this behaviour::
143+
144+
robotidy --configure OrderSettingsSections:imports_order=library,resource,variables
145+
146+
You can also preserve order inside any group by passing ``preserved`` instead of setting names::
147+
148+
robotidy --configure OrderSettingsSections:tags=preserved
143149

144150
Setting names omitted from custom order will be removed from the file. In following example we are missing metadata
145151
therefore all metadata will be removed::
@@ -190,7 +196,7 @@ Group of settings are separated by ``new_lines_between_groups = 1`` new lines. I
190196

191197
Default Tags tag
192198

193-
Libraries are grouped into built in libraries and custom libraries.
199+
If you're not preserving the default order of libraries they will be grouped into built in libraries and custom libraries.
194200
Parsing errors (such as Resources instead of Resource, duplicated settings) are moved to the end of section.
195201

196202
.. tabs::

robotidy/transformers/OrderSettingsSection.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,35 @@ class OrderSettingsSection(ModelTransformer):
2525
group is separated by ``new_lines_between_groups = 1`` new lines.
2626
Settings are grouped inside group. Default order can be modified through following parameters:
2727
- ``documentation_order = documentation,metadata``
28-
- ``imports_order = library,resource,variables``
28+
- ``imports_order = preserved``
2929
- ``settings_order = suite_setup,suite_teardown,test_setup,test_teardown,test_timeout,test_template``
3030
31+
By default order of imports is preserved. You can overwrite this behaviour::
32+
33+
robotidy --configure OrderSettingsSections:imports_order=library,resource,variables
34+
35+
You can also preserve order inside any group by passing ``preserved`` instead of setting names::
36+
37+
robotidy --configure OrderSettingsSections:tags=preserved
38+
3139
Setting names omitted from custom order will be removed from the file. In following example we are missing metadata
3240
therefore all metadata will be removed::
3341
3442
robotidy --configure OrderSettingsSection:documentation_order=documentation
3543
3644
Libraries are grouped into built in libraries and custom libraries.
3745
Parsing errors (such as Resources instead of Resource, duplicated settings) are moved to the end of section.
46+
47+
See https://robotidy.readthedocs.io/en/latest/transformers/OrderSettingsSection.html for more examples.
3848
"""
3949
def __init__(self, new_lines_between_groups: int = 1, group_order: str = None, documentation_order: str = None,
40-
imports_order: str = None, settings_order: str = None, tags_order: str = None):
50+
imports_order: str = 'preserved', settings_order: str = None, tags_order: str = None):
4151
self.last_section = None
52+
self.disabled_group = set()
4253
self.new_lines_between_groups = new_lines_between_groups
4354
self.group_order = self.parse_group_order(group_order)
4455
self.documentation_order = self.parse_order_in_group(
56+
'documentation',
4557
documentation_order,
4658
(
4759
Token.DOCUMENTATION,
@@ -53,6 +65,7 @@ def __init__(self, new_lines_between_groups: int = 1, group_order: str = None, d
5365
}
5466
)
5567
self.imports_order = self.parse_order_in_group(
68+
'imports',
5669
imports_order,
5770
(
5871
Token.LIBRARY,
@@ -66,6 +79,7 @@ def __init__(self, new_lines_between_groups: int = 1, group_order: str = None, d
6679
}
6780
)
6881
self.settings_order = self.parse_order_in_group(
82+
'settings',
6983
settings_order,
7084
(
7185
Token.SUITE_SETUP,
@@ -85,6 +99,7 @@ def __init__(self, new_lines_between_groups: int = 1, group_order: str = None, d
8599
}
86100
)
87101
self.tags_order = self.parse_order_in_group(
102+
'tags',
88103
tags_order,
89104
(
90105
Token.FORCE_TAGS,
@@ -117,12 +132,14 @@ def parse_group_order(order):
117132
)
118133
return parts
119134

120-
@staticmethod
121-
def parse_order_in_group(order, default, mapping):
135+
def parse_order_in_group(self, name, order, default, mapping):
122136
if order is None:
123137
return default
124138
if not order:
125139
return []
140+
if order == 'preserved':
141+
self.disabled_group.add(name)
142+
return default
126143
parts = order.lower().split(',')
127144
try:
128145
return [mapping[part] for part in parts]
@@ -176,14 +193,19 @@ def visit_SettingSection(self, node): # noqa
176193
last_index = len(order_of_groups) - 1
177194
for index, group in enumerate(order_of_groups):
178195
unordered = groups[group]
179-
if group == 'imports':
180-
unordered = self.sort_builtin_libs(unordered)
181-
order = group_map[group]
182-
for token_type in order:
196+
if group in self.disabled_group:
183197
for comment_lines, child in unordered:
184-
if child.type == token_type:
185-
new_body.extend(comment_lines)
186-
new_body.append(child)
198+
new_body.extend(comment_lines)
199+
new_body.append(child)
200+
else:
201+
if group == 'imports':
202+
unordered = self.sort_builtin_libs(unordered)
203+
order = group_map[group]
204+
for token_type in order:
205+
for comment_lines, child in unordered:
206+
if child.type == token_type:
207+
new_body.extend(comment_lines)
208+
new_body.append(child)
187209
if index != last_index:
188210
new_body.extend([empty_line] * self.new_lines_between_groups)
189211

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
*** Settings ***
2-
Library library.py
32
Resource resource.robot
3+
Library library.py

tests/atest/transformers/OrderSettingsSection/expected/test.robot

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ Documentation doc # this is comment
33
... another line
44
Metadata value param
55

6-
Library Collections
6+
Variables variables.py
77
Library Stuff
8-
Library stuff.py WITH NAME alias
8+
Library Collections
99
Resource robot.resource
10-
Variables variables.py
10+
Library stuff.py WITH NAME alias
1111

1212
Suite Setup Keyword
1313
# We all

tests/atest/transformers/OrderSettingsSection/expected/test_0_newline.robot

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
Documentation doc # this is comment
33
... another line
44
Metadata value param
5-
Library Collections
5+
Variables variables.py
66
Library Stuff
7-
Library stuff.py WITH NAME alias
7+
Library Collections
88
Resource robot.resource
9-
Variables variables.py
9+
Library stuff.py WITH NAME alias
1010
Suite Setup Keyword
1111
# We all
1212
# are commenting Suite Teardown

tests/atest/transformers/OrderSettingsSection/expected/test_2_newline.robot

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Documentation doc # this is comment
44
Metadata value param
55

66

7-
Library Collections
7+
Variables variables.py
88
Library Stuff
9-
Library stuff.py WITH NAME alias
9+
Library Collections
1010
Resource robot.resource
11-
Variables variables.py
11+
Library stuff.py WITH NAME alias
1212

1313

1414
Suite Setup Keyword

tests/atest/transformers/OrderSettingsSection/expected/test_group_order.robot

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ Documentation doc # this is comment
77
... another line
88
Metadata value param
99

10-
Library Collections
10+
Variables variables.py
1111
Library Stuff
12-
Library stuff.py WITH NAME alias
12+
Library Collections
1313
Resource robot.resource
14-
Variables variables.py
14+
Library stuff.py WITH NAME alias
1515

1616
Suite Setup Keyword
1717
# We all
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
*** Settings ***
2+
Force Tags tag
3+
... tag
4+
Default Tags 1
5+
6+
Documentation doc # this is comment
7+
... another line
8+
Metadata value param
9+
10+
Library Collections
11+
Library Stuff
12+
Library stuff.py WITH NAME alias
13+
Resource robot.resource
14+
Variables variables.py
15+
16+
Suite Setup Keyword
17+
# We all
18+
# are commenting Suite Teardown
19+
Suite Teardown Keyword2
20+
# i want to be keep together with Test Setup
21+
Test Setup Keyword
22+
Test Timeout 1min
23+
24+
*** Keywords ***

tests/atest/transformers/OrderSettingsSection/expected/test_missing_group_from_param.robot

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ Documentation doc # this is comment
33
... another line
44
Metadata value param
55

6-
Library Collections
6+
Variables variables.py
77
Library Stuff
8-
Library stuff.py WITH NAME alias
8+
Library Collections
99
Resource robot.resource
10-
Variables variables.py
10+
Library stuff.py WITH NAME alias
1111

1212
Suite Setup Keyword
1313
# We all

0 commit comments

Comments
 (0)