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

Deprecate conda.testing.integration.make_temp_env in favor of conda.testing.tmp_env #12680

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
543c8aa
Deprecate make_temp_env
kenodegard May 16, 2023
3807bb7
Convert test_priority.py to pytest style
kenodegard May 25, 2023
1eedcc5
Convert test_export.py to pytest style
kenodegard May 25, 2023
ee9bca4
Add tmp_env as helper function in BaseTestCase
kenodegard May 25, 2023
f0f3978
Correct deprecation releases
kenodegard May 25, 2023
443e96d
Merge remote-tracking branch 'upstream/main' into deprecate-make_temp…
kenodegard May 25, 2023
99d27ae
Reconvert test_priority.py to pytest style
kenodegard May 25, 2023
9b4ca60
Cleanup test_cli.py
kenodegard May 25, 2023
c02b5f0
Update test_priority.py
kenodegard May 25, 2023
480d097
Switch from run_command to conda_cli
kenodegard May 25, 2023
833d7be
Refactor test_priority.py
kenodegard May 26, 2023
4d1c665
Drop unused tmp_env
kenodegard May 26, 2023
dccd775
Remove legacy kwargs from tmp_env
kenodegard May 26, 2023
2f8713c
Stringify prefix before passing to run_command
kenodegard May 26, 2023
9306048
Update test_create tests
kenodegard May 26, 2023
09d615b
Update test_create.py
kenodegard May 26, 2023
4c43591
Update test_create.py
kenodegard May 26, 2023
ebb7491
Update test_cli.py
kenodegard May 26, 2023
b8cf9a2
Merge branch 'main' into deprecate-make_temp_env
kenodegard May 26, 2023
ea46da9
Merge remote-tracking branch 'upstream/main' into deprecate-make_temp…
kenodegard Jun 2, 2023
0f7a966
Merge remote-tracking branch 'upstream/main' into deprecate-make_temp…
kenodegard Jun 17, 2023
6903254
Merge remote-tracking branch 'upstream/main' into deprecate-make_temp…
kenodegard Jun 30, 2023
825fb1c
Revert BaseTestCase change
kenodegard Jun 30, 2023
d2d21f4
Remove unused imports
kenodegard Jun 30, 2023
f9731f1
Merge remote-tracking branch 'upstream/main' into deprecate-make_temp…
kenodegard Jul 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conda/core/prefix_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class PrefixDataType(type):
"""Basic caching of PrefixData instance objects."""

def __call__(cls, prefix_path, pip_interop_enabled=None):
prefix_path = str(prefix_path)
if prefix_path in PrefixData._cache_:
return PrefixData._cache_[prefix_path]
elif isinstance(prefix_path, PrefixData):
Expand Down
9 changes: 6 additions & 3 deletions conda/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,12 @@ def __call__(self, *argv: str) -> tuple[str, str, int]:
context.__init__(argparse_args=args)
init_loggers(context)

# run command
code = do_call(args, parser)
out, err = self.capsys.readouterr()
try:
# run command
code = do_call(args, parser)
finally:
# always flush stdout and stderr
out, err = self.capsys.readouterr()

# restore to prior state
reset_context()
Expand Down
4 changes: 2 additions & 2 deletions conda/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ def capture_json_with_argv(
addendum="Use `mocker.patch('conda.base.context.Context.active_prefix')` instead.",
)
@contextmanager
def set_active_prefix(prefix: str) -> None:
def set_active_prefix(prefix: os.PathLike) -> None:
old_prefix = os.environ["CONDA_PREFIX"]

try:
os.environ["CONDA_PREFIX"] = prefix
os.environ["CONDA_PREFIX"] = str(prefix)
yield
finally:
os.environ["CONDA_PREFIX"] = old_prefix
Expand Down
6 changes: 4 additions & 2 deletions conda/testing/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def run_command(command, prefix, *arguments, **kwargs):
Commands.RUN,
):
arguments.insert(0, "-p")
arguments.insert(1, prefix)
arguments.insert(1, str(prefix))
if command in (Commands.CREATE, Commands.INSTALL, Commands.REMOVE, Commands.UPDATE):
arguments.extend(["-y", "-q"])

Expand Down Expand Up @@ -294,10 +294,11 @@ def run_command(command, prefix, *arguments, **kwargs):
# Unfortunately there are other ways to change context, such as Commands.CREATE --offline.
# You will probably end up playing whack-a-bug here adding more and more the tuple here.
if command in (Commands.CONFIG,):
reset_context([os.path.join(prefix + os.sep, "condarc")], args)
reset_context([os.path.join(prefix, "condarc")], args)
return stdout, stderr, result


@deprecated("24.3", "24.9", addendum="Use `conda.testing.tmp_env` instead.")
@contextmanager
def make_temp_env(*packages, **kwargs):
name = kwargs.pop("name", None)
Expand Down Expand Up @@ -438,6 +439,7 @@ def package_is_installed(prefix, spec):


def _package_is_installed(prefix, spec):
PrefixData._cache_.clear()
spec = MatchSpec(spec)
prefix_recs = tuple(PrefixData(prefix).query(spec))
if len(prefix_recs) > 1:
Expand Down
55 changes: 29 additions & 26 deletions tests/cli/test_main_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import json
from datetime import datetime
from os import PathLike, walk
from os import walk
from os.path import basename, exists, isdir, join
from pathlib import Path
from shutil import copy

import pytest
from pytest_mock import MockerFixture

from conda.base.constants import (
CONDA_LOGS_DIR,
Expand All @@ -18,12 +20,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 +69,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 +90,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 @@ -112,7 +110,7 @@ def test_clean_and_packages(clear_cache):
# pkg still exists since its in use by temp env
assert_any_pkg(pkg, _get_pkgs(pkgs_dir))

run_command(Commands.REMOVE, prefix, pkg, "--yes", "--json")
run_command(Commands.REMOVE, str(prefix), pkg, "--yes", "--json")
stdout, _, _ = run_command(
Commands.CLEAN, "", "--packages", "--yes", "--json"
)
Expand All @@ -126,14 +124,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 +149,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 +173,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 +190,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 +213,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 +225,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 +251,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 +264,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 +282,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 +305,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 +325,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
3 changes: 1 addition & 2 deletions tests/core/test_subdir_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from conda.models.channel import Channel
from conda.models.records import PackageRecord
from conda.testing.helpers import CHANNEL_DIR
from conda.testing.integration import make_temp_env

log = getLogger(__name__)

Expand Down Expand Up @@ -233,7 +232,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(), env_vars(
{"CONDA_PLATFORM": platform, "CONDA_SSL_VERIFY": "false"},
stack_callback=conda_tests_ctxt_mgmt_def_pol,
):
Expand Down
11 changes: 5 additions & 6 deletions tests/gateways/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,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 @@ -69,7 +70,7 @@ def test_local_file_adapter_200():

@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 @@ -97,14 +98,12 @@ 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",
"--no-deps", # this fake repo only includes the zlib tarball
"zlib",
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
16 changes: 6 additions & 10 deletions tests/plugins/subcommands/doctor/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
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():
"""Make sure that we are able to call the ``conda doctor`` command"""

out, err, code = run(f"conda doctor")
out, err, code = run("conda doctor")

assert not err # no error message
assert not code # successful exit code


def test_conda_doctor_happy_path_verbose():
"""Make sure that we are able to run ``conda doctor`` command with the --verbose flag"""

out, err, code = run(f"conda doctor --verbose")
out, err, code = run("conda doctor --verbose")

assert not err # no error message
assert not code # successful exit code


def test_conda_doctor_happy_path_show_help():
"""Make sure that we are able to run ``conda doctor`` command with the --help flag"""

out, err, code = run(f"conda doctor --help")
out, err, code = run("conda doctor --help")

assert "Display a health report for your environment." in out
assert not err # no error message
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 "There are no packages with missing files." in out
Expand Down
3 changes: 0 additions & 3 deletions tests/plugins/subcommands/doctor/test_health_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
from pytest import MonkeyPatch

from conda.base.context import reset_context
from conda.common.io import env_vars
from conda.plugins.subcommands.doctor.health_checks import (
display_health_checks,
find_altered_packages,
find_packages_with_missing_files,
)
from conda.testing.integration import make_temp_env


@pytest.fixture
Expand Down Expand Up @@ -135,7 +133,6 @@ def test_json_cannot_be_loaded(env_ok: tuple[Path, str, str, str]):
"""Test that runs for the case when json file is missing"""
prefix, _, _, package = env_ok
# passing a None type to json.loads() so that it fails
package = None
assert find_altered_packages(prefix) == {}


Expand Down
Loading
Loading