def test_model_checkpoint_save_last(save_last, tmp_path, monkeypatch):
    """Tests that save_last produces only one last checkpoint."""
    seed_everything()
    model = LogInTwoMethods()
    epochs = 3
    monkeypatch.setattr(ModelCheckpoint, "CHECKPOINT_NAME_LAST", "last-{epoch}")
    model_checkpoint = ModelCheckpoint(monitor="early_stop_on", dirpath=tmp_path, save_top_k=-1, save_last=save_last)
    trainer = Trainer(
        default_root_dir=tmp_path,
        callbacks=[model_checkpoint],
        max_epochs=epochs,
        limit_train_batches=10,
        limit_val_batches=10,
        logger=False,
    )
    trainer.fit(model)
    last_filename = model_checkpoint._format_checkpoint_name(
        ModelCheckpoint.CHECKPOINT_NAME_LAST, {"epoch": trainer.current_epoch - 1}
    )
    last_filename = last_filename + ".ckpt"
    assert str(tmp_path / last_filename) == model_checkpoint.last_model_path
    assert set(os.listdir(tmp_path)) == set(
        [f"epoch={i}-step={j}.ckpt" for i, j in zip(range(epochs), [10, 20, 30])] + [last_filename]
    )
    if save_last == "link":
        assert os.path.islink(tmp_path / last_filename)
    else:
        assert os.path.isfile(tmp_path / last_filename)
    assert os.path.realpath(tmp_path / last_filename) == model_checkpoint._last_checkpoint_saved
Tests that save_last produces only one last checkpoint.
tests/tests_pytorch/checkpointing/test_model_checkpoint.py