| 419 | ], |
| 420 | ) |
| 421 | def test_echo_via_pager(monkeypatch, capfd, pager_cmd, test, tmp_path): |
| 422 | monkeypatch.setitem(os.environ, "PAGER", pager_cmd) |
| 423 | monkeypatch.setattr(click._termui_impl, "isatty", lambda x: True) |
| 424 | |
| 425 | test_input = test.test_input() |
| 426 | expected_pager = test.expected_pager |
| 427 | expected_stdout = test.expected_stdout |
| 428 | expected_stderr = test.expected_stderr |
| 429 | expected_error = test.expected_error |
| 430 | |
| 431 | check_raise = pytest.raises(expected_error) if expected_error else nullcontext() |
| 432 | |
| 433 | pager_out_tmp = tmp_path / "pager_out.txt" |
| 434 | with pager_out_tmp.open("w") as f: |
| 435 | force_subprocess_stdout = patch.object( |
| 436 | subprocess, |
| 437 | "Popen", |
| 438 | partial(subprocess.Popen, stdout=f), |
| 439 | ) |
| 440 | with force_subprocess_stdout: |
| 441 | with check_raise: |
| 442 | click.echo_via_pager(test_input) |
| 443 | |
| 444 | out, err = capfd.readouterr() |
| 445 | |
| 446 | pager = pager_out_tmp.read_text() |
| 447 | |
| 448 | assert pager == expected_pager, ( |
| 449 | f"Unexpected pager output in test case '{test.description}'" |
| 450 | ) |
| 451 | assert out == expected_stdout, ( |
| 452 | f"Unexpected stdout in test case '{test.description}'" |
| 453 | ) |
| 454 | assert err == expected_stderr, ( |
| 455 | f"Unexpected stderr in test case '{test.description}'" |
| 456 | ) |
| 457 | |
| 458 | |
| 459 | @pytest.mark.skipif(WIN, reason="Different behavior on windows.") |