Skip to content

Commit

Permalink
Merge pull request #287 from Mergifyio/devs/DouglasBlackwood/fix-cras…
Browse files Browse the repository at this point in the history
…h/Icf3cfd073774fff2b9551a3eff77d1ce1b505414

fix: display help even outside git repo
  • Loading branch information
mergify[bot] authored Nov 6, 2023
2 parents 216436e + 1fb6b35 commit 4476eb7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
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:
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

0 comments on commit 4476eb7

Please sign in to comment.