Skip to content

Commit 69db51d

Browse files
authored
feat(ci): improve error message on xml file not found (#757)
1 parent b1802c8 commit 69db51d

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

mergify_cli/ci/cli.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,36 @@
99
from mergify_cli.ci import upload
1010

1111

12+
class JUnitFile(click.Path):
13+
"""Custom Click parameter type for JUnit files with better error messages."""
14+
15+
def __init__(self) -> None:
16+
super().__init__(exists=True, dir_okay=False)
17+
18+
def convert( # type: ignore[override]
19+
self,
20+
value: str,
21+
param: click.Parameter | None,
22+
ctx: click.Context | None,
23+
) -> str:
24+
try:
25+
return super().convert(value, param, ctx)
26+
except click.BadParameter as e:
27+
if "does not exist" in str(e):
28+
# Provide a more helpful error message
29+
error_msg = (
30+
f"JUnit XML file '{value}' does not exist. \n\n"
31+
"This usually indicates that a previous CI step failed to generate the test results.\n"
32+
"Please check if your test execution step completed successfully and produced the expected output file."
33+
)
34+
raise click.BadParameter(
35+
error_msg,
36+
ctx=ctx,
37+
param=param,
38+
) from e
39+
raise
40+
41+
1242
def _process_tests_target_branch(
1343
_ctx: click.Context,
1444
_param: click.Parameter,
@@ -68,7 +98,7 @@ def _process_tests_target_branch(
6898
"files",
6999
nargs=-1,
70100
required=True,
71-
type=click.Path(exists=True, dir_okay=False),
101+
type=JUnitFile(),
72102
)
73103
@utils.run_with_asyncio
74104
async def junit_upload( # noqa: PLR0913
@@ -139,7 +169,7 @@ async def junit_upload( # noqa: PLR0913
139169
"files",
140170
nargs=-1,
141171
required=True,
142-
type=click.Path(exists=True, dir_okay=False),
172+
type=JUnitFile(),
143173
)
144174
@utils.run_with_asyncio
145175
async def junit_process( # noqa: PLR0913

mergify_cli/tests/ci/test_cli.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,32 @@ def test_process_tests_target_branch_callback() -> None:
281281
param_mock,
282282
"",
283283
)
284+
285+
286+
def test_junit_file_not_found_error_message() -> None:
287+
"""Test that a helpful error message is shown when JUnit file doesn't exist."""
288+
runner = testing.CliRunner()
289+
290+
# Set up minimal environment variables
291+
env = {
292+
"MERGIFY_API_URL": "https://api.mergify.com",
293+
"MERGIFY_TOKEN": "abc",
294+
"GITHUB_REPOSITORY": "user/repo",
295+
"GITHUB_BASE_REF": "main",
296+
}
297+
298+
with runner.isolated_filesystem():
299+
# Try to run junit-process with a non-existent file
300+
result = runner.invoke(
301+
cli_junit_upload.junit_process,
302+
["non_existent_junit.xml"],
303+
env=env,
304+
)
305+
306+
assert result.exit_code == 2 # Click parameter validation error
307+
assert "non_existent_junit.xml" in result.output
308+
assert "does not exist" in result.output
309+
assert "previous CI step failed" in result.output
310+
assert (
311+
"check if your test execution step completed successfully" in result.output
312+
)

0 commit comments

Comments
 (0)