From 6c5a65c222963f713379739f57273b82cac1a0b0 Mon Sep 17 00:00:00 2001 From: Suman Gole <60792767+golesuman@users.noreply.github.com> Date: Fri, 24 May 2024 21:49:52 +0545 Subject: [PATCH] fix: handle the file not found error gracefully (#46) --- src/commitlint/cli.py | 4 ++++ tests/test_cli.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/commitlint/cli.py b/src/commitlint/cli.py index d42b7f3..5f736f3 100644 --- a/src/commitlint/cli.py +++ b/src/commitlint/cli.py @@ -228,6 +228,10 @@ def main() -> None: console.error(f"{ex}") sys.exit(1) + except FileNotFoundError: + console.error(f"Error: file '{args.file}' not found") + sys.exit(1) + if __name__ == "__main__": main() # pragma: no cover diff --git a/tests/test_cli.py b/tests/test_cli.py index 9648885..35a78e5 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -383,6 +383,22 @@ def test__main__sets_config_for_verbose( main() assert config.verbose is True + @patch( + "commitlint.cli.get_args", + return_value=MagicMock( + file="path/to/non_existent_file.txt", skip_detail=False, quiet=False + ), + ) + def test__main__with_missing_file( + self, _mock_get_args, _mock_output_error, mock_output_success + ): + mock_open().side_effect = FileNotFoundError( + 2, "No such file or directory", "path/to/non_existent_file.txt" + ) + + with pytest.raises(SystemExit): + main() + class TestCLIMainQuiet: # main : quiet (directly checking stdout and stderr)