def test_rich_progress_bar_keyboard_interrupt(tmp_path):
    """Test to ensure that when the user keyboard interrupts, we close the progress bar."""

    class TestModel(BoringModel):
        def on_train_start(self) -> None:
            raise KeyboardInterrupt

    model = TestModel()

    with (
        mock.patch(
            "lightning.pytorch.callbacks.progress.rich_progress.Progress.stop", autospec=True
        ) as mock_progress_stop,
        pytest.raises(SystemExit),
    ):
        progress_bar = RichProgressBar()
        trainer = Trainer(
            default_root_dir=tmp_path,
            fast_dev_run=True,
            callbacks=progress_bar,
        )

        trainer.fit(model)
    mock_progress_stop.assert_called_once()
Test to ensure that when the user keyboard interrupts, we close the progress bar.
tests/tests_pytorch/callbacks/progress/test_rich_progress_bar.py