Skip to content

Implement "keep alive" in fixture/test #12

@mgautierfr

Description

@mgautierfr

Sometime, tests or fixtures need a fixture for its side effect (temporary directory...) and don't directly use the value itself.

#[fixture]
fn tmp_dir() -> std::io::Result<tempfile::TmpDir> {
    tempfile::tempdir()
}

#[fixture]
fn tmp_path(tmp_dir: TmpDir) -> PathBuf {
    tmp_dir.path().to_owned()  // <- This will return a path to a non existant directory
                               //    tmp_dir will delete the directory when it is drop.
}

We need to implement a way to prevent the source fixture being drop too soon.

Some ideas of api:

#[fixture(keep=tmp_dir)]
fn tmp_path(tmp_dir: TmpDir) -> PathBuf {
    tmp_dir.path().to_owned()
}
#[fixture]
fn tmp_path(#[keep] tmp_dir: TmpDir) -> PathBuf {
    tmp_dir.path().to_owned()
}
#[fixture]
fn tmp_path(ctx: Context, tmp_dir: TmpDir) -> PathBuf {
    ctx.keep(tmp_dir);
    tmp_dir.path().to_owned()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions