The variant has on the left-hand side the topologically sorted vector and the right-hand side is a pair showing the path and its parent that represent a cycle in the graph making the sort impossible. The goal is to implement #551 which needs to throw an error if the topo-sort fails. However, the error-message is supposed to contain a graph of store-paths and the API to generate this is inherently async. Now, catching the exception and re-throwing another one is impossible since `co_await` is forbidden in `catch`-blocks and adding another topoSort variant that allows an async `makeError` also seems odd. Hence, I decided to alter the data-structure in use a bit for this use-case. One out of two uses of the function are affected after all. Change-Id: I70a987f470437df8beb3b1cc203ff88701d0aa1b
46 lines
1.2 KiB
C++
46 lines
1.2 KiB
C++
#include "lix/libutil/topo-sort.hh"
|
|
#include <gtest/gtest.h>
|
|
|
|
namespace nix {
|
|
static auto testToposort(const std::map<std::string, std::set<std::string>> & data)
|
|
{
|
|
std::set<std::string> keys;
|
|
for (auto & [k, _] : data) {
|
|
keys.insert(k);
|
|
}
|
|
|
|
return topoSort(keys, {[&](const std::string & lib) { return data.at(lib); }});
|
|
}
|
|
|
|
TEST(toposort, trivial)
|
|
{
|
|
// The dependencies are incomplete on purpose here, this is just a test-case.
|
|
auto result = testToposort(
|
|
{{"openssh", {"glibc", "zlib", "polkit"}},
|
|
{"zlib", {"glibc"}},
|
|
{"polkit", {"glibc", "pam"}},
|
|
{"pam", {"glibc"}},
|
|
{"glibc", {}}}
|
|
);
|
|
|
|
auto ordered = std::get<std::vector<std::string>>(result);
|
|
ASSERT_EQ(5, ordered.size());
|
|
|
|
ASSERT_EQ("openssh", ordered[0]);
|
|
ASSERT_EQ("zlib", ordered[1]);
|
|
ASSERT_EQ("polkit", ordered[2]);
|
|
ASSERT_EQ("pam", ordered[3]);
|
|
ASSERT_EQ("glibc", ordered[4]);
|
|
}
|
|
|
|
TEST(toposort, cycle)
|
|
{
|
|
auto result = testToposort({{"foo", {"bar"}}, {"bar", {"baz"}}, {"baz", {"foo"}}});
|
|
|
|
auto cycle = std::get<Cycle<std::string>>(result);
|
|
|
|
ASSERT_EQ(cycle.path, "bar");
|
|
ASSERT_EQ(cycle.parent, "foo");
|
|
}
|
|
}
|