Skip to content

Commit 83cd472

Browse files
authored
Formatting summary & verbose output (#608)
1 parent a9aead9 commit 83cd472

File tree

3 files changed

+105
-25
lines changed

3 files changed

+105
-25
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
More verbose output (#572)
2+
---------------------------
3+
4+
Robotidy output is now more verbose. If the file is formatted (or would be formatted if not for
5+
``--check`` or ``--no-overwrite options) the file path and run summary is displayed:
6+
7+
```
8+
> robotidy --check .
9+
Would reformat D:\test_repository\resources\db_keywords.resource file
10+
Would reformat D:\test_repository\tests\ui\login.robot file
11+
12+
2 files would be reformatted, 112 files would be left unchanged.
13+
```
14+
15+
```
16+
> robotidy .
17+
Formatting D:\test_repository\resources\db_keywords.resource file
18+
Formatting D:\test_repository\tests\ui\login.robot file
19+
20+
2 files reformatted, 112 files left unchanged.
21+
```
22+
23+
```
24+
> robotidy --verbose .
25+
Found D:\test_repository\resources\ui_keywords.resource file
26+
Found (...)
27+
Formatting D:\test_repository\resources\db_keywords.resource file
28+
Found (...)
29+
Formatting D:\test_repository\tests\ui\login.robot file
30+
Found (...)
31+
32+
2 files reformatted, 112 files left unchanged.
33+
```

robotidy/app.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ def get_model(self, source):
2929

3030
def transform_files(self):
3131
changed_files = 0
32+
all_files = 0
33+
stdin = False
3234
for source, config in self.main_config.get_sources_with_configs():
3335
self.config = config
36+
all_files += 1
3437
disabler_finder = RegisterDisablers(self.config.formatting.start_line, self.config.formatting.end_line)
3538
try:
3639
stdin = False
@@ -40,14 +43,15 @@ def transform_files(self):
4043
click.echo("Loading file from stdin")
4144
source = self.load_from_stdin()
4245
elif self.config.verbose:
43-
click.echo(f"Transforming {source} file")
46+
click.echo(f"Found {source} file")
4447
model = self.get_model(source)
4548
model_path = model.source
4649
disabler_finder.visit(model)
4750
if disabler_finder.file_disabled:
4851
continue
4952
diff, old_model, new_model, model = self.transform_until_stable(model, disabler_finder)
5053
if diff:
54+
self.log_formatted_source(source, stdin)
5155
changed_files += 1
5256
self.output_diff(model_path, old_model, new_model)
5357
if stdin:
@@ -59,10 +63,33 @@ def transform_files(self):
5963
f"Failed to decode {source}. Default supported encoding by Robot Framework is UTF-8. Skipping file"
6064
)
6165
pass
66+
return self.formatting_result(all_files, changed_files, stdin)
67+
68+
def formatting_result(self, all_files: int, changed_files: int, stdin: bool):
69+
"""
70+
Print formatting summary and return status code.
71+
"""
72+
if not stdin:
73+
all_files = all_files - changed_files
74+
all_files_plurar = "" if all_files == 1 else "s"
75+
changed_files_plurar = "" if changed_files == 1 else "s"
76+
future_tense = "" if self.config.overwrite else " would be"
77+
click.echo(
78+
f"\n{changed_files} file{changed_files_plurar}{future_tense} reformatted, "
79+
f"{all_files} file{all_files_plurar}{future_tense} left unchanged."
80+
)
6281
if not self.config.check or not changed_files:
6382
return 0
6483
return 1
6584

85+
def log_formatted_source(self, source: str, stdin: bool):
86+
if stdin:
87+
return
88+
if not self.config.overwrite:
89+
click.echo(f"Would reformat {source}")
90+
else:
91+
click.echo(f"Reformatted {source}")
92+
6693
def transform_until_stable(self, model, disabler_finder):
6794
diff, old_model, new_model = self.transform(model, disabler_finder.disablers)
6895
reruns = self.config.reruns

tests/utest/test_cli.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,6 @@ def switch_cwd(new_cwd):
3939
os.chdir(prev_cwd)
4040

4141

42-
@contextmanager
43-
def switch_cwd(new_cwd):
44-
prev_cwd = Path.cwd()
45-
os.chdir(new_cwd)
46-
try:
47-
yield
48-
finally:
49-
os.chdir(prev_cwd)
50-
51-
5242
class TestCli:
5343
@pytest.mark.parametrize(
5444
"name, similar",
@@ -320,28 +310,46 @@ def test_help(self, flag):
320310
result = run_tidy([flag])
321311
assert f"Robotidy is a tool for formatting" in result.output
322312

323-
@pytest.mark.parametrize("source, return_status", [("golden.robot", 0), ("not_golden.robot", 1)])
324-
def test_check(self, source, return_status):
313+
@pytest.mark.parametrize(
314+
"source, return_status, expected_output",
315+
[
316+
("golden.robot", 0, "\n0 files would be reformatted, 1 file would be left unchanged.\n"),
317+
("not_golden.robot", 1, "\n1 file would be reformatted, 0 files would be left unchanged.\n"),
318+
],
319+
)
320+
def test_check(self, source, return_status, expected_output):
325321
source = TEST_DATA_DIR / "check" / source
322+
if return_status:
323+
expected_output = f"Would reformat {source}\n{expected_output}"
326324
with patch("robotidy.utils.misc.ModelWriter") as mock_writer:
327-
run_tidy(
325+
result = run_tidy(
328326
["--check", "--transform", "NormalizeSectionHeaderName", str(source)],
329327
exit_code=return_status,
330328
)
331329
mock_writer.assert_not_called()
330+
assert result.output == expected_output
332331

333-
@pytest.mark.parametrize("source, return_status", [("golden.robot", 0), ("not_golden.robot", 1)])
334-
def test_check_overwrite(self, source, return_status):
332+
@pytest.mark.parametrize(
333+
"source, return_status, expected_output",
334+
[
335+
("golden.robot", 0, "\n0 files reformatted, 1 file left unchanged.\n"),
336+
("not_golden.robot", 1, "\n1 file reformatted, 0 files left unchanged.\n"),
337+
],
338+
)
339+
def test_check_overwrite(self, source, return_status, expected_output):
335340
source = TEST_DATA_DIR / "check" / source
341+
if return_status:
342+
expected_output = f"Reformatted {source}\n{expected_output}"
336343
with patch("robotidy.utils.misc.ModelWriter") as mock_writer:
337-
run_tidy(
344+
result = run_tidy(
338345
["--check", "--overwrite", "--transform", "NormalizeSectionHeaderName", str(source)],
339346
exit_code=return_status,
340347
)
341348
if return_status:
342349
mock_writer.assert_called()
343350
else:
344351
mock_writer.assert_not_called()
352+
assert result.output == expected_output
345353

346354
@pytest.mark.parametrize("color_flag", ["--color", "--no-color", None])
347355
@pytest.mark.parametrize("color_env", [True, False])
@@ -409,26 +417,38 @@ def test_exclude_gitignore(self, exclude, extend_exclude, skip_gitignore, allowe
409417
assert paths == allowed_paths
410418

411419
@pytest.mark.parametrize(
412-
"source, should_parse",
420+
"source, should_parse, summary",
413421
[
414-
(None, ["test.robot", "resources/test.robot"]), # calls: robotidy
415-
("test3.robot", ["test3.robot"]), # calls: robotidy test3.robot
416-
("test.robot", ["test.robot"]),
417-
(".", ["test.robot", "test3.robot", "resources/test.robot"]),
422+
(
423+
None,
424+
["test.robot", "resources/test.robot"],
425+
"0 files reformatted, 2 files left unchanged.",
426+
), # calls: robotidy
427+
(
428+
"test3.robot",
429+
["test3.robot"],
430+
"0 files reformatted, 1 file left unchanged.",
431+
), # calls: robotidy test3.robot
432+
("test.robot", ["test.robot"], "0 files reformatted, 1 file left unchanged."),
433+
(
434+
".",
435+
["test.robot", "test3.robot", "resources/test.robot"],
436+
"0 files reformatted, 3 files left unchanged.",
437+
),
418438
],
419439
)
420-
def test_src_and_space_in_param_in_configuration(self, source, should_parse):
440+
def test_src_and_space_in_param_in_configuration(self, source, should_parse, summary):
421441
source_dir = TEST_DATA_DIR / "pyproject_with_src"
422442
os.chdir(source_dir)
423443
if source is not None:
424444
source = source_dir / source
425445
result = run_tidy([str(source)])
426446
else:
427447
result = run_tidy()
428-
expected = [f"Loaded configuration from {source_dir / 'pyproject.toml'}"]
448+
expected = [f"Loaded configuration from {source_dir / 'pyproject.toml'}", summary]
429449
for file in should_parse:
430450
path = source_dir / file
431-
expected.append(f"Transforming {path} file")
451+
expected.append(f"Found {path} file")
432452
actual = sorted(line for line in result.output.split("\n") if line.strip())
433453
assert actual == sorted(expected)
434454

0 commit comments

Comments
 (0)