From ad0272004e5f2d690dffac6b1c5d4d2aa3902a43 Mon Sep 17 00:00:00 2001 From: Ken Odegard Date: Mon, 17 Jul 2023 11:31:00 -0500 Subject: [PATCH 1/4] Parametrize test_priority --- tests/test_priority.py | 150 ++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 100 deletions(-) diff --git a/tests/test_priority.py b/tests/test_priority.py index 64b21f3bd8b..68bdb805e92 100644 --- a/tests/test_priority.py +++ b/tests/test_priority.py @@ -1,110 +1,60 @@ # Copyright (C) 2012 Anaconda, Inc # SPDX-License-Identifier: BSD-3-Clause -import json -import re - import pytest +from pytest import MonkeyPatch +from pytest_mock import MockerFixture -from conda.base.context import conda_tests_ctxt_mgmt_def_pol, context -from conda.common.io import env_var +from conda.base.context import context, reset_context from conda.core.prefix_data import PrefixData -from conda.testing.integration import ( - Commands, - make_temp_env, - package_is_installed, - run_command, -) - - -@pytest.mark.integration -def test_channel_order_channel_priority_true(): - # This is broken, make_temp_env will reset the context. We get away with it, but really - # we need a function that does both these at the same time. - with env_var( - "CONDA_PINNED_PACKAGES", - "python=3.8", - stack_callback=conda_tests_ctxt_mgmt_def_pol, - ): - with make_temp_env("pycosat=0.6.3") as prefix: - assert package_is_installed(prefix, "python=3.8") - assert package_is_installed(prefix, "pycosat") - - payload, _, _ = run_command( - Commands.CONFIG, prefix, "--get", "channels", "--json" - ) - default_channels = json.loads(payload)["get"].get("channels") - if default_channels: - run_command(Commands.CONFIG, prefix, "--remove-key", "channels") - - # add conda-forge channel - o, e, _ = run_command( - Commands.CONFIG, - prefix, - "--prepend", - "channels", - "conda-forge", - "--json", - ) - assert context.channels == ("conda-forge", "defaults"), o + e - # update --all - update_stdout, _, _ = run_command(Commands.UPDATE, prefix, "--all") - - # this assertion works with the pinned_packages config to make sure - # conda update --all still respects the pinned python version - assert package_is_installed(prefix, "python=3.8") - - # pycosat should be in the SUPERSEDED list - # after the 4.4 solver work, looks like it's in the DOWNGRADED list - # This language needs changed anyway here. - # For packages that CHANGE because they're being moved to a higher-priority channel - # the message should be - # - # The following packages will be UPDATED to a higher-priority channel: - # - installed_str, x = update_stdout.split("UPDATED") - assert re.search( - r"pkgs/main::pycosat-0.6.3-py38h[^\s]+ --> conda-forge::pycosat", x - ) - - # python sys.version should show conda-forge python - assert PrefixData(prefix).get("python").channel.name == "conda-forge" - - # conda list should show pycosat coming from conda-forge - assert PrefixData(prefix).get("pycosat").channel.name == "conda-forge" +from conda.testing import CondaCLIFixture, TmpEnvFixture +from conda.testing.helpers import set_active_prefix @pytest.mark.integration -def test_channel_priority_update(): - """This case will fail now.""" - with make_temp_env("python=3.8", "pycosat") as prefix: - assert package_is_installed(prefix, "python") - - # clear channels config first to not assume default is defaults - payload, _, _ = run_command( - Commands.CONFIG, prefix, "--get", "channels", "--json" - ) - default_channels = json.loads(payload)["get"].get("channels") - if default_channels: - run_command(Commands.CONFIG, prefix, "--remove-key", "channels") +@pytest.mark.parametrize( + "pinned_package", + [ + pytest.param(True, id="with pinned_package"), + pytest.param(False, id="without pinned_package"), + ], +) +def test_reorder_channel_priority( + tmp_env: TmpEnvFixture, + monkeypatch: MonkeyPatch, + mocker: MockerFixture, + conda_cli: CondaCLIFixture, + pinned_package: bool, +): + # use "cheap" packages with no dependencies + package1 = "zlib" + package2 = "ca-certificates" + + # set pinned package + if pinned_package: + monkeypatch.setenv("CONDA_PINNED_PACKAGES", package1) + + # add defaults channel + monkeypatch.setenv("CONDA_CHANNELS", "defaults") + reset_context() + assert context.channels == ("defaults",) + + # create environment with package1 and package2 + with tmp_env(package1, package2) as prefix, set_active_prefix(prefix): + # check both packages are installed from defaults + PrefixData._cache_.clear() + assert PrefixData(prefix).get(package1).channel.name == "pkgs/main" + assert PrefixData(prefix).get(package2).channel.name == "pkgs/main" # add conda-forge channel - o, e, _ = run_command( - Commands.CONFIG, - prefix, - "--prepend", - "channels", - "conda-forge", - "--json", - ) - assert context.channels == ("conda-forge", "defaults"), o + e - - # update python - update_stdout, _, _ = run_command(Commands.UPDATE, prefix, "python") - - # pycosat should be in the SUPERSEDED list - superceded_split = update_stdout.split("UPDATED") - assert len(superceded_split) == 2 - assert "conda-forge" in superceded_split[1] - - # python sys.version should show conda-forge python - assert PrefixData(prefix).get("python").channel.name == "conda-forge" + monkeypatch.setenv("CONDA_CHANNELS", "conda-forge,defaults") + reset_context() + assert context.channels == ("conda-forge", "defaults") + + # update --all + conda_cli("update", "--prefix", prefix, "--all", "--yes") + + # check pinned package is unchanged but unpinned packages are updated from conda-forge + PrefixData._cache_.clear() + expected_channel = "pkgs/main" if pinned_package else "conda-forge" + assert PrefixData(prefix).get(package1).channel.name == expected_channel + assert PrefixData(prefix).get(package2).channel.name == "conda-forge" From 4b72f3503bf7be54d84ef9a30cd776176a12202e Mon Sep 17 00:00:00 2001 From: Ken Odegard Date: Mon, 17 Jul 2023 11:34:56 -0500 Subject: [PATCH 2/4] Update channel preference --- tests/test_priority.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_priority.py b/tests/test_priority.py index 68bdb805e92..98490a80ee6 100644 --- a/tests/test_priority.py +++ b/tests/test_priority.py @@ -33,10 +33,10 @@ def test_reorder_channel_priority( if pinned_package: monkeypatch.setenv("CONDA_PINNED_PACKAGES", package1) - # add defaults channel - monkeypatch.setenv("CONDA_CHANNELS", "defaults") + # prefer defaults over conda-forge + monkeypatch.setenv("CONDA_CHANNELS", "defaults,conda-forge") reset_context() - assert context.channels == ("defaults",) + assert context.channels == ("defaults", "conda-forge") # create environment with package1 and package2 with tmp_env(package1, package2) as prefix, set_active_prefix(prefix): @@ -45,7 +45,7 @@ def test_reorder_channel_priority( assert PrefixData(prefix).get(package1).channel.name == "pkgs/main" assert PrefixData(prefix).get(package2).channel.name == "pkgs/main" - # add conda-forge channel + # prefer conda-forge over defaults monkeypatch.setenv("CONDA_CHANNELS", "conda-forge,defaults") reset_context() assert context.channels == ("conda-forge", "defaults") From 8b393fd691d34ac806d2001c75cac02acf9171e1 Mon Sep 17 00:00:00 2001 From: Ken Odegard Date: Mon, 17 Jul 2023 19:42:59 -0500 Subject: [PATCH 3/4] Remove unnecessary set_active_prefix --- tests/test_priority.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_priority.py b/tests/test_priority.py index 98490a80ee6..9cff2f2ed4e 100644 --- a/tests/test_priority.py +++ b/tests/test_priority.py @@ -2,12 +2,10 @@ # SPDX-License-Identifier: BSD-3-Clause import pytest from pytest import MonkeyPatch -from pytest_mock import MockerFixture from conda.base.context import context, reset_context from conda.core.prefix_data import PrefixData from conda.testing import CondaCLIFixture, TmpEnvFixture -from conda.testing.helpers import set_active_prefix @pytest.mark.integration @@ -21,7 +19,6 @@ def test_reorder_channel_priority( tmp_env: TmpEnvFixture, monkeypatch: MonkeyPatch, - mocker: MockerFixture, conda_cli: CondaCLIFixture, pinned_package: bool, ): @@ -39,7 +36,7 @@ def test_reorder_channel_priority( assert context.channels == ("defaults", "conda-forge") # create environment with package1 and package2 - with tmp_env(package1, package2) as prefix, set_active_prefix(prefix): + with tmp_env(package1, package2) as prefix: # check both packages are installed from defaults PrefixData._cache_.clear() assert PrefixData(prefix).get(package1).channel.name == "pkgs/main" From 05e77246c945d21c77ee74dd93806bcd8f9dba2e Mon Sep 17 00:00:00 2001 From: Ken Odegard Date: Mon, 17 Jul 2023 21:36:27 -0500 Subject: [PATCH 4/4] Use CLI args instead of envvars --- tests/test_priority.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tests/test_priority.py b/tests/test_priority.py index 9cff2f2ed4e..70801943f44 100644 --- a/tests/test_priority.py +++ b/tests/test_priority.py @@ -30,25 +30,24 @@ def test_reorder_channel_priority( if pinned_package: monkeypatch.setenv("CONDA_PINNED_PACKAGES", package1) - # prefer defaults over conda-forge - monkeypatch.setenv("CONDA_CHANNELS", "defaults,conda-forge") - reset_context() - assert context.channels == ("defaults", "conda-forge") - # create environment with package1 and package2 - with tmp_env(package1, package2) as prefix: + with tmp_env( + "--override-channels", "--channel=defaults", package1, package2 + ) as prefix: # check both packages are installed from defaults PrefixData._cache_.clear() assert PrefixData(prefix).get(package1).channel.name == "pkgs/main" assert PrefixData(prefix).get(package2).channel.name == "pkgs/main" - # prefer conda-forge over defaults - monkeypatch.setenv("CONDA_CHANNELS", "conda-forge,defaults") - reset_context() - assert context.channels == ("conda-forge", "defaults") - # update --all - conda_cli("update", "--prefix", prefix, "--all", "--yes") + conda_cli( + "update", + f"--prefix={prefix}", + "--override-channels", + "--channel=conda-forge", + "--all", + "--yes", + ) # check pinned package is unchanged but unpinned packages are updated from conda-forge PrefixData._cache_.clear()