forked from mamba-org/mamba
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25b77d4
commit f8b5923
Showing
5 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |