|
18 | 18 | from tests.conftest import AssertStr
|
19 | 19 |
|
20 | 20 |
|
21 |
| -# TODO: |
22 |
| -# I need to make it so these tests don't cause side effects |
23 |
| -# pytest.skip(allow_module_level=True) |
24 |
| - |
25 |
| - |
26 | 21 | @pytest.fixture(autouse=True)
|
27 | 22 | def default_config(initialize_rich_click: None) -> None:
|
28 | 23 | # Default config settings from https://github.com/Textualize/rich/blob/master/tests/render.py
|
@@ -373,3 +368,60 @@ def cli():
|
373 | 368 | """
|
374 | 369 |
|
375 | 370 | assert_str(actual=res.stdout.decode(), expectation=expected_output)
|
| 371 | + |
| 372 | + |
| 373 | +@pytest.mark.skipif(CLICK_IS_BEFORE_VERSION_8X, reason="Warning message gets in the way.") |
| 374 | +def test_error_to_stderr(mock_script_writer: Callable[[str], Path], assert_str: AssertStr) -> None: |
| 375 | + mock_script_writer( |
| 376 | + ''' |
| 377 | + import click |
| 378 | +
|
| 379 | + @click.group("foo") |
| 380 | + def foo(): |
| 381 | + """foo group""" |
| 382 | +
|
| 383 | + @foo.command("bar") |
| 384 | + def bar(): |
| 385 | + """bar command""" |
| 386 | + ''' |
| 387 | + ) |
| 388 | + |
| 389 | + res_grp = subprocess.run( |
| 390 | + [sys.executable, "-m", "src.rich_click", "mymodule:foo", "--bad-input"], |
| 391 | + stdout=subprocess.PIPE, |
| 392 | + stderr=subprocess.PIPE, |
| 393 | + env={**os.environ, "TERMINAL_WIDTH": "100", "FORCE_COLOR": "False"}, |
| 394 | + ) |
| 395 | + assert res_grp.returncode == 2 |
| 396 | + |
| 397 | + expected_output_grp = """ |
| 398 | + Usage: python -m src.rich_click.mymodule [OPTIONS] COMMAND [ARGS]... |
| 399 | +
|
| 400 | + Try 'python -m src.rich_click.mymodule --help' for help |
| 401 | + ╭─ Error ──────────────────────────────────────────────────────────────────────────────────────────╮ |
| 402 | + │ No such option: --bad-input │ |
| 403 | + ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ |
| 404 | + """ |
| 405 | + |
| 406 | + assert_str(actual=res_grp.stdout.decode(), expectation="") |
| 407 | + assert_str(actual=res_grp.stderr.decode(), expectation=expected_output_grp) |
| 408 | + |
| 409 | + res_cmd = subprocess.run( |
| 410 | + [sys.executable, "-m", "src.rich_click", "mymodule:foo", "bar", "--bad-input"], |
| 411 | + stdout=subprocess.PIPE, |
| 412 | + stderr=subprocess.PIPE, |
| 413 | + env={**os.environ, "TERMINAL_WIDTH": "100", "FORCE_COLOR": "False"}, |
| 414 | + ) |
| 415 | + assert res_cmd.returncode == 2 |
| 416 | + |
| 417 | + expected_output_grp = """ |
| 418 | + Usage: python -m src.rich_click.mymodule bar [OPTIONS] |
| 419 | +
|
| 420 | + Try 'python -m src.rich_click.mymodule bar --help' for help |
| 421 | + ╭─ Error ──────────────────────────────────────────────────────────────────────────────────────────╮ |
| 422 | + │ No such option: --bad-input │ |
| 423 | + ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ |
| 424 | + """ |
| 425 | + |
| 426 | + assert_str(actual=res_cmd.stdout.decode(), expectation="") |
| 427 | + assert_str(actual=res_cmd.stderr.decode(), expectation=expected_output_grp) |
0 commit comments