Skip to content

Commit 6dd688c

Browse files
authored
Allow to disable selected transformers (#171)
* add missing lexer * allow to disable selected transformers * fix lexer name
1 parent 5b0c8e5 commit 6dd688c

File tree

6 files changed

+69
-32
lines changed

6 files changed

+69
-32
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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)
77
- 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)
8+
- Allow to disable selected transformers [#170](https://github.com/MarketSquare/robotframework-tidy/issues/170)
89

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

docs/source/configuration/configuring_transformers.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ transformers will be used. For example::
1313
In first command robotidy will run only ``NormalizeNewLines`` transformer and it will configure it with ``test_case_lines = 2``.
1414
In second command robotidy will run all of the transformers but will configure ``NormalizeNewLines`` with ``test_case_lines = 2``.
1515

16+
You can also run all transformers except selected ones. For that you need to configure transformer you want to exclude
17+
with ``enabled`` parameter::
18+
19+
robotidy --configure TRANSFORMER_NAME:enabled=False
20+
1621
.. note::
1722
To see list of available transformers run:
1823

docs/source/transformers/AlignSettingsSection.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ to 0.
9595

9696
.. tabs::
9797

98-
.. code-tab:: argument_indent=4 (default)
98+
.. code-tab:: robotframework argument_indent=4 (default)
9999

100100
*** Settings ***
101101
Suite Setup Start Session
@@ -104,7 +104,7 @@ to 0.
104104
... password=${PASSWORD}
105105
Suite Teardown Close Session
106106

107-
.. code-tab:: argument_indent=2
107+
.. code-tab:: robotframework argument_indent=2
108108

109109
*** Settings ***
110110
Suite Setup Start Session
@@ -113,7 +113,7 @@ to 0.
113113
... password=${PASSWORD}
114114
Suite Teardown Close Session
115115

116-
.. code-tab:: argument_indent=0
116+
.. code-tab:: robotframework argument_indent=0
117117

118118
*** Settings ***
119119
Suite Setup Start Session

docs/source/transformers/index.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ Transformers
1010

1111
*
1212

13+
.. rubric:: List of transformers
14+
15+
To see list of all transformers currently implemented in robotidy run::
16+
17+
robotidy --list
18+
19+
To see brief docs for selected transformer run::
20+
21+
robotidy --desc TRANSFORMER_NAME
22+
1323
See :ref:`configuring-transformers` to learn how transformers can be configured.
1424

1525
.. rubric:: Order of transformers
@@ -32,19 +42,9 @@ It will transform files according to internal order (in this example ``ReplaceRu
3242
robotidy --transform SplitTooLongLine src
3343
robotidy --transform ReplaceRunKeywordIf src
3444

35-
You can also add ``--force-order`` flag to use order provided in cli:
45+
You can also add ``--force-order`` flag to use order provided in cli::
46+
3647
robotidy --force-order --transform SplitTooLongLine --transform ReplaceRunKeywordIf src
3748

3849
External transformers are used last. If you want to change this behaviour (for example run your custom transformer
3950
before default ones) you need to use ``--force-order`` flag.
40-
41-
.. rubric:: List of transformers
42-
43-
To see list of all transformers currently implemented in robotidy run::
44-
45-
robotidy --list
46-
47-
To see brief docs for selected transformer run::
48-
49-
robotidy --desc TRANSFORMER_NAME
50-

robotidy/transformers/__init__.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,41 @@ def import_transformer(name, args):
5454
def load_transformer(name, args, config):
5555
# if we configure the same parameter for both --transform and --configure we need to overwrite it
5656
# it is done by converting to dict and back to list in format of key=value
57-
temp_args = {}
58-
for arg in chain(args, config.get(name, ())):
59-
param, value = arg.split('=', maxsplit=1)
60-
temp_args[param] = value
61-
args = [f'{key}={value}' for key, value in temp_args.items()]
57+
args, enabled = parse_params(args, config)
58+
if not enabled:
59+
return None
6260
import_name = f'robotidy.transformers.{name}' if name in TRANSFORMERS else name
6361
return import_transformer(import_name, args)
6462

6563

64+
def parse_params(args, config):
65+
temp_args = {}
66+
enabled = True
67+
for arg in chain(args, config):
68+
param, value = arg.split('=', maxsplit=1)
69+
if param == 'enabled':
70+
enabled = value.lower() == 'true'
71+
else:
72+
temp_args[param] = value
73+
return [f'{key}={value}' for key, value in temp_args.items()], enabled
74+
75+
6676
def load_transformers(allowed_transformers, config, allow_disabled=False, force_order=False):
6777
""" Dynamically load all classes from this file with attribute `name` defined in allowed_transformers """
6878
loaded_transformers = []
6979
allowed_mapped = {name: args for name, args in allowed_transformers} if allowed_transformers else {}
7080
if not force_order:
7181
for name in TRANSFORMERS:
72-
if allowed_mapped:
73-
if name in allowed_mapped:
74-
imported_class = load_transformer(name, allowed_mapped[name], config)
75-
if imported_class is None:
76-
return []
77-
loaded_transformers.append(imported_class)
78-
else:
79-
imported_class = import_transformer(f'robotidy.transformers.{name}', config.get(name, ()))
82+
if not allowed_mapped or name in allowed_mapped:
83+
imported_class = load_transformer(name, allowed_mapped.get(name, ()), config.get(name, ()))
8084
if imported_class is None:
81-
return []
82-
if allow_disabled or getattr(imported_class, 'ENABLED', True):
85+
continue
86+
if allowed_mapped or allow_disabled or getattr(imported_class, 'ENABLED', True):
8387
loaded_transformers.append(imported_class)
8488
for name in allowed_mapped:
8589
if force_order or name not in TRANSFORMERS:
86-
imported_class = load_transformer(name, allowed_mapped[name], config)
90+
imported_class = load_transformer(name, allowed_mapped[name], config.get(name, ()))
8791
if imported_class is None:
88-
return []
92+
continue
8993
loaded_transformers.append(imported_class)
9094
return loaded_transformers

tests/utest/test_cli.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,30 @@ def test_line_sep(self, line_sep):
321321
with open(str(actual), newline='') as f:
322322
actual_str = f.read()
323323
assert actual_str == expected_str, 'Line endings does not match'
324+
325+
@pytest.mark.parametrize('force_order', [True, False])
326+
@pytest.mark.parametrize('allow_disabled', [True, False])
327+
@pytest.mark.parametrize('transformers, configure, present', [
328+
(None, {}, True),
329+
(None, {'AlignVariablesSection': ['enabled=True']}, True),
330+
(None, {'AlignVariablesSection': ['enabled=false']}, False),
331+
([('NormalizeAssignments', [])], {}, False),
332+
([('NormalizeAssignments', []), ('AlignVariablesSection', [])], {}, True),
333+
([('NormalizeAssignments', []), ('AlignVariablesSection', ['up_to_column=4'])], {}, True),
334+
([('NormalizeAssignments', []), ('AlignVariablesSection', ['up_to_column=4', 'enabled=True'])], {}, True),
335+
([('NormalizeAssignments', []), ('AlignVariablesSection', ['up_to_column=4', 'enabled=False'])], {}, False),
336+
([('NormalizeAssignments', []), ('AlignVariablesSection', ['up_to_column=4'])],
337+
{'AlignVariablesSection': ['enabled=True']}, True),
338+
([('NormalizeAssignments', []), ('AlignVariablesSection', ['up_to_column=4'])],
339+
{'AlignVariablesSection': ['enabled=False']}, False)
340+
])
341+
def test_disable_transformers(self, transformers, configure, present, force_order, allow_disabled):
342+
if force_order and not transformers:
343+
present = False
344+
disabled_transformer = 'AlignVariablesSection'
345+
transformers = load_transformers(transformers, configure, allow_disabled=allow_disabled,
346+
force_order=force_order)
347+
if present:
348+
assert any(transformer.__class__.__name__ == disabled_transformer for transformer in transformers)
349+
else:
350+
assert all(transformer.__class__.__name__ != disabled_transformer for transformer in transformers)

0 commit comments

Comments
 (0)