|
9 | 9 | from mergify_cli.ci import upload |
10 | 10 |
|
11 | 11 |
|
| 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 | + |
12 | 42 | def _process_tests_target_branch( |
13 | 43 | _ctx: click.Context, |
14 | 44 | _param: click.Parameter, |
@@ -68,7 +98,7 @@ def _process_tests_target_branch( |
68 | 98 | "files", |
69 | 99 | nargs=-1, |
70 | 100 | required=True, |
71 | | - type=click.Path(exists=True, dir_okay=False), |
| 101 | + type=JUnitFile(), |
72 | 102 | ) |
73 | 103 | @utils.run_with_asyncio |
74 | 104 | async def junit_upload( # noqa: PLR0913 |
@@ -139,7 +169,7 @@ async def junit_upload( # noqa: PLR0913 |
139 | 169 | "files", |
140 | 170 | nargs=-1, |
141 | 171 | required=True, |
142 | | - type=click.Path(exists=True, dir_okay=False), |
| 172 | + type=JUnitFile(), |
143 | 173 | ) |
144 | 174 | @utils.run_with_asyncio |
145 | 175 | async def junit_process( # noqa: PLR0913 |
|
0 commit comments