From c51ce62af7f378c69b9be8432070e667fdb57427 Mon Sep 17 00:00:00 2001 From: Jonas Haag Date: Thu, 31 Aug 2023 22:52:58 +0200 Subject: [PATCH] Parse subdirs in CLI match specs (#2799) Fixes #2792 For example, the channel in `install conda-forge/noarch::tzdata` would previously be parsed as `conda-forge/noarch` while it should be parsed as `conda-forge` with `noarch` subdir. --- libmamba/include/mamba/core/channel.hpp | 1 + libmamba/src/core/channel.cpp | 12 +++++++---- libmamba/src/core/match_spec.cpp | 25 +++++++++++----------- libmamba/tests/src/core/test_cpp.cpp | 25 ++++++++++++++++++++++ libmamba/tests/src/util/test_url_manip.cpp | 20 +++++++++++++++++ 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/libmamba/include/mamba/core/channel.hpp b/libmamba/include/mamba/core/channel.hpp index 5ef58164d4..3bff04e32d 100644 --- a/libmamba/include/mamba/core/channel.hpp +++ b/libmamba/include/mamba/core/channel.hpp @@ -18,6 +18,7 @@ namespace mamba { + std::vector get_known_platforms(); // Note: Channels can only be created using ChannelContext. class Channel { diff --git a/libmamba/src/core/channel.cpp b/libmamba/src/core/channel.cpp index 10961fd485..66cef72e3e 100644 --- a/libmamba/src/core/channel.cpp +++ b/libmamba/src/core/channel.cpp @@ -232,18 +232,17 @@ namespace mamba // that already contains the platform else { - std::string cleaned_url = "", platform = ""; + std::string platform = ""; util::split_platform( - KNOWN_PLATFORMS, + get_known_platforms(), value, Context::instance().platform, - cleaned_url, + value, platform ); if (!platform.empty()) { platforms.push_back(std::move(platform)); - value = cleaned_url; } } } @@ -256,6 +255,11 @@ namespace mamba } } // namespace + std::vector get_known_platforms() + { + return KNOWN_PLATFORMS; + } + /************************** * Channel implementation * **************************/ diff --git a/libmamba/src/core/match_spec.cpp b/libmamba/src/core/match_spec.cpp index f979cdbaf3..9dadca0a81 100644 --- a/libmamba/src/core/match_spec.cpp +++ b/libmamba/src/core/match_spec.cpp @@ -7,6 +7,7 @@ #include #include "mamba/core/channel.hpp" +#include "mamba/core/context.hpp" #include "mamba/core/environment.hpp" #include "mamba/core/match_spec.hpp" #include "mamba/core/output.hpp" @@ -177,17 +178,14 @@ namespace mamba { throw std::runtime_error("Parsing of channel / namespace / subdir failed."); } - // TODO implement Channel, and parsing of the channel here! - // channel = subdir = channel_str; - // channel, subdir = _parse_channel(channel_str) - // if 'channel' in brackets: - // b_channel, b_subdir = _parse_channel(brackets.pop('channel')) - // if b_channel: - // channel = b_channel - // if b_subdir: - // subdir = b_subdir - // if 'subdir' in brackets: - // subdir = brackets.pop('subdir') + + std::string cleaned_url; + std::string platform; + util::split_platform(get_known_platforms(), channel, Context::instance().platform, channel, platform); + if (!platform.empty()) + { + subdir = platform; + } // support faulty conda matchspecs such as `libblas=[build=*mkl]`, which is // the repr of `libblas=*=*mkl` @@ -279,7 +277,10 @@ namespace mamba } else if (k == "subdir") { - subdir = v; + if (platform.empty()) + { + subdir = v; + } } else if (k == "url") { diff --git a/libmamba/tests/src/core/test_cpp.cpp b/libmamba/tests/src/core/test_cpp.cpp index 29bd399a17..f41ef95e06 100644 --- a/libmamba/tests/src/core/test_cpp.cpp +++ b/libmamba/tests/src/core/test_cpp.cpp @@ -235,6 +235,31 @@ namespace mamba MatchSpec ms("numpy=1.20", channel_context); CHECK_EQ(ms.str(), "numpy=1.20"); } + + { + MatchSpec ms("conda-forge::tzdata", channel_context); + CHECK_EQ(ms.str(), "conda-forge::tzdata"); + } + { + MatchSpec ms("conda-forge::noarch/tzdata", channel_context); + CHECK_EQ(ms.str(), "conda-forge::noarch/tzdata"); + } + { + MatchSpec ms("pkgs/main::tzdata", channel_context); + CHECK_EQ(ms.str(), "pkgs/main::tzdata"); + } + { + MatchSpec ms("pkgs/main/noarch::tzdata", channel_context); + CHECK_EQ(ms.str(), "pkgs/main/noarch::tzdata"); + } + { + MatchSpec ms("conda-forge/noarch::tzdata[subdir=linux64]", channel_context); + CHECK_EQ(ms.str(), "conda-forge/noarch::tzdata"); + } + { + MatchSpec ms("conda-forge::tzdata[subdir=linux64]", channel_context); + CHECK_EQ(ms.str(), "conda-forge/linux64::tzdata"); + } } TEST_CASE("is_simple") diff --git a/libmamba/tests/src/util/test_url_manip.cpp b/libmamba/tests/src/util/test_url_manip.cpp index e96521c8bd..db981cd0cc 100644 --- a/libmamba/tests/src/util/test_url_manip.cpp +++ b/libmamba/tests/src/util/test_url_manip.cpp @@ -127,6 +127,26 @@ TEST_SUITE("util::url_manip") CHECK_EQ(platform_found, "noarch"); CHECK_EQ(cleaned_url, "https://mamba.com"); + + split_platform( + { "noarch", "linux-64" }, + "https://conda.anaconda.org/conda-forge/noarch", + std::string(mamba::specs::build_platform_name()), + cleaned_url, + platform_found + ); + CHECK_EQ(platform_found, "noarch"); + CHECK_EQ(cleaned_url, "https://conda.anaconda.org/conda-forge"); + + split_platform( + { "noarch", "linux-64" }, + "https://conda.anaconda.org/pkgs/main/noarch", + std::string(mamba::specs::build_platform_name()), + cleaned_url, + platform_found + ); + CHECK_EQ(platform_found, "noarch"); + CHECK_EQ(cleaned_url, "https://conda.anaconda.org/pkgs/main"); } TEST_CASE("path_to_url")