Skip to content

Commit

Permalink
Remove expensive conda-build autouse fixture (conda#11356)
Browse files Browse the repository at this point in the history
* Refactor activate test into parameterized

* Rework conda_build_recipes autouse fixture

* Remove unused pytest --shell arg

* Refactor unittest setup/teardown to pytest.fixture

* Bugfix & print removal

* Mark as an integration test
  • Loading branch information
kenodegard authored Mar 24, 2022
1 parent 8236c19 commit cab0d9d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 72 deletions.
12 changes: 6 additions & 6 deletions tests/cli/test_cli_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


@pytest.fixture
def fix_cli_install(tmpdir):
def prefix(tmpdir):
prefix = tmpdir.mkdir("cli_install_prefix")
test_env = tmpdir.mkdir("cli_install_test_env")
run_command(Commands.CREATE, str(prefix), 'python=3.7')
Expand All @@ -29,18 +29,18 @@ def fix_cli_install(tmpdir):


@pytest.mark.integration
def test_pre_link_message(fix_cli_install, conda_build_recipes):
prefix = fix_cli_install[0]
def test_pre_link_message(prefix, pre_link_messages_package):
prefix, _ = prefix
with patch("conda.cli.common.confirm_yn", return_value=True):
stdout, _, _ = run_command(
Commands.INSTALL, prefix, "pre_link_messages_package", "--use-local"
Commands.INSTALL, prefix, pre_link_messages_package, "--use-local"
)
assert "Lorem ipsum dolor sit amet" in stdout


@pytest.mark.integration
def test_find_conflicts_called_once(fix_cli_install):
prefix, test_env = fix_cli_install
def test_find_conflicts_called_once(prefix):
prefix, test_env = prefix
bad_deps = {'python': {((MatchSpec("statistics"), MatchSpec("python[version='>=2.7,<2.8.0a0']")), 'python=3')}}

with patch(
Expand Down
38 changes: 12 additions & 26 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,23 @@
# SPDX-License-Identifier: BSD-3-Clause
from pathlib import Path
import subprocess
import sys

import pytest

from conda.testing.fixtures import (
suppress_resource_warning,
tmpdir,
clear_subdir_cache,
)

win_default_shells = ["cmd.exe", "powershell", "git_bash", "cygwin"]
shells = ["bash", "zsh"]
if sys.platform == "win32":
shells = win_default_shells
def _conda_build_recipe(recipe):
subprocess.run(
["conda-build", str(Path(__file__).resolve().parent / "test-recipes" / recipe)],
check=True,
)
return recipe


def pytest_addoption(parser):
parser.addoption("--shell", action="append", default=[],
help="list of shells to run shell tests on")
@pytest.fixture(scope="session")
def activate_deactivate_package():
return _conda_build_recipe("activate_deactivate_package")


def pytest_generate_tests(metafunc):
if 'shell' in metafunc.fixturenames:
metafunc.parametrize("shell", metafunc.config.option.shell)


@pytest.fixture(scope="session", autouse=True)
def conda_build_recipes():
test_recipes = Path(__file__).resolve().parent / "test-recipes"
recipes_to_build = ["activate_deactivate_package", "pre_link_messages_package"]
packages = [str(test_recipes / pkg) for pkg in recipes_to_build]
cmd = ["conda-build"]
cmd.extend(packages)
subprocess.run(cmd, check=True)
@pytest.fixture(scope="session")
def pre_link_messages_package():
return _conda_build_recipe("pre_link_messages_package")
76 changes: 36 additions & 40 deletions tests/test_activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from re import escape
from collections import OrderedDict
from itertools import chain
from logging import getLogger
import os
from os.path import dirname, isdir, join
Expand Down Expand Up @@ -55,11 +54,6 @@
from conda.testing.integration import Commands, run_command, SPACER_CHARACTER
from conda.auxlib.decorators import memoize

try:
from unittest.mock import patch
except ImportError:
from mock import patch

log = getLogger(__name__)


Expand Down Expand Up @@ -2635,44 +2629,46 @@ def test_legacy_activate_deactivate_cmd_exe(self):
conda_shlvl = shell.get_env_var('CONDA_SHLVL')
assert conda_shlvl == '0', conda_shlvl

@pytest.mark.integration
class ActivationIntegrationTests(TestCase):
@pytest.fixture(scope="module")
def prefix():
tempdirdir = gettempdir()

def setUp(self):
tempdirdir = gettempdir()
root_dirname = str(uuid4())[:4] + SPACER_CHARACTER + str(uuid4())[:4]
root = join(tempdirdir, root_dirname)
mkdir_p(join(root, "conda-meta"))
assert isdir(root)
touch(join(root, "conda-meta", "history"))

prefix_dirname = str(uuid4())[:4] + SPACER_CHARACTER + str(uuid4())[:4]
self.prefix = join(tempdirdir, prefix_dirname)
mkdir_p(join(self.prefix, 'conda-meta'))
assert isdir(self.prefix)
touch(join(self.prefix, 'conda-meta', 'history'))
prefix = join(root, "envs", "charizard")
mkdir_p(join(prefix, "conda-meta"))
touch(join(prefix, "conda-meta", "history"))

self.prefix2 = join(self.prefix, 'envs', 'charizard')
mkdir_p(join(self.prefix2, 'conda-meta'))
touch(join(self.prefix2, 'conda-meta', 'history'))

def tearDown(self):
rm_rf(self.prefix)
rm_rf(self.prefix2)

def activate_deactivate_modify_path(self, shell):
activate_deactivate_package = "activate_deactivate_package"
activate_deactivate_package_path_string = "teststringfromactivate/bin/test"
original_path = os.environ.get("PATH")
run_command(Commands.INSTALL, self.prefix2, activate_deactivate_package, "--use-local")
yield prefix

with InteractiveShell(shell) as shell:
shell.sendline('conda activate "%s"' % self.prefix2)
activated_env_path = shell.get_env_var("PATH")
shell.sendline('conda deactivate')
rm_rf(root)

assert activate_deactivate_package_path_string in activated_env_path
assert original_path == os.environ.get("PATH")
@pytest.mark.integration
@pytest.mark.parametrize(
["shell"],
[
pytest.param(
"bash",
marks=pytest.mark.skipif(bash_unsupported(), reason=bash_unsupported_because()),
),
pytest.param(
"cmd.exe",
marks=pytest.mark.skipif(not which("cmd.exe"), reason="cmd.exe not installed"),
),
],
)
def test_activate_deactivate_modify_path(shell, prefix, activate_deactivate_package):
original_path = os.environ.get("PATH")
run_command(Commands.INSTALL, prefix, activate_deactivate_package, "--use-local")

@pytest.mark.skipif(bash_unsupported(), reason=bash_unsupported_because())
def test_activate_deactivate_modify_path_bash(self):
self.activate_deactivate_modify_path("bash")
with InteractiveShell(shell) as sh:
sh.sendline('conda activate "%s"' % prefix)
activated_env_path = sh.get_env_var("PATH")
sh.sendline("conda deactivate")

@pytest.mark.skipif(not which('cmd.exe'), reason='cmd.exe not installed')
def test_activate_deactivate_modify_path(self):
self.activate_deactivate_modify_path("cmd.exe")
assert "teststringfromactivate/bin/test" in activated_env_path
assert original_path == os.environ.get("PATH")

0 comments on commit cab0d9d

Please sign in to comment.