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

Replace _get_temp_prefix with path_factory #529

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 6 additions & 19 deletions tests/channel_testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import socket
import subprocess
import sys
from typing import TYPE_CHECKING

import pytest
from conda.testing.integration import _get_temp_prefix, run_command
from xprocess import ProcessStarter

if TYPE_CHECKING:
from conda.testing.fixtures import PathFactoryFixture


def _dummy_http_server(xprocess, name, port, auth="none", user=None, password=None, token=None):
"""
Expand Down Expand Up @@ -134,16 +137,15 @@ def http_server_auth_token(xprocess):


def create_with_channel(
channel, solver="libmamba", check=True, **kwargs
channel, path_factory: PathFactoryFixture, solver="libmamba", check=True, **kwargs
) -> subprocess.CompletedProcess:
return subprocess.run(
[
sys.executable,
"-m",
"conda",
"create",
"-p",
_get_temp_prefix(),
f"--prefix={path_factory()}",
f"--solver={solver}",
"--json",
"--override-channels",
Expand All @@ -154,18 +156,3 @@ def create_with_channel(
check=check,
**kwargs,
)


def create_with_channel_in_process(channel, solver="libmamba", **kwargs) -> tuple[str, str, int]:
stdout, stderr, returncode = run_command(
"create",
_get_temp_prefix(),
f"--solver={solver}",
"--json",
"--override-channels",
"-c",
channel,
"test-package",
**kwargs,
)
return stdout, stderr, returncode
63 changes: 40 additions & 23 deletions tests/test_channels.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Copyright (C) 2022 Anaconda, Inc
# Copyright (C) 2023 conda
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations

import json
import os
import shutil
import sys
from pathlib import Path
from subprocess import check_call
from typing import TYPE_CHECKING
from urllib.request import urlretrieve

import pytest
Expand All @@ -15,11 +18,7 @@
from conda.common.io import env_vars
from conda.core.prefix_data import PrefixData
from conda.models.channel import Channel
from conda.testing.integration import (
_get_temp_prefix,
make_temp_env,
package_is_installed,
)
from conda.testing.integration import make_temp_env, package_is_installed
from conda.testing.integration import run_command as conda_inprocess

from .channel_testing.helpers import (
Expand All @@ -31,13 +30,16 @@
)
from .utils import conda_subprocess, write_env_config

if TYPE_CHECKING:
from conda.testing.fixtures import PathFactoryFixture

DATA = Path(__file__).parent / "data"


def test_channel_matchspec():
def test_channel_matchspec(path_factory: PathFactoryFixture) -> None:
stdout, *_ = conda_inprocess(
"create",
_get_temp_prefix(),
str(path_factory()),
"--solver=libmamba",
"--json",
"--override-channels",
Expand Down Expand Up @@ -141,7 +143,7 @@ def _setup_channels_custom(prefix, force=False):
_setup_channels_custom,
),
)
def test_mirrors_do_not_leak_channels(config_env, tmp_path, tmp_env):
def test_mirrors_do_not_leak_channels(config_env, path_factory: PathFactoryFixture) -> None:
"""
https://github.com/conda/conda-libmamba-solver/issues/108

Expand All @@ -153,7 +155,7 @@ def test_mirrors_do_not_leak_channels(config_env, tmp_path, tmp_env):
is undesirable.
"""

with env_vars({"CONDA_PKGS_DIRS": tmp_path}), tmp_env() as prefix:
with env_vars({"CONDA_PKGS_DIRS": path_factory()}), make_temp_env() as prefix:
assert (Path(prefix) / "conda-meta" / "history").exists()

# Setup conda configuration
Expand Down Expand Up @@ -266,23 +268,38 @@ def test_conda_build_with_aliased_channels(tmp_path):
condarc.unlink()


def test_http_server_auth_none(http_server_auth_none): # noqa: F811
create_with_channel(http_server_auth_none)
def test_http_server_auth_none(
http_server_auth_none, # noqa: F811
path_factory: PathFactoryFixture,
) -> None:
create_with_channel(http_server_auth_none, path_factory)


def test_http_server_auth_basic(http_server_auth_basic): # noqa: F811
create_with_channel(http_server_auth_basic)
def test_http_server_auth_basic(
http_server_auth_basic, # noqa: F811
path_factory: PathFactoryFixture,
) -> None:
create_with_channel(http_server_auth_basic, path_factory)


def test_http_server_auth_basic_email(http_server_auth_basic_email): # noqa: F811
create_with_channel(http_server_auth_basic_email)
def test_http_server_auth_basic_email(
http_server_auth_basic_email, # noqa: F811
path_factory: PathFactoryFixture,
) -> None:
create_with_channel(http_server_auth_basic_email, path_factory)


def test_http_server_auth_token(http_server_auth_token): # noqa: F811
create_with_channel(http_server_auth_token)
def test_http_server_auth_token(
http_server_auth_token, # noqa: F811
path_factory: PathFactoryFixture,
) -> None:
create_with_channel(http_server_auth_token, path_factory)


def test_http_server_auth_token_in_defaults(http_server_auth_token): # noqa: F811
def test_http_server_auth_token_in_defaults(
http_server_auth_token, # noqa: F811
path_factory: PathFactoryFixture,
) -> None:
condarc = Path.home() / ".condarc"
condarc_contents = condarc.read_text() if condarc.is_file() else None
try:
Expand All @@ -297,7 +314,7 @@ def test_http_server_auth_token_in_defaults(http_server_auth_token): # noqa: F8
conda_subprocess(
"create",
"-p",
_get_temp_prefix(use_restricted_unicode=on_win),
path_factory(),
"--solver=libmamba",
"test-package",
)
Expand All @@ -308,14 +325,14 @@ def test_http_server_auth_token_in_defaults(http_server_auth_token): # noqa: F8
condarc.unlink()


def test_local_spec():
"https://github.com/conda/conda-libmamba-solver/issues/398"
def test_local_spec(path_factory: PathFactoryFixture) -> None:
# https://github.com/conda/conda-libmamba-solver/issues/398
env = os.environ.copy()
env["CONDA_BLD_PATH"] = str(DATA / "mamba_repo")
process = conda_subprocess(
"create",
"-p",
_get_temp_prefix(use_restricted_unicode=on_win),
path_factory(),
"--dry-run",
"--solver=libmamba",
"--channel=local",
Expand All @@ -327,7 +344,7 @@ def test_local_spec():
process = conda_subprocess(
"create",
"-p",
_get_temp_prefix(use_restricted_unicode=on_win),
path_factory(),
"--dry-run",
"--solver=libmamba",
"local::test-package",
Expand Down
40 changes: 1 addition & 39 deletions tests/test_experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
import pytest
from conda.base.context import context, fresh_context
from conda.exceptions import CondaEnvironmentError
from conda.testing.integration import Commands, _get_temp_prefix, run_command


def _get_temp_prefix_safe():
return _get_temp_prefix(use_restricted_unicode=True).replace(" ", "")
from conda.testing.integration import Commands, run_command


def print_and_check_output(*args, **kwargs):
Expand Down Expand Up @@ -70,37 +66,3 @@ def test_cli_flag_in_help():
for command in commands_without_flag:
process = print_and_check_output([sys.executable, "-m", "conda"] + command + ["--help"])
assert "--solver" not in process.stdout


def cli_flag_and_env_var_settings():
env_no_var = os.environ.copy()
env_no_var.pop("CONDA_SOLVER", None)
env_libmamba = env_no_var.copy()
env_classic = env_no_var.copy()
env_libmamba["CONDA_SOLVER"] = "libmamba"
env_classic["CONDA_SOLVER"] = "classic"
command = [
sys.executable,
"-m",
"conda",
"create",
"-y",
"-p",
_get_temp_prefix_safe(),
"--dry-run",
"xz",
]
cli_libmamba = ["--solver=libmamba"]
cli_classic = ["--solver=classic"]
tests = [
["no flag, no env", command, env_no_var, "classic"],
["no flag, env classic", command, env_classic, "classic"],
["no flag, env libmamba", command, env_libmamba, "libmamba"],
["flag libmamba, no env", command + cli_libmamba, env_no_var, "libmamba"],
["flag libmamba, env libmamba", command + cli_libmamba, env_libmamba, "libmamba"],
["flag libmamba, env classic", command + cli_libmamba, env_classic, "libmamba"],
["flag classic, no env", command + cli_classic, env_no_var, "classic"],
["flag classic, env libmamba", command + cli_classic, env_libmamba, "classic"],
["flag classic, env classic", command + cli_classic, env_classic, "classic"],
]
return tests
33 changes: 19 additions & 14 deletions tests/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
Measure the speed and memory usage of the different backend solvers
"""

from __future__ import annotations

import os
import shutil
from typing import TYPE_CHECKING

import pytest
from conda.base.context import context
from conda.common.io import env_var
from conda.exceptions import DryRunExit
from conda.testing.integration import (
Commands,
_get_temp_prefix,
make_temp_env,
run_command,
)
from conda.testing.integration import Commands, make_temp_env, run_command

if TYPE_CHECKING:
from collections.abc import Iterable
from pathlib import Path

from conda.testing.fixtures import PathFactoryFixture
from pytest import FixtureRequest

platform = context.subdir

Expand All @@ -41,21 +46,19 @@ def _channels_as_args(channels):
return tuple(args)


def _tmp_prefix_safe():
return _get_temp_prefix(use_restricted_unicode=True).replace(" ", "")


@pytest.fixture(
scope="module",
params=[f for f in os.listdir(TEST_DATA_DIR) if f.endswith(".lock")],
)
def prefix_and_channels(request):
def prefix_and_channels(
request: FixtureRequest, session_path_factory: PathFactoryFixture
) -> Iterable[tuple[Path, list[str]]]:
lockfile = os.path.join(TEST_DATA_DIR, request.param)
lock_platform = lockfile.split(".")[-2]
if lock_platform != platform:
pytest.skip(f"Running platform {platform} does not match file platform {lock_platform}")
with env_var("CONDA_TEST_SAVE_TEMPS", "1"):
prefix = _tmp_prefix_safe()
prefix = session_path_factory()
with make_temp_env("--file", lockfile, prefix=prefix) as prefix:
channels = _get_channels_from_lockfile(lockfile)
yield prefix, channels
Expand Down Expand Up @@ -132,11 +135,13 @@ def test_update_all(prefix_and_channels, solver_args):


@pytest.mark.slow
def test_install_vaex_from_conda_forge_and_defaults(solver_args):
def test_install_vaex_from_conda_forge_and_defaults(
solver_args: tuple[str, ...], path_factory: PathFactoryFixture
) -> None:
try:
run_command(
Commands.CREATE,
_tmp_prefix_safe(),
str(path_factory()),
*solver_args,
"--override-channels",
"-c",
Expand Down
Loading