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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
```
robotidy -c NormalizeAssignments:equal_sign_type=equal_sign -c NormalizeAssignments:equal_sign_type_variables=remove <src>
```
- New `OrderTags` (non default) transformer. It orders tags in lexicographic order ([#205](https://github.com/MarketSquare/robotframework-tidy/issues/205))
- New `NormalizeTags` (non default) transformer. It normalizes tag name case and removes duplicates ([#212](https://github.com/MarketSquare/robotframework-tidy/issues/212))

### Features
- It is now possible to provide source paths in configuration file ([#154](https://github.com/MarketSquare/robotframework-tidy/issues/154))
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Installation <a name="installation"></a>

You can install Robotidy simply by running:
```
pip install robotframework-tidy
pip install -U robotframework-tidy
```

Usage <a name="usage"></a>
Expand All @@ -56,6 +56,9 @@ All command line options can be displayed in help message by executing:
robotidy --help
```

See [documentation](https://robotidy.readthedocs.io/en/latest/configuration/index.html) for information how to configure
robotidy.

Example <a name="example"></a>
-------
Ugly code before transforming with robotidy:
Expand Down
5 changes: 3 additions & 2 deletions docs/source/transformers/NormalizeTags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

NormalizeTags
================================
Normalize tag names by normalizing case and removing duplicates.

NormalizeTags is not included in default transformers, that's why you need to call it with ``--transform`` explicitly::

Expand All @@ -12,13 +13,13 @@ Or configure `enable` parameter::
robotidy --configure NormalizeTags:enabled=True


Supported cases: lowercase (default), uppercase, titlecase.
Supported cases: lowercase (default), uppercase, title case.
You can configure case using `case` parameter::

robotidy --transform NormalizeTags:case=uppercase


You can remove duplicates without normalizing case by setting normalize_case parameter to False::
You can remove duplicates without normalizing case by setting `normalize_case` parameter to False::

robotidy --transform NormalizeTags:normalize_case=False

7 changes: 3 additions & 4 deletions docs/source/transformers/OrderTags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

OrderTags
================================
Order tags in case-insensitive way in ascending order.

OrderTags is not included in default transformers, that's why you need to call it with ``--transform`` explicitly::

Expand All @@ -11,10 +12,8 @@ Or configure `enable` parameter::

robotidy --configure OrderTags:enabled=True

By default tags are ordered in case-insensitive way in ascending order.
This relates to tags in Test Cases, Keywords, Force Tags and Default Tags.


.. tabs::

.. code-tab:: robotframework Before
Expand Down Expand Up @@ -51,7 +50,7 @@ This relates to tags in Test Cases, Keywords, Force Tags and Default Tags.
[Tags] aa Ab ba Bb Ca Cb
No Operation

Using the same example with reverse=True param we will get tags in descending order::
Using the same example with `reverse=True` param we will get tags in descending order::

robotidy --transform OrderTags:reverse=True src

Expand Down Expand Up @@ -121,4 +120,4 @@ Force Tags and Default Tags ordering can be disabled like this::
*** Test Cases ***
Tags Upper Lower
[Tags] aa Ab ba Bb Ca Cb
My Keyword
My Keyword
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
line-length = 120
2 changes: 1 addition & 1 deletion robotidy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from robotidy.version import __version__


__all__ = ['__version__']
__all__ = ["__version__"]
2 changes: 1 addition & 1 deletion robotidy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from robotidy.cli import cli


if __name__ == '__main__':
if __name__ == "__main__":
cli()
25 changes: 11 additions & 14 deletions robotidy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@
class RobotidyAPI(Robotidy):
def __init__(self, src: str, output: Optional[str], **kwargs):
config = find_and_read_config((src,))
config = {
k: str(v) if not isinstance(v, (list, dict)) else v
for k, v in config.items()
}
config = {k: str(v) if not isinstance(v, (list, dict)) else v for k, v in config.items()}
converter = TransformType()
transformers = [converter.convert(tr, None, None) for tr in config.get('transform', ())]
configurations = [converter.convert(c, None, None) for c in config.get('configure', ())]
transformers = [converter.convert(tr, None, None) for tr in config.get("transform", ())]
configurations = [converter.convert(c, None, None) for c in config.get("configure", ())]
formatting_config = GlobalFormattingConfig(
space_count=kwargs.get('spacecount', None) or int(config.get('spacecount', 4)),
separator=kwargs.get('separator', None) or config.get('separator', 'space'),
line_sep=config.get('lineseparator', 'native'),
start_line=kwargs.get('startline', None) or int(config['startline']) if 'startline' in config else None,
end_line=kwargs.get('endline', None) or int(config['endline']) if 'endline' in config else None
space_count=kwargs.get("spacecount", None) or int(config.get("spacecount", 4)),
separator=kwargs.get("separator", None) or config.get("separator", "space"),
line_sep=config.get("lineseparator", "native"),
start_line=kwargs.get("startline", None) or int(config["startline"]) if "startline" in config else None,
end_line=kwargs.get("endline", None) or int(config["endline"]) if "endline" in config else None,
)
exclude = config.get('exclude', None)
extend_exclude = config.get('extend_exclude', None)
exclude = config.get("exclude", None)
extend_exclude = config.get("extend_exclude", None)
exclude = validate_regex(exclude if exclude is not None else DEFAULT_EXCLUDES)
extend_exclude = validate_regex(extend_exclude)
super().__init__(
Expand All @@ -42,7 +39,7 @@ def __init__(self, src: str, output: Optional[str], **kwargs):
verbose=False,
check=False,
output=output,
force_order=False
force_order=False,
)


Expand Down
54 changes: 30 additions & 24 deletions robotidy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@
StatementLinesCollector,
decorate_diff_with_color,
GlobalFormattingConfig,
ModelWriter
ModelWriter,
)


class Robotidy:
def __init__(self,
transformers: List[Tuple[str, List]],
transformers_config: List[Tuple[str, List]],
src: Tuple[str, ...],
exclude: Pattern,
extend_exclude: Pattern,
overwrite: bool,
show_diff: bool,
formatting_config: GlobalFormattingConfig,
verbose: bool,
check: bool,
output: Optional[Path],
force_order: bool
):
def __init__(
self,
transformers: List[Tuple[str, List]],
transformers_config: List[Tuple[str, List]],
src: Tuple[str, ...],
exclude: Pattern,
extend_exclude: Pattern,
overwrite: bool,
show_diff: bool,
formatting_config: GlobalFormattingConfig,
verbose: bool,
check: bool,
output: Optional[Path],
force_order: bool,
):
self.sources = get_paths(src, exclude, extend_exclude)
self.overwrite = overwrite
self.show_diff = show_diff
Expand All @@ -44,20 +45,20 @@ def __init__(self,
self.transformers = load_transformers(transformers, transformers_config, force_order=force_order)
for transformer in self.transformers:
# inject global settings TODO: handle it better
setattr(transformer, 'formatting_config', self.formatting_config)
setattr(transformer, "formatting_config", self.formatting_config)

def transform_files(self):
changed_files = 0
for source in self.sources:
try:
stdin = False
if str(source) == '-':
if str(source) == "-":
stdin = True
if self.verbose:
click.echo('Loading file from stdin')
click.echo("Loading file from stdin")
source = self.load_from_stdin()
elif self.verbose:
click.echo(f'Transforming {source} file')
click.echo(f"Transforming {source} file")
model = get_model(source)
diff, old_model, new_model = self.transform(model)
if diff:
Expand Down Expand Up @@ -97,16 +98,21 @@ def save_model(self, model):
output = self.output or model.source
ModelWriter(output=output, newline=self.formatting_config.line_sep).write(model)

def output_diff(self, path: str, old_model: StatementLinesCollector, new_model: StatementLinesCollector):
def output_diff(
self,
path: str,
old_model: StatementLinesCollector,
new_model: StatementLinesCollector,
):
if not self.show_diff:
return
old = [l + '\n' for l in old_model.text.splitlines()]
new = [l + '\n' for l in new_model.text.splitlines()]
lines = list(unified_diff(old, new, fromfile=f'{path}\tbefore', tofile=f'{path}\tafter'))
old = [l + "\n" for l in old_model.text.splitlines()]
new = [l + "\n" for l in new_model.text.splitlines()]
lines = list(unified_diff(old, new, fromfile=f"{path}\tbefore", tofile=f"{path}\tafter"))
if not lines:
return
colorized_output = decorate_diff_with_color(lines)
click.echo(colorized_output.encode('ascii', 'ignore').decode('ascii'), color=True)
click.echo(colorized_output.encode("ascii", "ignore").decode("ascii"), color=True)

@staticmethod
def convert_configure(configure: List[Tuple[str, List]]) -> Dict[str, List]:
Expand Down
Loading