Skip to content

Commit

Permalink
Improve file errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz committed Feb 3, 2024
1 parent a6646c6 commit 57caac5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
12 changes: 12 additions & 0 deletions docs/releasenotes/unreleased/fixes.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Improved error handling of file related issues (#629)
-----------------------------------------------------

In case Robotidy failed to read or write to file, generic message with decode error was shown. Now it will display
more detailed error message::

> robotidy read_only_file.robot
Failed to decode read_only_file.robot with an error: Opening file 'read_only_file.robot' failed:
PermissionError: [Errno 13] Permission denied: 'read_only_file.robot'
Skipping file

0 files reformatted, 1 file left unchanged.
14 changes: 5 additions & 9 deletions robotidy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,15 @@ def transform_files(self):
if disabler_finder.file_disabled:
continue
diff, old_model, new_model, model = self.transform_until_stable(model, disabler_finder)
if diff:
self.log_formatted_source(source, stdin)
changed_files += 1
self.output_diff(model_path, old_model, new_model)
if stdin:
self.print_to_stdout(new_model)
elif diff:
self.save_model(model_path, model)
except DataError:
click.echo(
f"Failed to decode {source}. Default supported encoding by Robot Framework is UTF-8. Skipping file"
)
pass
self.log_formatted_source(source, stdin)
self.output_diff(model_path, old_model, new_model)
changed_files += 1
except DataError as err:
click.echo(f"Failed to decode {source} with an error: {err}\nSkipping file")
return self.formatting_result(all_files, changed_files, stdin)

def formatting_result(self, all_files: int, changed_files: int, stdin: bool):
Expand Down
6 changes: 6 additions & 0 deletions tests/utest/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ def test_check_overwrite(self, source, return_status, expected_output):
mock_writer.assert_not_called()
assert result.output == expected_output

def test_read_only_file(self):
source = TEST_DATA_DIR / "read_only" / "test.robot"
# overwrite input which is read-only file
result = run_tidy([str(source)], overwrite_input=True)
assert "Permission denied" in result.output

@pytest.mark.parametrize("color_flag", ["--color", "--no-color", None])
@pytest.mark.parametrize("color_env", [True, False])
def test_disable_coloring(self, color_flag, color_env):
Expand Down
10 changes: 10 additions & 0 deletions tests/utest/testdata/read_only/test.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*** Settings ***






*** Test Cases ***
Test
Test
19 changes: 13 additions & 6 deletions tests/utest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
from robotidy.cli import cli


def run_tidy(args: List[str] = None, exit_code: int = 0, output: Optional[str] = None, std_in: Optional = None):
def run_tidy(
args: List[str] = None,
exit_code: int = 0,
output: Optional[str] = None,
std_in: Optional = None,
overwrite_input: bool = False,
):
runner = CliRunner()
arguments = args if args is not None else []
if output:
output_path = str(Path(Path(__file__).parent, "actual", output))
else:
output_path = str(Path(Path(__file__).parent, "actual", "tmp"))
arguments = ["--output", output_path] + arguments
if not overwrite_input:
if output:
output_path = str(Path(Path(__file__).parent, "actual", output))
else:
output_path = str(Path(Path(__file__).parent, "actual", "tmp"))
arguments = ["--output", output_path] + arguments
result = runner.invoke(cli, arguments, input=std_in)
if result.exit_code != exit_code:
print(result.output)
Expand Down

0 comments on commit 57caac5

Please sign in to comment.