Skip to content

Commit

Permalink
fix: handle the file not found error gracefully (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
golesuman authored May 24, 2024
1 parent 84bced3 commit 6c5a65c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit 6c5a65c

Please sign in to comment.