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

Refine exit codes #595

Merged
merged 5 commits into from
Jul 31, 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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ These features will be included in the next release:

Added
-----
- New exit codes 2 for file not found, 3 for invalid command line arguments, 4 for
missing dependencies and 123 for unknown failures.
- Display exit code in parentheses after error message.

Fixed
-----
Expand Down
4 changes: 2 additions & 2 deletions constraints-oldest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
# interpreter and Python ependencies. Keep this up-to-date with minimum
# versions in `setup.cfg`.
black==22.3.0
darkgraylib==1.3.2
darkgraylib==2.0.0
defusedxml==0.7.1
flake8-2020==1.6.1
flake8-bugbear==22.1.11
flake8-comprehensions==3.7.0
flynt==0.76
graylint==1.1.2
graylint==2.0.0
mypy==0.990
Pygments==2.4.0
pytest==6.2.0
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ packages = find:
install_requires =
# NOTE: remember to keep `constraints-oldest.txt` in sync with these
black>=22.3.0
darkgraylib~=1.3.2
graylint~=1.1.2
darkgraylib~=2.0.0
graylint~=2.0.0
toml>=0.10.0
# NOTE: remember to keep `.github/workflows/python-package.yml` in sync
# with the minimum required Python version
Expand Down
31 changes: 22 additions & 9 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
from darker.import_sorting import apply_isort, isort
from darker.utils import debug_dump, glob_any
from darker.verification import ASTVerifier, BinarySearch, NotEquivalentError
from darkgraylib.command_line import (
EXIT_CODE_CMDLINE_ERROR,
EXIT_CODE_DEPENDENCY,
EXIT_CODE_FILE_NOT_FOUND,
EXIT_CODE_UNKNOWN,
)
from darkgraylib.config import show_config_if_debug
from darkgraylib.files import find_project_root
from darkgraylib.git import (
Expand Down Expand Up @@ -445,7 +451,8 @@ def _import_pygments(): # type: ignore
return highlight, TerminalFormatter, PythonLexer


def main( # pylint: disable=too-many-locals,too-many-branches,too-many-statements
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
def main( # noqa: C901,PLR0912,PLR0915
argv: List[str] = None,
) -> int:
"""Parse the command line and reformat and optionally lint each source file
Expand Down Expand Up @@ -549,10 +556,8 @@ def main( # pylint: disable=too-many-locals,too-many-branches,too-many-statemen
rev2_repr = (
"the working tree" if revrange.rev2 == WORKTREE else revrange.rev2
)
raise ArgumentError(
Action(["PATH"], "path"),
f"Error: Path(s) {missing_reprs} do not exist in {rev2_repr}",
)
msg = f"Path(s) {missing_reprs} do not exist in {rev2_repr}"
raise FileNotFoundError(msg)

# These paths are relative to `common_root`:
files_to_process = filter_python_files(paths, common_root, {})
Expand Down Expand Up @@ -631,10 +636,18 @@ def main_with_error_handling() -> int:
"""Entry point for console script"""
try:
return main()
except (ArgumentError, DependencyError) as exc_info:
if logger.root.level < logging.WARNING:
raise
sys.exit(str(exc_info))
except FileNotFoundError as exc_info:
logger.exception("%s (%d)", exc_info, EXIT_CODE_FILE_NOT_FOUND) # noqa: TRY401
return EXIT_CODE_FILE_NOT_FOUND
except ArgumentError as exc_info:
logger.exception("%s (%d)", exc_info, EXIT_CODE_CMDLINE_ERROR) # noqa: TRY401
return EXIT_CODE_CMDLINE_ERROR
except DependencyError as exc_info:
logger.exception("%s (%d)", exc_info, EXIT_CODE_DEPENDENCY) # noqa: TRY401
return EXIT_CODE_DEPENDENCY
except Exception as exc_info: # pylint: disable=broad-exception-caught
logger.exception("%s (%d)", exc_info, EXIT_CODE_UNKNOWN) # noqa: TRY401
return EXIT_CODE_UNKNOWN


if __name__ == "__main__":
Expand Down
11 changes: 4 additions & 7 deletions src/darker/tests/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import os
import re
from argparse import ArgumentError
from importlib import reload
from pathlib import Path
from textwrap import dedent
Expand Down Expand Up @@ -711,10 +710,8 @@ def test_main_missing_in_worktree(git_repo):
paths["a.py"].unlink()

with pytest.raises(
ArgumentError,
match=re.escape(
"argument PATH: Error: Path(s) 'a.py' do not exist in the working tree"
),
FileNotFoundError,
match=re.escape("Path(s) 'a.py' do not exist in the working tree"),
):

main(["a.py"])
Expand All @@ -727,8 +724,8 @@ def test_main_missing_in_revision(git_repo):
paths["a.py"].touch()

with pytest.raises(
ArgumentError,
match=re.escape("argument PATH: Error: Path(s) 'a.py' do not exist in HEAD"),
FileNotFoundError,
match=re.escape("Path(s) 'a.py' do not exist in HEAD"),
):

main(["--diff", "--revision", "..HEAD", "a.py"])
7 changes: 4 additions & 3 deletions src/darker/tests/test_main_stdin_filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import toml

import darker.__main__
from darkgraylib.command_line import EXIT_CODE_CMDLINE_ERROR
from darkgraylib.config import ConfigurationError
from darkgraylib.testtools.git_repo_plugin import GitRepoFixture
from darkgraylib.testtools.helpers import raises_if_exception
Expand All @@ -18,7 +19,7 @@


@pytest.mark.kwparametrize(
dict(expect=SystemExit(2)),
dict(expect=SystemExit(EXIT_CODE_CMDLINE_ERROR)),
dict(config_src=["a.py"], expect_a_py='modified = "a.py worktree"\n'),
dict(config_src=["b.py"], src=["a.py"], expect_a_py='modified = "a.py worktree"\n'),
dict(
Expand Down Expand Up @@ -125,8 +126,8 @@
" ':WORKTREE:'"
),
),
dict(revision="..:STDIN:", expect=SystemExit(2)),
dict(revision="..:WORKTREE:", expect=SystemExit(2)),
dict(revision="..:STDIN:", expect=SystemExit(EXIT_CODE_CMDLINE_ERROR)),
dict(revision="..:WORKTREE:", expect=SystemExit(EXIT_CODE_CMDLINE_ERROR)),
config_src=None,
src=[],
stdin_filename=None,
Expand Down
Loading