From c1ed3cae98f76197181157b69c6ce4c105fca103 Mon Sep 17 00:00:00 2001 From: Suman Gole Date: Fri, 24 May 2024 20:44:10 +0545 Subject: [PATCH] fix: handle the file not error gracefully --- 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..a8e2336 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 as ex: + console.error(f"{ex}") + sys.exit(1) + if __name__ == "__main__": main() # pragma: no cover diff --git a/tests/test_cli.py b/tests/test_cli.py index 9648885..0495d59 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__valid_commit_message_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)