Repository: kedro-org/kedro
File: tests/framework/cli/conftest.py (Python)

Code before:
    import_module(PACKAGE_NAME)
    configure_project(PACKAGE_NAME)
    yield fake_kedro_cli

    # reset side-effects of configure_project
    pipelines.configure()

    for key, value in old_settings.items():
        settings.set(key, value)
    sys.path = old_path

    # configure_project does imports that add PACKAGE_NAME.pipelines,
    # PACKAGE_NAME.settings to sys.modules. These need to be removed.
    # Ideally we would reset sys.modules to exactly what it was before
    # running anything, but removal of distutils.build.commands from
    # sys.modules mysteriously makes some tests for `kedro micropkg package`
    # fail on Windows, Python 3.7 and 3.8.
    for module in list(sys.modules.keys()):
        if module.startswith(PACKAGE_NAME):
            del sys.modules[module]


@fixture(scope="module", params=[None, KedroDataCatalog])
def fake_project_cli_parametrized(
    fake_repo_path: Path,
    dummy_config: Path,
    fake_kedro_cli: click.CommandCollection,
    request,
):
    # TODO: remove parametrization after removing old catalog as KedroDataCatalog will be default
    default_catalog = request.param
    old_settings = settings.as_dict()
    starter_path = Path(__file__).resolve().parents[3]
    starter_path = starter_path / "features" / "steps" / "test_starter"
    CliRunner().invoke(
        fake_kedro_cli, ["new", "-c", str(dummy_config), "--starter", str(starter_path)]
    )
    # Delete the project logging.yml, which leaves behind info.log and error.log files.
    # This leaves logging config as the framework default.
    try:
        (fake_repo_path / "conf" / "logging.yml").unlink()
    except FileNotFoundError:
        pass

    # NOTE: Here we load a couple of modules, as they would be imported in
    # the code and tests.
    # It's safe to remove the new entries from path due to the python
    # module caching mechanism. Any `reload` on it will not work though.
    old_path = sys.path.copy()
    sys.path = [str(fake_repo_path / "src"), *sys.path]


Review comment (suggestion):
Btw, did you try changing the scope already? Maybe if this is set to `class` scope instead, it won't show have the same problem with parameterisation because it will instantiate the fixture fresh. I saw that the issue is with the pipeline create CLI tests. I didn't dig too far, but looked like it was a matter of needing a clean environment. 

Code after:
    import_module(PACKAGE_NAME)
    configure_project(PACKAGE_NAME)
    yield fake_kedro_cli

    # reset side-effects of configure_project
    pipelines.configure()

    for key, value in old_settings.items():
        settings.set(key, value)
    sys.path = old_path

    # configure_project does imports that add PACKAGE_NAME.pipelines,
    # PACKAGE_NAME.settings to sys.modules. These need to be removed.
    # Ideally we would reset sys.modules to exactly what it was before
    # running anything, but removal of distutils.build.commands from
    # sys.modules mysteriously makes some tests for `kedro micropkg package`
    # fail on Windows, Python 3.7 and 3.8.
    for module in list(sys.modules.keys()):
        if module.startswith(PACKAGE_NAME):
            del sys.modules[module]


# We use `None` as the first parameter since the default `DATA_CATALOG_CLASS`
# set in `settings.py` is `DataCatalog`. We do not set `DataCatalog` explicitly
# in fixture params to test loading the default class.
@fixture(scope="module", params=[None, KedroDataCatalog])
def fake_project_cli_parametrized(
    fake_repo_path: Path,
    dummy_config: Path,
    fake_kedro_cli: click.CommandCollection,
    request,
):
    # TODO: remove parametrization after removing old catalog as KedroDataCatalog will be default
    default_catalog = request.param
    old_settings = settings.as_dict()
    starter_path = Path(__file__).resolve().parents[3]
    starter_path = starter_path / "features" / "steps" / "test_starter"
    CliRunner().invoke(
        fake_kedro_cli, ["new", "-c", str(dummy_config), "--starter", str(starter_path)]
    )
    # Delete the project logging.yml, which leaves behind info.log and error.log files.
    # This leaves logging config as the framework default.
    try:
        (fake_repo_path / "conf" / "logging.yml").unlink()
    except FileNotFoundError:
        pass

    # NOTE: Here we load a couple of modules, as they would be imported in
    # the code and tests.
    # It's safe to remove the new entries from path due to the python
    # module caching mechanism. Any `reload` on it will not work though.