Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle the file not found error gracefully #46

Merged
merged 2 commits into from
May 24, 2024
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
4 changes: 4 additions & 0 deletions src/commitlint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading