Skip to content

Commit

Permalink
Add [Setup] to keyword order (#691)
Browse files Browse the repository at this point in the history
* Add [Setup] to keyword order

* Update OrderSettings documentation

* Change order of Timeout
  • Loading branch information
bhirsz authored May 14, 2024
1 parent 6409873 commit 66bdb73
Show file tree
Hide file tree
Showing 13 changed files with 499 additions and 16 deletions.
15 changes: 15 additions & 0 deletions docs/releasenotes/unreleased/transformers.3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Timeout] and [Setup] order for keywords in OrderSettings (#690)
-----------------------------------------------------------------

Default order of keyword settings in ``OrderSettings`` transformer was modified. Robot Framework 7.0 added ``[Setup]``
to keywords (which wasn't supported by Robotidy until now). ``[Timeout]`` order was also changed.

Old default order was ``documentation,tags,timeout,arguments`` and new order is
``documentation,tags,arguments,timeout,setup``.

``[Timeout]`` order was changed to follow Robot Framework Style Guide recommendation.

If you are using ``OrderSettings`` with custom order, this change requires to add ``setup`` to your order.

Note that if you're using ``[Setup]`` with keywords in your code (supported in RF from 7.0) but run Robotidy with older
version (pre 7.0) it will order ``[Setup]`` like a keyword call - essentially ignoring its order.
82 changes: 76 additions & 6 deletions docs/source/transformers/OrderSettings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,78 @@
OrderSettings
================================

Order settings like ``[Arguments]``, ``[Setup]``, ``[Return]`` inside Keywords and Test Cases.
Order settings like ``[Arguments]``, ``[Setup]``, ``[Tags]`` inside Keywords and Test Cases.

.. |TRANSFORMERNAME| replace:: OrderSettings
.. include:: enabled_hint.txt

Keyword settings ``[Documentation]``, ``[Tags]``, ``[Timeout]``, ``[Arguments]`` are put before keyword body and
settings like ``[Teardown]``, ``[Return]`` are moved to the end of keyword.
Test case settings ``[Documentation]``, ``[Tags]``, ``[Template]``, ``[Timeout]``, ``[Setup]`` are put before test case
body and ``[Teardown]`` is moved to the end of test case.

Keyword settings ``[Documentation]``, ``[Tags]``, ``[Timeout]``, ``[Arguments]``, ``[Setup]`` are put before keyword
body and settings like ``[Teardown]``, ``[Return]`` are moved to the end of keyword.

.. tab-set::

.. tab-item:: Before

.. code:: robotframework
*** Test Cases ***
Test
[Setup] Setup
[Teardown] Teardown
[Documentation] Test documentation.
[Tags] tags
[Template] Template
[Timeout] 60 min
Test Step
*** Keywords ***
Keyword
[Teardown] Keyword
[Return] ${value}
[Arguments] ${arg}
[Documentation] this is
... doc
[Setup] Setup
[Tags] sanity
Pass
.. tab-item:: After

.. code:: robotframework
*** Test Cases ***
Test
[Documentation] Test documentation.
[Tags] tags
[Template] Template
[Timeout] 60 min
[Setup] Setup
Test Step
[Teardown] Teardown
*** Keywords ***
Keyword
[Documentation] this is
... doc
[Tags] sanity
[Arguments] ${arg}
[Setup] Setup
Pass
[Teardown] Keyword
[Return] ${value}
Test case settings ``[Documentation]``, ``[Tags]``, ``[Template]``, ``[Timeout]``, ``[Setup]`` are put before test case body and
``[Teardown]`` is moved to the end of test case.
Configure order of the settings
----------------------------------

Default order can be changed using following parameters:

- ``keyword_before = documentation,tags,timeout,arguments``
- ``keyword_before = documentation,tags,arguments,timeout,setup``
- ``keyword_after = teardown,return``
- ``test_before = documentation,tags,template,timeout,setup``
- ``test_after = teardown``
Expand All @@ -58,6 +83,9 @@ For example::

robotidy --configure OrderSettings:test_before=setup,teardown:test_after=documentation,tags

It is not required to overwrite all orders - for example configuring only ``test_before`` and ``test_after`` keeps
keyword order as default.

.. tab-set::

.. tab-item:: Before
Expand All @@ -75,6 +103,18 @@ For example::
[Setup] Setup # comment
Keyword2
*** Keywords ***
Keyword
[Documentation] this is
... doc
[Tags] sanity
[Arguments] ${arg}
[Setup] Setup
Pass
[Teardown] Keyword
[Return] ${value}
.. tab-item:: After

.. code:: robotframework
Expand All @@ -90,6 +130,18 @@ For example::
[Tags]
... tag
*** Keywords ***
Keyword
[Documentation] this is
... doc
[Tags] sanity
[Arguments] ${arg}
[Setup] Setup
Pass
[Teardown] Keyword
[Return] ${value}
Not all settings names need to be passed to given parameter. Missing setting names are not ordered. Example::

robotidy --configure OrderSettings:keyword_before=:keyword_after=
Expand All @@ -107,6 +159,24 @@ We need to overwrite both orders::

robotidy --configure OrderSettings:test_before=teardown:test_after=

Splitting configuration
-----------------------

Robotidy combines split configuration. It is possible to configure the same transformer in multiple CLI commands or
configuration entries::

robotidy --configure OrderSettings:keyword_before=documentation,tags,timeout,arguments,setup --configure OrderSettings:keyword_after=teardown,return

Configuration files can also contain spaces for better readability.

.. code-block:: toml
[tool.robotidy]
configure = [
"OrderSettings: keyword_before = documentation, tags, timeout, arguments, setup",
"OrderSettings: keyword_after = teardown, return",
]
Settings comments
---------------------

Expand Down
5 changes: 3 additions & 2 deletions robotidy/transformers/OrderSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ class OrderSettings(Transformer):
KEYWORD_SETTINGS = {
"documentation": Token.DOCUMENTATION,
"tags": Token.TAGS,
"timeout": Token.TIMEOUT,
"arguments": Token.ARGUMENTS,
"timeout": Token.TIMEOUT,
"setup": Token.SETUP,
"return": Token.RETURN,
"teardown": Token.TEARDOWN,
}
Expand All @@ -96,7 +97,7 @@ class OrderSettings(Transformer):

def __init__(
self,
keyword_before: str = "documentation,tags,timeout,arguments",
keyword_before: str = "documentation,tags,arguments,timeout,setup",
keyword_after: str = "teardown,return",
test_before: str = "documentation,tags,template,timeout,setup",
test_after: str = "teardown",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Keyword
[Tags] sanity
[Arguments] ${arg}
[Teardown] Keyword
[Setup] Setup
[Return] ${value}

Another Keyword ${var}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
*** Test Cases ***
Test case 1
Keyword
Keyword
[Documentation] this is
... doc
[Tags]
... tag
[Setup] Setup # comment
[Teardown] Teardown

Test case 2
Keyword
[Timeout] timeout2 # this is error because it is duplicate
[Template] Template
[Timeout] timeout

Test case with comment at the end
[Teardown] Keyword
# comment

# comment

Test case 3
Golden Keyword

Test case 4
Keyword1
# comment1
Keyword2
# comment2
Keyword3
[Teardown] teardown

Test case 5 # comment1
Keyword1
# comment2
Keyword2
# comment3
Keyword3
[Documentation] this is
[Teardown] teardown

*** Keywords ***
Keyword
Keyword
No Operation
IF ${condition}
Log ${stuff}
END
FOR ${var} IN 1 2
Log ${var}
END
Pass
[Setup] Setup
[Documentation] this is
... doc
[Tags] sanity
[Arguments] ${arg}
[Teardown] Keyword
[Return] ${value}

Another Keyword ${var}
No Operation
[Timeout]

Keyword With Comment
Keyword
[Return] ${value}
# I am comment

Keyword With Empty Line And Comment
Keyword
[Return] ${value}

# I am comment in new line

Another Keyword
No Operation
# innocent comment

Comment Before setting
Keyword
# I want to be here
[Return] ${value}

Return first and comment last
Keyword
[Return] stuff
# I want to be here

Comment on the same line # comment
[Documentation] this is
... doc
# what will happen with me?
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Keyword
... doc
[Tags] sanity
[Arguments] ${arg}
[Setup] Setup
Keyword
No Operation
IF ${condition}
Expand Down
Loading

0 comments on commit 66bdb73

Please sign in to comment.