Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up macOS test apps on failure #117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

marcoesters
Copy link
Contributor

Description

If the test apps of the test package already exist, the menuinst tests will fail. If there is an AssertionError in these tests, the files are currently not cleaned up either.

Checklist - did you ...

  • Add a file to the news directory (using the template) for the next release's release notes?
  • Add / update necessary tests?
  • Add / update outdated documentation?

@conda-bot conda-bot added the cla-signed [bot] added once the contributor has signed the CLA label Dec 3, 2024
@marcoesters marcoesters marked this pull request as ready for review December 3, 2024 21:35
@marcoesters marcoesters requested a review from a team as a code owner December 3, 2024 21:35
Copy link
Member

@jezdez jezdez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more pytest-native to use a fixture to have pre and post-run cleanup tasks: https://docs.pytest.org/en/stable/how-to/fixtures.html#yield-fixtures-recommended

A fixture you could use for example (untested):

# If the test app already exists, this test will fail,
# so clean up before and after the run.
def _clean_macos_apps(shortcuts: list[Path]):
    if not sys.platform == "darwin":
        return
    for shortcut in shortcuts:
        if shortcut.exists():
            shutil.rmtree(shortcut)

@pytest.fixture
def clean_shortcuts(tmp_path: Path, shortcut_path: dict[str, str]):
    # The shortcut will take 'root_prefix' as the base, but conda-standalone
    # sets that to its temporary 'sys.prefix' as provided by the pyinstaller
    # self-extraction. We override it via 'CONDA_ROOT_PREFIX' in the same
    # way 'constructor' will do it.
    variables = {"base": Path(sys.prefix).name, "name": tmp_path.name}
    shortcuts = [
        folder / shortcut_path[sys.platform].format(**variables)
        for folder in _get_shortcut_dirs()
    ]
    _clean_macos_apps(shortcuts)
    yield {"tmp_path": tmp_path, "shortcuts": shortcuts}
    _clean_macos_apps(shortcuts)

Please also stop importing conftest since this is just a pytest configuration module, not a regular Python module to store test tools or configuration in. Simply convert the values there into pytest fixtures, which will make the tests a lot more resilient to test reordering.

@marcoesters
Copy link
Contributor Author

It's more pytest-native to use a fixture to have pre and post-run cleanup tasks: https://docs.pytest.org/en/stable/how-to/fixtures.html#yield-fixtures-recommended

A fixture you could use for example (untested):

# If the test app already exists, this test will fail,
# so clean up before and after the run.
def _clean_macos_apps(shortcuts: list[Path]):
    if not sys.platform == "darwin":
        return
    for shortcut in shortcuts:
        if shortcut.exists():
            shutil.rmtree(shortcut)

@pytest.fixture
def clean_shortcuts(tmp_path: Path, shortcut_path: dict[str, str]):
    # The shortcut will take 'root_prefix' as the base, but conda-standalone
    # sets that to its temporary 'sys.prefix' as provided by the pyinstaller
    # self-extraction. We override it via 'CONDA_ROOT_PREFIX' in the same
    # way 'constructor' will do it.
    variables = {"base": Path(sys.prefix).name, "name": tmp_path.name}
    shortcuts = [
        folder / shortcut_path[sys.platform].format(**variables)
        for folder in _get_shortcut_dirs()
    ]
    _clean_macos_apps(shortcuts)
    yield {"tmp_path": tmp_path, "shortcuts": shortcuts}
    _clean_macos_apps(shortcuts)

Please also stop importing conftest since this is just a pytest configuration module, not a regular Python module to store test tools or configuration in. Simply convert the values there into pytest fixtures, which will make the tests a lot more resilient to test reordering.

I created that fixture, but I'm not sure I like the solution. The problem is the format statement, which is not fixed for all purposes. It is used, here, too:

variables = {
"base": base_env.name,
"name": shortcut_env.name,
}

This makes the fixture a little limited.

@marcoesters marcoesters requested a review from jezdez January 8, 2025 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed [bot] added once the contributor has signed the CLA
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

3 participants