Skip to content

Commit

Permalink
Add VersionSpec::is_explicitly_free
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Dec 28, 2023
1 parent 7e503bc commit 73eb02e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions libmamba/include/mamba/specs/version_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ namespace mamba::specs
VersionSpec() = default;
explicit VersionSpec(tree_type&& tree) noexcept;

/**
* Returns wether the VersionSpec is unconstrained.
*
* Due to the complex nature of VersionSpec expressions, it is not always easy to know
* whether a complex expression can be simpified to the unconstrained one.
* This functions only handles the trivial cases.
*/
[[nodiscard]] auto is_explicitly_free() const -> bool;

/**
* A string representation of the version spec.
*
Expand Down
7 changes: 7 additions & 0 deletions libmamba/src/specs/version_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ namespace mamba::specs
return m_tree.evaluate([&point](const auto& node) { return node.contains(point); });
}

auto VersionSpec::is_explicitly_free() const -> bool
{
const auto free_pred = VersionPredicate::make_free();
const auto is_free_pred = [&free_pred](const auto& node) { return node == free_pred; };
return m_tree.empty() || ((m_tree.size() == 1) && m_tree.evaluate(is_free_pred));
}

auto VersionSpec::str() const -> std::string
{
return fmt::format("{}", *this);
Expand Down
21 changes: 21 additions & 0 deletions libmamba/tests/src/specs/test_version_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,25 @@ TEST_SUITE("specs::version_spec")
CHECK_EQ(vs.str_conda_build(), "2.3.*,<3.0");
}
}

TEST_CASE("VersionSpec::is_free")
{
{
using namespace mamba::util;

auto parser = InfixParser<VersionPredicate, BoolOperator>{};
parser.push_variable(VersionPredicate::make_free());
parser.finalize();
auto spec = VersionSpec(std::move(parser).tree());

CHECK(spec.is_explicitly_free());
}

CHECK(VersionSpec().is_explicitly_free());
CHECK(VersionSpec::parse("*").is_explicitly_free());
CHECK(VersionSpec::parse("").is_explicitly_free());

CHECK_FALSE(VersionSpec::parse("==2.3|!=2.3").is_explicitly_free());
CHECK_FALSE(VersionSpec::parse("=2.3,<3.0").is_explicitly_free());
}
}

0 comments on commit 73eb02e

Please sign in to comment.