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: display help even outside git repo #287

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
25 changes: 17 additions & 8 deletions mergify_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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}",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
23 changes: 20 additions & 3 deletions mergify_cli/tests/test_mergify_cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
sileht marked this conversation as resolved.
Show resolved Hide resolved
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