Skip to content

Commit

Permalink
Add PackageInfo rvalue string ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Sep 22, 2022
1 parent 25b77d4 commit f8b5923
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions libmamba/include/mamba/core/package_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace mamba
PackageInfo(Solvable* s);
PackageInfo(nlohmann::json&& j);
PackageInfo(const std::string& name);
PackageInfo(std::string&& name);
PackageInfo(const std::string& name,
const std::string& version,
const std::string build_string,
Expand Down
5 changes: 5 additions & 0 deletions libmamba/src/core/package_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ namespace mamba
{
}

PackageInfo::PackageInfo(std::string&& n)
: name(std::move(n))
{
}

PackageInfo::PackageInfo(const std::string& n,
const std::string& v,
const std::string b,
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/core/subdirdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ namespace mamba
m_valid_cache_path = "";
m_expired_cache_path = "";
m_loaded = false;
m_mod_etag = nlohmann::json();
m_mod_etag = nlohmann::json::object();

LOG_INFO << "Searching index cache file for repo '" << m_repodata_url << "'";

Expand Down
1 change: 1 addition & 0 deletions libmamba/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(TEST_SRCS
test_invoke.cpp
test_tasksync.cpp
test_filesystem.cpp
test_satisfiability_error.cpp
)

message(STATUS "Building libmamba C++ tests")
Expand Down
123 changes: 123 additions & 0 deletions libmamba/tests/test_satisfiability_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <array>
#include <vector>
#include <string>
#include <string_view>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <random>

#include <gtest/gtest.h>
#include <nlohmann/json.hpp>
#include "spdlog/spdlog.h"
#ifdef SPDLOG_FMT_EXTERNAL
#include <fmt/color.h>
#else
#include <spdlog/fmt/bundled/color.h>
#endif

#include "mamba/core/channel.hpp"
#include "mamba/core/pool.hpp"
#include "mamba/core/solver.hpp"
#include "mamba/core/subdirdata.hpp"
#include "mamba/core/package_cache.hpp"
#include "mamba/core/mamba_fs.hpp"
#include "mamba/core/satisfiability_error.hpp"
#include "mamba/core/package_info.hpp"
#include "mamba/core/fsutil.hpp"

namespace mamba
{

/**
* Simple factory for building a PackageInfo.
*/
auto mkpkg(std::string name, std::string version, std::vector<std::string> dependencies)
-> PackageInfo
{
auto pkg = PackageInfo(std::move(name));
pkg.version = std::move(version);
pkg.depends = std::move(dependencies);
return pkg;
}

template <typename PkgRange>
auto create_repodata(fs::u8path dir, PkgRange const& packages) -> void
{
namespace nl = nlohmann;

// Entry exepected for a PacakgeInf in repodata.json.
auto make_entry = [](PackageInfo const& pkg)
{
auto j = nl::json::object();
auto filename
= fmt::format("{}-{}-{}.tar.bz2", pkg.name, pkg.version, pkg.build_string);
j[std::move(filename)] = pkg.json_record();
return j;
};
auto repodata_j = nl::json::object();
repodata_j["packages"] = nl::json::array();
std::transform(packages.begin(),
packages.end(),
std::back_inserter(repodata_j["packages"]),
make_entry);

fs::create_directories(dir / "noarch");
std::ofstream(dir / "noarch/repodata.json", std::ofstream::app) << repodata_j;
}

/**
* Generate a string of random characters.
*/
auto random_str(std::size_t n) -> std::string
{
static auto constexpr chars = std::string_view{ "abcdefghijklmnopqrstuvwxyz1234567890" };
auto result = std::string(n, 'a');
auto gen = std::default_random_engine(std::random_device()());
auto choice = std::uniform_int_distribution<std::size_t>(0, chars.size() - 1);
std::generate(result.begin(), result.end(), [&]() { return chars[choice(gen)]; });
return result;
}


template <typename PkgRange>
auto create_problem(PkgRange const& packages, std::vector<std::string> const& specs)
// -> std::pair<MSolver, MPool>
{
auto const tmp_dir = fs::temp_directory_path() / "mamba/tests" / random_str(20);
LOG_ERROR << tmp_dir;
auto const tmp_cache = tmp_dir / "cache";
// fs::create_directories(tmp_cache);
auto cache = MultiPackageCache({ tmp_cache });
auto const tmp_prefix = tmp_dir / "prefix";
create_repodata(tmp_prefix, packages);
// TODO
// auto const& channel = make_channel("file:///")
auto const& channel = make_channel("file://" + tmp_prefix.string());
// TODO move semantics
// auto pool = MPool();
auto pool = std::make_unique<MPool>();
auto expected_sudir = MSubdirData::create(channel, "some-platform", "some-url", cache);
if (!expected_sudir)
{
throw expected_sudir.error();
}
expected_sudir->create_repo(*pool);
// TODO move_semantics
// auto solver = MSolver(pool, { { SOLVER_FLAG_ALLOW_DOWNGRADE, 1 } });
auto solver = std::make_unique<MSolver>(
*pool, std::vector{ std::pair{ SOLVER_FLAG_ALLOW_DOWNGRADE, 1 } });
solver->add_jobs(specs, SOLVER_INSTALL);
// TODO missing move ctor
// return { std::move(solver), std::move(pool) };
return std::pair{ std::move(solver), std::move(pool) };
}

TEST(test_foo, test_foo)
{
auto [solver, pool] = create_problem(std::array{ mkpkg("foo", "0.1.0", {}) }, { "foo" });
auto const solved = solver->solve();
LOG_ERROR << solver->all_problems_to_str();
EXPECT_TRUE(solved);
}
}

0 comments on commit f8b5923

Please sign in to comment.