Skip to content

Commit

Permalink
Deprecate make_temp_env
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard committed May 16, 2023
1 parent 21232ba commit 543c8aa
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 313 deletions.
1 change: 1 addition & 0 deletions conda/testing/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ def run_command(command, prefix, *arguments, **kwargs):
return stdout, stderr, result


@deprecated("23.9", "24.3", addendum="Use `conda.testing.tmp_env` instead.")
@contextmanager
def make_temp_env(*packages, **kwargs):
name = kwargs.pop("name", None)
Expand Down
49 changes: 25 additions & 24 deletions tests/cli/test_main_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@
from conda.cli.main_clean import _get_size
from conda.core.subdir_data import create_cache_dir
from conda.gateways.disk.create import mkdir_p
from conda.testing.integration import (
Commands,
make_temp_env,
make_temp_package_cache,
run_command,
)
from conda.testing import TmpEnvFixture
from conda.testing.integration import Commands, make_temp_package_cache, run_command


def _get_pkgs(pkgs_dir):
Expand Down Expand Up @@ -71,14 +67,14 @@ def assert_not_pkg(name, contents):


# conda clean --force-pkgs-dirs
def test_clean_force_pkgs_dirs(clear_cache):
def test_clean_force_pkgs_dirs(clear_cache, tmp_env: TmpEnvFixture):
pkg = "bzip2"

with make_temp_package_cache() as pkgs_dir:
# pkgs_dir is a directory
assert isdir(pkgs_dir)

with make_temp_env(pkg):
with tmp_env(pkg):
stdout, _, _ = run_command(
Commands.CLEAN, "", "--force-pkgs-dirs", "--yes", "--json"
)
Expand All @@ -92,14 +88,14 @@ def test_clean_force_pkgs_dirs(clear_cache):


# conda clean --packages
def test_clean_and_packages(clear_cache):
def test_clean_and_packages(clear_cache, tmp_env: TmpEnvFixture):
pkg = "bzip2"

with make_temp_package_cache() as pkgs_dir:
# pkg doesn't exist ahead of time
assert_not_pkg(pkg, _get_pkgs(pkgs_dir))

with make_temp_env(pkg) as prefix:
with tmp_env(pkg) as prefix:
# pkg exists
assert_any_pkg(pkg, _get_pkgs(pkgs_dir))

Expand All @@ -126,14 +122,14 @@ def test_clean_and_packages(clear_cache):


# conda clean --tarballs
def test_clean_tarballs(clear_cache):
def test_clean_tarballs(clear_cache, tmp_env: TmpEnvFixture):
pkg = "bzip2"

with make_temp_package_cache() as pkgs_dir:
# tarball doesn't exist ahead of time
assert_not_pkg(pkg, _get_tars(pkgs_dir))

with make_temp_env(pkg):
with tmp_env(pkg):
# tarball exists
assert_any_pkg(pkg, _get_tars(pkgs_dir))

Expand All @@ -151,14 +147,14 @@ def test_clean_tarballs(clear_cache):


# conda clean --index-cache
def test_clean_index_cache(clear_cache):
def test_clean_index_cache(clear_cache, tmp_env: TmpEnvFixture):
pkg = "bzip2"

with make_temp_package_cache():
# index cache doesn't exist ahead of time
assert not _get_index_cache()

with make_temp_env(pkg):
with tmp_env(pkg):
# index cache exists
assert _get_index_cache()

Expand All @@ -175,7 +171,7 @@ def test_clean_index_cache(clear_cache):


# conda clean --tempfiles
def test_clean_tempfiles(clear_cache):
def test_clean_tempfiles(clear_cache, tmp_env: TmpEnvFixture):
"""Tempfiles are either suffixed with .c~ or .trash.
.c~ is used to indicate that conda is actively using that file. If the conda process is
Expand All @@ -192,7 +188,7 @@ def test_clean_tempfiles(clear_cache):
# tempfiles don't exist ahead of time
assert not _get_tempfiles(pkgs_dir)

with make_temp_env(pkg):
with tmp_env(pkg):
# mimic tempfiles being created
path = _get_tars(pkgs_dir)[0] # grab any tarball
for ext in CONDA_TEMP_EXTENSIONS:
Expand All @@ -215,7 +211,7 @@ def test_clean_tempfiles(clear_cache):


# conda clean --logfiles
def test_clean_logfiles(clear_cache):
def test_clean_logfiles(clear_cache, tmp_env: TmpEnvFixture):
"""Logfiles are found in pkgs_dir/.logs.
Since these log files were uniquely created during the experimental
Expand All @@ -227,7 +223,7 @@ def test_clean_logfiles(clear_cache):
# logfiles don't exist ahead of time
assert not _get_logfiles(pkgs_dir)

with make_temp_env(pkg):
with tmp_env(pkg):
# mimic logfiles being created
logs = join(pkgs_dir, CONDA_LOGS_DIR)
mkdir_p(logs)
Expand All @@ -253,7 +249,7 @@ def test_clean_logfiles(clear_cache):

# conda clean --all [--verbose]
@pytest.mark.parametrize("verbose", [True, False])
def test_clean_all(clear_cache, verbose: bool):
def test_clean_all(clear_cache, verbose: bool, tmp_env: TmpEnvFixture):
pkg = "bzip2"
args = ("--yes", "--json")
if verbose:
Expand All @@ -266,7 +262,7 @@ def test_clean_all(clear_cache, verbose: bool):
assert_not_pkg(pkg, tars)
assert not cache

with make_temp_env(pkg) as prefix:
with tmp_env(pkg) as prefix:
# pkg, tarball, & index cache exists
pkgs, tars, cache = _get_all(pkgs_dir)
assert_any_pkg(pkg, pkgs)
Expand All @@ -284,7 +280,7 @@ def test_clean_all(clear_cache, verbose: bool):
assert_not_pkg(pkg, tars)
assert not cache

run_command(Commands.REMOVE, prefix, pkg, *args)
run_command(Commands.REMOVE, str(prefix), pkg, *args)
stdout, _, _ = run_command(Commands.CLEAN, "", "--packages", *args)
json.loads(stdout) # assert valid json

Expand All @@ -307,13 +303,18 @@ def test_clean_all(clear_cache, verbose: bool):

# conda clean --all --verbose
@pytest.mark.parametrize("as_json", [True, False])
def test_clean_all_mock_lstat(clear_cache, mocker: MockerFixture, as_json: bool):
def test_clean_all_mock_lstat(
clear_cache,
mocker: MockerFixture,
as_json: bool,
tmp_env: TmpEnvFixture,
):
pkg = "bzip2"
args = ("--yes", "--verbose")
if as_json:
args = (*args, "--json")

with make_temp_package_cache() as pkgs_dir, make_temp_env(pkg) as prefix:
with make_temp_package_cache() as pkgs_dir, tmp_env(pkg) as prefix:
# pkg, tarball, & index cache exists
pkgs, tars, cache = _get_all(pkgs_dir)
assert_any_pkg(pkg, pkgs)
Expand All @@ -322,7 +323,7 @@ def test_clean_all_mock_lstat(clear_cache, mocker: MockerFixture, as_json: bool)

mocker.patch("os.lstat", side_effect=OSError)

run_command(Commands.REMOVE, prefix, pkg, *args)
run_command(Commands.REMOVE, str(prefix), pkg, *args)
stdout, _, _ = run_command(Commands.CLEAN, "", "--packages", *args)
assert "WARNING:" in stdout
if as_json:
Expand Down
11 changes: 6 additions & 5 deletions tests/cli/test_main_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
# SPDX-License-Identifier: BSD-3-Clause
import json

from conda.testing.integration import Commands, make_temp_env, run_command
from conda.testing import TmpEnvFixture
from conda.testing.integration import Commands, run_command


# conda list
def test_list():
def test_list(tmp_env: TmpEnvFixture):
pkg = "ca-certificates" # has no dependencies
with make_temp_env(pkg) as prefix:
with tmp_env(pkg) as prefix:
stdout, _, _ = run_command(Commands.LIST, prefix, "--json")
assert any(item["name"] == pkg for item in json.loads(stdout))


# conda list --reverse
def test_list_reverse():
def test_list_reverse(tmp_env: TmpEnvFixture):
pkg = "curl" # has dependencies
with make_temp_env(pkg) as prefix:
with tmp_env(pkg) as prefix:
stdout, _, _ = run_command(Commands.LIST, prefix, "--json")
names = [item["name"] for item in json.loads(stdout)]
assert names == sorted(names)
Expand Down
6 changes: 3 additions & 3 deletions tests/core/test_subdir_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
)
from conda.models.channel import Channel
from conda.models.records import PackageRecord
from conda.testing import TmpEnvFixture
from conda.testing.helpers import CHANNEL_DIR
from conda.testing.integration import make_temp_env

log = getLogger(__name__)

Expand Down Expand Up @@ -289,7 +289,7 @@ def test_use_only_tar_bz2(platform=OVERRIDE_PLATFORM):
assert precs[0].fn.endswith(".conda")


def test_subdir_data_coverage(platform=OVERRIDE_PLATFORM):
def test_subdir_data_coverage(tmp_env: TmpEnvFixture, platform=OVERRIDE_PLATFORM):
class ChannelCacheClear:
def __enter__(self):
return
Expand All @@ -298,7 +298,7 @@ def __exit__(self, *exc):
Channel._cache_.clear()

# disable SSL_VERIFY to cover 'turn off warnings' line
with ChannelCacheClear(), make_temp_env(), env_vars(
with ChannelCacheClear(), tmp_env(), env_vars(
{"CONDA_PLATFORM": platform, "CONDA_SSL_VERIFY": "false"},
stack_callback=conda_tests_ctxt_mgmt_def_pol,
):
Expand Down
9 changes: 5 additions & 4 deletions tests/gateways/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from conda.gateways.anaconda_client import remove_binstar_token, set_binstar_token
from conda.gateways.connection.session import CondaHttpAuth, CondaSession
from conda.gateways.disk.delete import rm_rf
from conda.testing import TmpEnvFixture
from conda.testing.gateways.fixtures import MINIO_EXE
from conda.testing.integration import env_var, make_temp_env
from conda.testing.integration import env_var

log = getLogger(__name__)

Expand Down Expand Up @@ -71,7 +72,7 @@ def test_local_file_adapter_200(self):

@pytest.mark.skipif(MINIO_EXE is None, reason="Minio server not available")
@pytest.mark.integration
def test_s3_server(minio_s3_server):
def test_s3_server(minio_s3_server, tmp_env: TmpEnvFixture):
import boto3
from botocore.client import Config

Expand Down Expand Up @@ -99,7 +100,7 @@ def test_s3_server(minio_s3_server):
):
# the .conda files in this repo are somehow corrupted
with env_var("CONDA_USE_ONLY_TAR_BZ2", "True"):
with make_temp_env(
with tmp_env(
"--override-channels",
f"--channel=s3://{bucket_name}",
"--download-only",
Expand All @@ -108,5 +109,5 @@ def test_s3_server(minio_s3_server):
use_exception_handler=False,
no_capture=True,
):
# we just want to run make_temp_env and cleanup after
# we just want to run tmp_env and cleanup after
pass
14 changes: 7 additions & 7 deletions tests/plugins/subcommands/doctor/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
)
from conda.plugins.subcommands.doctor.cli import get_prefix
from conda.plugins.subcommands.doctor.health_checks import MISSING_FILES_SUCCESS_MESSAGE
from conda.testing import TmpEnvFixture
from conda.testing.helpers import run_inprocess_conda_command as run
from conda.testing.integration import make_temp_env


def test_conda_doctor_happy_path():
Expand Down Expand Up @@ -48,10 +48,10 @@ def test_conda_doctor_happy_path_show_help():
assert not code # successful exit code


def test_conda_doctor_with_test_environment():
def test_conda_doctor_with_test_environment(tmp_env: TmpEnvFixture):
"""Make sure that we are able to call ``conda doctor`` command for a specific environment"""

with make_temp_env() as prefix:
with tmp_env() as prefix:
out, err, code = run(f"conda doctor --prefix '{prefix}'")

assert MISSING_FILES_SUCCESS_MESSAGE in out
Expand All @@ -68,8 +68,8 @@ def test_get_prefix_bad_name():
get_prefix(Namespace(name="invalid", prefix=None))


def test_get_prefix_prefix():
with make_temp_env() as prefix:
def test_get_prefix_prefix(tmp_env: TmpEnvFixture):
with tmp_env() as prefix:
assert get_prefix(Namespace(name=None, prefix=prefix)) == prefix


Expand All @@ -78,8 +78,8 @@ def test_get_prefix_bad_prefix(tmp_path: Path):
assert get_prefix(Namespace(name=None, prefix=tmp_path))


def test_get_prefix_active():
with make_temp_env() as prefix, env_vars(
def test_get_prefix_active(tmp_env: TmpEnvFixture):
with tmp_env() as prefix, env_vars(
{"CONDA_PREFIX": prefix},
stack_callback=conda_tests_ctxt_mgmt_def_pol,
):
Expand Down
1 change: 0 additions & 1 deletion tests/plugins/subcommands/doctor/test_health_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
display_health_checks,
find_packages_with_missing_files,
)
from conda.testing.integration import make_temp_env


@pytest.fixture
Expand Down
16 changes: 6 additions & 10 deletions tests/test_activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@
from conda.gateways.disk.create import mkdir_p
from conda.gateways.disk.delete import rm_rf
from conda.gateways.disk.update import touch
from conda.testing import TmpEnvFixture
from conda.testing.helpers import tempdir
from conda.testing.integration import (
SPACER_CHARACTER,
Commands,
make_temp_env,
run_command,
)
from conda.testing.integration import SPACER_CHARACTER, Commands, run_command

log = getLogger(__name__)

Expand Down Expand Up @@ -3131,7 +3127,7 @@ def test_activate_deactivate_modify_path(shell, prefix, activate_deactivate_pack


@pytest.fixture(scope="module")
def create_stackable_envs():
def create_stackable_envs(tmp_env: TmpEnvFixture):
# generate stackable environments, two with curl and one without curl
which = f"{'where' if on_win else 'which -a'} curl"

Expand All @@ -3154,9 +3150,9 @@ def __init__(self, prefix=None, paths=None):
which,
)

with make_temp_env("curl", name="fake_base") as base:
with make_temp_env("curl", name="haspkg") as haspkg:
with make_temp_env(name="notpkg") as notpkg:
with tmp_env("curl") as base:
with tmp_env("curl") as haspkg:
with tmp_env() as notpkg:
yield which, {
"sys": Env(paths=sys),
"base": Env(prefix=base),
Expand Down
Loading

0 comments on commit 543c8aa

Please sign in to comment.