12rambau / pytest-copie

The pytest plugin for your copier templates 📒
https://pytest-copie.readthedocs.io/en/latest/
MIT License
13 stars 2 forks source link

Support different fixtures for scopes #77

Open tschm opened 3 months ago

tschm commented 3 months ago

The tmp_path fixture has scope "function". Hence your copie fixture will always have the same scope which makes it less useful than it could be.

I have worked first on a fixture for creating a temp_path (for dst) that has a larger scope, e.g

@pytest.fixture(scope="session")
def copier_dst(tmp_path_factory):
    target = tmp_path_factory.mktemp("copier")
    LOGGER.info(target)
    return target

and then doing the expensive construction of the template only once for all tests...

@pytest.fixture(scope="module")
def result(src_path, copier_dst):
    run_copy(
        src_path=src_path,
        dst_path=copier_dst,
        unsafe=True,
        defaults=True,
        user_defaults={"project_name": "maffay"},
    )

    os.chdir(copier_dst)

    # fire off poetry
    os.system("poetry update -vv")
    # yield the folder
    yield copier_dst
    # the path is deleted at the very end when all tests have finished
    shutil.rmtree(copier_dst)

I am now exposing the built template and can run various tests against it.

What do you think? Please share your thoughts.

12rambau commented 3 months ago

Hi @tschm and thanks for trying pytest-copie!

Your use case is quite different from the design idea I had in mind.

To me, every time I'm calling back the constructor means that I'm trying a new configuration thus I want to start from scratch. For running multiple test on the resulting build, I usually run the template test suit like in this repository: https://github.com/12rambau/pypackage/blob/main/tests/test_build.py

What would happen with a session scope constructor if anything goes wrong in one of your tests ? file corruption, deletion of folder, pre-commit modifications etc ....