Skip to content

Commit eb487b4

Browse files
authored
Fix --custom-transformers not supported in the configuration file (#647)
1 parent bd49e13 commit eb487b4

File tree

10 files changed

+42
-12
lines changed

10 files changed

+42
-12
lines changed

docs/releasenotes/4.0.0.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,13 @@ any other transformer not listed with ``--transform``. That's why if user would
195195
> robotidy --transform MyCustomClass.py test.robot
196196

197197
It would disable all default transformers and only run MyCustomClass.
198-
This release introduces new option ``--load-transformer`` which imports custom transformers on top of the default ones::
198+
This release introduces new option ``--custom-transformers`` which imports custom transformers on top of the default ones::
199199

200-
> robotidy --load-transformer MyCustomClass.py test.robot
200+
> robotidy --custom-transformers MyCustomClass.py test.robot
201201

202202
It is also possible to pass transformer configuration either using this option or through ``--configure``::
203203

204-
> robotidy -c ExtClass1.py:param=value --load-transformer ExtClass2.py:param2=value test.robot
204+
> robotidy -c ExtClass1.py:param=value --custom-transformers ExtClass2.py:param2=value test.robot
205205

206206
Load custom transformers from the module
207207
-------------------------------------------
@@ -210,7 +210,7 @@ It is now possible to load transformers from the Python module. Importing transf
210210
to how custom libraries are imported in Robot Framework. If the file has the same name as transformer, it will
211211
be auto-imported. The following command::
212212

213-
> robotidy --load-transformer CustomClass.py
213+
> robotidy --custom-transformers CustomClass.py
214214

215215
will load ``class CustomClass`` from the ``CustomClass.py`` file. It's the old behaviour and it will not change.
216216

@@ -357,7 +357,7 @@ Spaces in the transformer name or configuration
357357
Spaces are now allowed in the transformer path or configuration value. To provide such name or value
358358
enclose it with quotation marks::
359359

360-
> robotidy --load-transformer "C:\\My Transformers\\Transformer.py"
360+
> robotidy --custom-transformers "C:\\My Transformers\\Transformer.py"
361361
> robotidy --configure CustomTransformer:value="param value"
362362

363363
Bugs
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load_transformers not supported in the configuration file (#576)
2+
-----------------------------------------------------------------
3+
4+
``--load-transformers`` (and ``load_transformers`` in the toml configuration file) was renamed before to
5+
``--custom-transformers`` (``custom_transformers``). Old name remained due to backward compatibility. However it was
6+
not handled properly and Robotidy cli only supported ``--load-transformers`` while configuration file only
7+
``custom_transformers``. Now the approach should be unified and both cli and configuration file should support old
8+
and new option name.

docs/source/configuration/configuring_transformers.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
Configuring Transformers
44
========================
55

6-
Transformers can be configured through three different options: ``--transform`` (``-t``), ``--load-transformer`` and
6+
Transformers can be configured through three different options: ``--transform`` (``-t``), ``--custom-transformers`` and
77
``--configure`` (``-c``). They share the same syntax for parameter names and values.
88

99
- ``--configure`` simply provides the configuration to the transformer,
1010
- ``--transform`` is used to include only transformers that have ``--transform`` option,
11-
- ``--load-transformer`` is used to load user transformers. Read more at :ref:`external-transformers`.
11+
- ``--custom-transformers`` is used to load user transformers. Read more at :ref:`external-transformers`.
1212

1313
For example::
1414

1515
robotidy --transform NormalizeNewLines:test_case_lines=2 src
1616
robotidy --configure NormalizeNewLines:test_case_lines=2 src
17-
robotidy --configure NormalizeNewLines:test_case_lines=1 --load-transformer MyCustomTransformer.py:param=value src
17+
robotidy --configure NormalizeNewLines:test_case_lines=1 --custom-transformers MyCustomTransformer.py:param=value src
1818

1919
With first command robotidy will run only ``NormalizeNewLines`` transformer and it will configure it with ``test_case_lines = 2``.
2020

docs/source/external_transformers.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ file with class to run external transformers with *robotidy*::
77

88
robotidy --transform MyTransformers.YourCustomTransformer src
99
robotidy --transform C:\transformers\YourCustomTransformer2.py src
10-
robotidy --load-transformer C:\transformers\YourCustomTransformer2.py src
10+
robotidy --custom-transformers C:\transformers\YourCustomTransformer2.py src
1111

1212
External transformers can be configured in the same way internal transformers are configured - see :ref:`configuring-transformers`.
1313

14-
You can use both ``--transform`` and ``--load-transformer`` options to load custom user transformer. The main difference
15-
is that ``--transform`` works like include and will only run transformers listed with ``--transform``. While ``--load-transformer``
14+
You can use both ``--transform`` and ``--custom-transformers`` options to load custom user transformer. The main difference
15+
is that ``--transform`` works like include and will only run transformers listed with ``--transform``. While ``--custom-transformers``
1616
will run default transformers first and then user transformers.
1717

1818
Importing whole modules
@@ -21,7 +21,7 @@ Importing whole modules
2121
Importing transformers from module works similarly to how custom libraries are imported in Robot Framework. If the the
2222
file has the same name as transformer it will be auto imported. For example following import::
2323

24-
robotidy --load-transformer CustomFormatter.py src
24+
robotidy --custom-transformers CustomFormatter.py src
2525

2626
will auto import class ``CustomFormatter`` from the file.
2727

robotidy/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ def generate_config(global_config: config_module.MainConfig):
232232
)
233233
@click.option(
234234
"--load-transformers",
235+
"--custom-transformers",
235236
"custom_transformers",
236237
type=TransformConfigParameter(),
237238
multiple=True,

robotidy/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ def from_config_file(self, config: Dict, config_path: Path) -> "RawConfig":
186186
options_map = map_class_fields_with_their_types(self)
187187
parsed_config = {"defined_in_config": {"defined_in_config", "config_path"}, "config_path": config_path}
188188
for key, value in config.items():
189+
# workaround to be able to use two option names for same action - backward compatibility change
190+
if key == "load_transformers":
191+
key = "custom_transformers"
189192
if key not in options_map:
190193
raise exceptions.NoSuchOptionError(key, list(options_map.keys())) from None
191194
value_type = options_map[key]

tests/utest/test_cli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,18 @@ def test_skip_options(self, tmp_path):
518518
option_names.append("empty")
519519
run_tidy([*option_names, str(tmp_path)])
520520

521+
@pytest.mark.parametrize("option_name", ["custom_transformers", "load_transformers"])
522+
def test_load_custom_transformers_from_config(self, option_name):
523+
config_path = TEST_DATA_DIR / "config_with_custom_transformers" / f"{option_name}.toml"
524+
config_file = read_pyproject_config(config_path)
525+
config = RawConfig().from_config_file(config_file, config_path)
526+
assert config.custom_transformers[0].name == "CustomTransformer.py"
527+
528+
@pytest.mark.parametrize("option_name", ["--custom-transformers", "--load-transformers"])
529+
def test_load_custom_transformers_from_cli(self, option_name, tmp_path):
530+
custom_transformer = TEST_DATA_DIR / "config_with_custom_transformers" / "CustomTransformer.py"
531+
run_tidy([option_name, str(custom_transformer), str(tmp_path)])
532+
521533

522534
class TestGenerateConfig:
523535
def validate_generated_default_configuration(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class CustomTransformer:
2+
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.robotidy]
2+
custom_transformers = ["CustomTransformer.py"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.robotidy]
2+
load_transformers = ["CustomTransformer.py"]

0 commit comments

Comments
 (0)