diff --git a/mergify_cli/__init__.py b/mergify_cli/__init__.py index 2d715f9..dcc0194 100755 --- a/mergify_cli/__init__.py +++ b/mergify_cli/__init__.py @@ -390,7 +390,7 @@ async def log_httpx_response(response: httpx.Response) -> None: ) -def get_trunk(trunk: str | None = None) -> str: +def get_trunk() -> str: try: trunk = subprocess.check_output( "git config --get mergify-cli.stack-trunk", @@ -401,9 +401,13 @@ def get_trunk(trunk: str | None = None) -> str: trunk = "" if not trunk: - dest_branch = subprocess.check_output( - "git rev-parse --abbrev-ref HEAD", shell=True, text=True - ).strip() + try: + dest_branch = subprocess.check_output( + "git rev-parse --abbrev-ref HEAD", shell=True, text=True + ).strip() + except subprocess.CalledProcessError: + return "" + try: trunk = subprocess.check_output( f"git for-each-ref --format='%(upstream:short)' refs/heads/{dest_branch}", @@ -636,8 +640,7 @@ async def stack_main(args: argparse.Namespace) -> None: ) -def cli() -> None: - global DEBUG +def parse_args(args: typing.Sequence[str]) -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument("--debug", action="store_true", help="debug mode") parser.add_argument( @@ -687,12 +690,18 @@ def cli() -> None: help="branch prefix used to create stacked PR", ) - known_args, extra = parser.parse_known_args() + known_args, _ = parser.parse_known_args(args) if known_args.action is None: sys.argv.insert(1, "stack") - args = parser.parse_args() + return parser.parse_args(args) + + +def cli() -> None: + args = parse_args(sys.argv[1:]) + if args.debug: + global DEBUG DEBUG = True asyncio.run(args.func(args)) diff --git a/mergify_cli/tests/test_mergify_cli.py b/mergify_cli/tests/test_mergify_cli.py index eb8da25..3410863 100644 --- a/mergify_cli/tests/test_mergify_cli.py +++ b/mergify_cli/tests/test_mergify_cli.py @@ -1,5 +1,5 @@ # -# Copyright © 2021 Mergify SAS +# Copyright © 2021-2023 Mergify SAS # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -13,6 +13,23 @@ # License for the specific language governing permissions and limitations # under the License. +import subprocess +from unittest import mock -def test_noop() -> None: - pass +import pytest + +import mergify_cli + + +def test_cli_help(capsys: pytest.CaptureFixture[str]) -> None: + with pytest.raises(SystemExit, match="0"): + with mock.patch( + "subprocess.check_output", + side_effect=subprocess.CalledProcessError(2, ""), + ): + mergify_cli.parse_args(["--help"]) + + stdout = capsys.readouterr().out + assert "usage: " in stdout + assert "positional arguments:" in stdout + assert "options:" in stdout