Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features
- Add extra indent for arguments in Suite Setup/Teardown, Test Setup/Teardown in AlignSettingsSection [#155](https://github.com/MarketSquare/robotframework-tidy/issues/155)
- 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)

### Fixes
- Do not count documentation length when aligning all columns in settings section [#156](https://github.com/MarketSquare/robotframework-tidy/issues/156)
Expand Down
38 changes: 22 additions & 16 deletions docs/source/transformers/OrderSettingsSection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ run separately with::
robotidy --transform OrderSettingsSection src

Settings are grouped in following groups:

- documentation (Documentation, Metadata),
- imports (Library, Resource, Variables),
- settings (Suite Setup and Teardown, Test Setup and Teardown, Test Timeout, Test Template),
Expand All @@ -19,8 +20,9 @@ Settings are grouped in following groups:
Then ordered by groups (according to ``group_order = documentation,imports,settings,tags`` order). Every
group is separated by ``new_lines_between_groups = 1`` new lines.
Settings are grouped inside group. Default order can be modified through following parameters:

- ``documentation_order = documentation,metadata``
- ``imports_order = library,resource,variables``
- ``imports_order = preserved``
- ``settings_order = suite_setup,suite_teardown,test_setup,test_teardown,test_timeout,test_template``

.. tabs::
Expand Down Expand Up @@ -59,11 +61,11 @@ Settings are grouped inside group. Default order can be modified through followi
... another line
Metadata value param

Library Collections
Variables variables.py
Library Stuff
Library stuff.py WITH NAME alias
Library Collections
Resource robot.resource
Variables variables.py
Library stuff.py WITH NAME alias

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

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

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

.. tabs::

.. code-tab:: robotframework Default order

Library Collections
Library Stuff
Library stuff.py WITH NAME alias
Resource robot.resource
Variables variables.py
Suite Setup Suite Setup Keyword
Suite Teardown Suite Teardown Keyword
Test Timeout 1min

.. code-tab:: robotframework Configured order

Variables variables.py
Library Collections
Library Stuff
Library stuff.py WITH NAME alias
Resource robot.resource
Suite Teardown Suite Teardown Keyword
Suite Setup Suite Setup Keyword
Test Timeout 1min

By default order of imports is preserved. You can overwrite this behaviour::

robotidy --configure OrderSettingsSections:imports_order=library,resource,variables

You can also preserve order inside any group by passing ``preserved`` instead of setting names::

robotidy --configure OrderSettingsSections:tags=preserved

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

Default Tags tag

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

.. tabs::
Expand Down
44 changes: 33 additions & 11 deletions robotidy/transformers/OrderSettingsSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,35 @@ class OrderSettingsSection(ModelTransformer):
group is separated by ``new_lines_between_groups = 1`` new lines.
Settings are grouped inside group. Default order can be modified through following parameters:
- ``documentation_order = documentation,metadata``
- ``imports_order = library,resource,variables``
- ``imports_order = preserved``
- ``settings_order = suite_setup,suite_teardown,test_setup,test_teardown,test_timeout,test_template``

By default order of imports is preserved. You can overwrite this behaviour::

robotidy --configure OrderSettingsSections:imports_order=library,resource,variables

You can also preserve order inside any group by passing ``preserved`` instead of setting names::

robotidy --configure OrderSettingsSections:tags=preserved

Setting names omitted from custom order will be removed from the file. In following example we are missing metadata
therefore all metadata will be removed::

robotidy --configure OrderSettingsSection:documentation_order=documentation

Libraries are grouped into built in libraries and custom libraries.
Parsing errors (such as Resources instead of Resource, duplicated settings) are moved to the end of section.

See https://robotidy.readthedocs.io/en/latest/transformers/OrderSettingsSection.html for more examples.
"""
def __init__(self, new_lines_between_groups: int = 1, group_order: str = None, documentation_order: str = None,
imports_order: str = None, settings_order: str = None, tags_order: str = None):
imports_order: str = 'preserved', settings_order: str = None, tags_order: str = None):
self.last_section = None
self.disabled_group = set()
self.new_lines_between_groups = new_lines_between_groups
self.group_order = self.parse_group_order(group_order)
self.documentation_order = self.parse_order_in_group(
'documentation',
documentation_order,
(
Token.DOCUMENTATION,
Expand All @@ -53,6 +65,7 @@ def __init__(self, new_lines_between_groups: int = 1, group_order: str = None, d
}
)
self.imports_order = self.parse_order_in_group(
'imports',
imports_order,
(
Token.LIBRARY,
Expand All @@ -66,6 +79,7 @@ def __init__(self, new_lines_between_groups: int = 1, group_order: str = None, d
}
)
self.settings_order = self.parse_order_in_group(
'settings',
settings_order,
(
Token.SUITE_SETUP,
Expand All @@ -85,6 +99,7 @@ def __init__(self, new_lines_between_groups: int = 1, group_order: str = None, d
}
)
self.tags_order = self.parse_order_in_group(
'tags',
tags_order,
(
Token.FORCE_TAGS,
Expand Down Expand Up @@ -117,12 +132,14 @@ def parse_group_order(order):
)
return parts

@staticmethod
def parse_order_in_group(order, default, mapping):
def parse_order_in_group(self, name, order, default, mapping):
if order is None:
return default
if not order:
return []
if order == 'preserved':
self.disabled_group.add(name)
return default
parts = order.lower().split(',')
try:
return [mapping[part] for part in parts]
Expand Down Expand Up @@ -176,14 +193,19 @@ def visit_SettingSection(self, node): # noqa
last_index = len(order_of_groups) - 1
for index, group in enumerate(order_of_groups):
unordered = groups[group]
if group == 'imports':
unordered = self.sort_builtin_libs(unordered)
order = group_map[group]
for token_type in order:
if group in self.disabled_group:
for comment_lines, child in unordered:
if child.type == token_type:
new_body.extend(comment_lines)
new_body.append(child)
new_body.extend(comment_lines)
new_body.append(child)
else:
if group == 'imports':
unordered = self.sort_builtin_libs(unordered)
order = group_map[group]
for token_type in order:
for comment_lines, child in unordered:
if child.type == token_type:
new_body.extend(comment_lines)
new_body.append(child)
if index != last_index:
new_body.extend([empty_line] * self.new_lines_between_groups)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*** Settings ***
Library library.py
Resource resource.robot
Library library.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Documentation doc # this is comment
... another line
Metadata value param

Library Collections
Variables variables.py
Library Stuff
Library stuff.py WITH NAME alias
Library Collections
Resource robot.resource
Variables variables.py
Library stuff.py WITH NAME alias

Suite Setup Keyword
# We all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Documentation doc # this is comment
... another line
Metadata value param
Library Collections
Variables variables.py
Library Stuff
Library stuff.py WITH NAME alias
Library Collections
Resource robot.resource
Variables variables.py
Library stuff.py WITH NAME alias
Suite Setup Keyword
# We all
# are commenting Suite Teardown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Documentation doc # this is comment
Metadata value param


Library Collections
Variables variables.py
Library Stuff
Library stuff.py WITH NAME alias
Library Collections
Resource robot.resource
Variables variables.py
Library stuff.py WITH NAME alias


Suite Setup Keyword
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Documentation doc # this is comment
... another line
Metadata value param

Library Collections
Variables variables.py
Library Stuff
Library stuff.py WITH NAME alias
Library Collections
Resource robot.resource
Variables variables.py
Library stuff.py WITH NAME alias

Suite Setup Keyword
# We all
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
*** Settings ***
Force Tags tag
... tag
Default Tags 1

Documentation doc # this is comment
... another line
Metadata value param

Library Collections
Library Stuff
Library stuff.py WITH NAME alias
Resource robot.resource
Variables variables.py

Suite Setup Keyword
# We all
# are commenting Suite Teardown
Suite Teardown Keyword2
# i want to be keep together with Test Setup
Test Setup Keyword
Test Timeout 1min

*** Keywords ***
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Documentation doc # this is comment
... another line
Metadata value param

Library Collections
Variables variables.py
Library Stuff
Library stuff.py WITH NAME alias
Library Collections
Resource robot.resource
Variables variables.py
Library stuff.py WITH NAME alias

Suite Setup Keyword
# We all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def test_custom_group_order(self):
config=':group_order=tags,documentation,imports,settings'
)

def test_custom_group_order_import_ordered(self):
run_tidy_and_compare(
self.TRANSFORMER_NAME,
source='test.robot',
expected='test_group_order_import_ordered.robot',
config=':group_order=tags,documentation,imports,settings:imports_order=library,resource,variables'
)

def test_missing_group_from_param(self):
run_tidy_and_compare(
self.TRANSFORMER_NAME,
Expand Down