def test_resume_and_old_checkpoint_files_remain(same_resume_folder, tmp_path):
    """Test that checkpoints saved in the resume-folder won't be deleted under the save-top-k mechanism."""
    model = BoringModel()
    trainer_kwargs = {
        "default_root_dir": tmp_path,
        "limit_train_batches": 10,
        "limit_val_batches": 0,
        "enable_progress_bar": False,
        "enable_model_summary": False,
        "logger": False,
    }
    first = tmp_path / "first"
    second = tmp_path / "second"
    new_dirpath = first if same_resume_folder else second

    # Generate checkpoints in the first folder
    callback = ModelCheckpoint(dirpath=first, monitor="step", mode="max", save_top_k=2, every_n_train_steps=2)
    trainer = Trainer(callbacks=callback, max_steps=5, **trainer_kwargs)
    trainer.fit(model)
    assert set(os.listdir(first)) == {"epoch=0-step=2.ckpt", "epoch=0-step=4.ckpt"}

    # Continue training from checkpoint
    callback = ModelCheckpoint(dirpath=new_dirpath, monitor="step", mode="max", save_top_k=2, every_n_train_steps=2)
    trainer = Trainer(callbacks=callback, max_steps=8, **trainer_kwargs)
    trainer.fit(model, ckpt_path=str(first / "epoch=0-step=4.ckpt"))
    if same_resume_folder:
        assert set(os.listdir(first)) == {
            "epoch=0-step=4.ckpt",  # do not delete checkpoint from which we resume from
            "epoch=0-step=6.ckpt",
            "epoch=0-step=8.ckpt",
        }
    else:
        assert set(os.listdir(first)) == {"epoch=0-step=2.ckpt", "epoch=0-step=4.ckpt"}  # no files deleted
        assert set(os.listdir(second)) == {"epoch=0-step=6.ckpt", "epoch=0-step=8.ckpt"}
Test that checkpoints saved in the resume-folder won't be deleted under the save-top-k mechanism.
tests/tests_pytorch/checkpointing/test_model_checkpoint.py