Merge pull request 'Deterministic package resolver with source diagnostics (#4)' (#20) from issue/4-package-resolver into main
This commit was merged in pull request #20.
This commit is contained in:
commit
34cfd9cb39
2 files changed
+902
No files matched your search
@@ -0,0 +1,268 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Update/Resolver.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using Void::Update::PackageDependency;
|
||||
using Void::Update::PackageDescription;
|
||||
using Void::Update::Resolution;
|
||||
using Void::Update::ResolutionDiagnostic;
|
||||
using Void::Update::ResolutionRequest;
|
||||
using Void::Semver::Version;
|
||||
|
||||
[[nodiscard]] Version version(std::string_view text)
|
||||
{
|
||||
return *Void::Semver::parse_strict(text);
|
||||
}
|
||||
|
||||
[[nodiscard]] PackageDependency dependency(std::string_view id,
|
||||
std::string_view range)
|
||||
{
|
||||
auto parsed = Void::Update::make_package_dependency(std::string{id}, range);
|
||||
return *parsed;
|
||||
}
|
||||
|
||||
[[nodiscard]] PackageDescription package(
|
||||
std::string id,
|
||||
std::string_view version_text,
|
||||
std::string digest,
|
||||
std::vector<std::string> capabilities = {},
|
||||
std::vector<PackageDependency> dependencies = {})
|
||||
{
|
||||
return PackageDescription{.id = std::move(id),
|
||||
.version = version(version_text),
|
||||
.content_digest = std::move(digest),
|
||||
.capabilities = std::move(capabilities),
|
||||
.dependencies = std::move(dependencies)};
|
||||
}
|
||||
|
||||
using Summary = std::tuple<std::string, std::string, std::string>;
|
||||
|
||||
[[nodiscard]] std::vector<Summary> summarize(const Resolution& resolution)
|
||||
{
|
||||
std::vector<Summary> out;
|
||||
for (const auto& entry : resolution.packages) {
|
||||
out.emplace_back(entry.identity.id,
|
||||
entry.identity.version.to_string(),
|
||||
entry.identity.content_digest);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::vector<std::string> selected(std::initializer_list<const char*> ids)
|
||||
{
|
||||
std::vector<std::string> out;
|
||||
for (const char* id : ids) {
|
||||
out.push_back(id);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test resolve_packages_transitive_closure()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("app", "1.0.0", "d-app", {}, {dependency("lib", "^1.0")}),
|
||||
package("lib", "1.0.0", "d-lib-1"),
|
||||
package("lib", "1.1.0", "d-lib-2"),
|
||||
package("lib", "2.0.0", "d-lib-3"),
|
||||
};
|
||||
auto roots = selected({"app"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(result.has_value());
|
||||
const auto summary = summarize(*result);
|
||||
Void::Testing::require(summary.size() == 2);
|
||||
Void::Testing::expect_eq(std::get<0>(summary[0]), std::string{"app"});
|
||||
Void::Testing::expect_eq(std::get<0>(summary[1]), std::string{"lib"});
|
||||
Void::Testing::expect_eq(std::get<1>(summary[1]), std::string{"1.1.0"});
|
||||
Void::Testing::expect_eq(std::get<2>(summary[1]), std::string{"d-lib-2"});
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_is_order_invariant()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("app", "1.0.0", "d-app", {}, {dependency("lib", "^1.0")}),
|
||||
package("lib", "1.0.0", "d-lib-1"),
|
||||
package("lib", "1.1.0", "d-lib-2"),
|
||||
};
|
||||
auto roots = selected({"app"});
|
||||
const auto first = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
std::reverse(installed.begin(), installed.end());
|
||||
const auto second = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(first.has_value() && second.has_value());
|
||||
Void::Testing::expect(summarize(*first) == summarize(*second));
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_missing_dependency()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("app", "1.0.0", "d-app", {}, {dependency("gone", "^1.0")})};
|
||||
auto roots = selected({"app"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind ==
|
||||
Void::Update::ResolutionErrorKind::missing_dependency);
|
||||
Void::Testing::expect_eq(result.error().package, std::string{"app"});
|
||||
Void::Testing::expect_eq(result.error().field,
|
||||
std::string{"dependencies[0].range"});
|
||||
Void::Testing::expect_eq(result.error().related, std::string{"gone"});
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_incompatible_version()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("app", "1.0.0", "d-app", {}, {dependency("lib", "^2.0")}),
|
||||
package("lib", "1.0.0", "d-lib")};
|
||||
auto roots = selected({"app"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind ==
|
||||
Void::Update::ResolutionErrorKind::incompatible_version);
|
||||
Void::Testing::expect_eq(result.error().related, std::string{"lib"});
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_backtracks_for_constraints()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("app", "1.0.0", "d-app", {}, {dependency("lib", "=1.0.0")}),
|
||||
package("lib", "1.0.0", "d-lib-1"),
|
||||
package("lib", "1.1.0", "d-lib-2")};
|
||||
auto roots = selected({"app", "lib"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(result.has_value());
|
||||
const auto summary = summarize(*result);
|
||||
Void::Testing::require(summary.size() == 2);
|
||||
Void::Testing::expect_eq(std::get<1>(summary[1]), std::string{"1.0.0"});
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_detects_cycle()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("a", "1.0.0", "d-a", {}, {dependency("b", "*")}),
|
||||
package("b", "1.0.0", "d-b", {}, {dependency("a", "*")})};
|
||||
auto roots = selected({"a"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind ==
|
||||
Void::Update::ResolutionErrorKind::cyclic_dependency);
|
||||
Void::Testing::expect_eq(result.error().related, std::string{"a -> b -> a"});
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_rejects_ambiguous_identity()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("lib", "1.0.0+one", "d-one"),
|
||||
package("lib", "1.0.0+two", "d-two")};
|
||||
auto roots = selected({"lib"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind ==
|
||||
Void::Update::ResolutionErrorKind::ambiguous_package);
|
||||
Void::Testing::expect_eq(result.error().package, std::string{"lib"});
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_rejects_competing_exclusive_capabilities()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("gpu-a", "1.0.0", "d-a", {"gpu"}),
|
||||
package("gpu-b", "1.0.0", "d-b", {"gpu"})};
|
||||
auto roots = selected({"gpu-a", "gpu-b"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(
|
||||
result.error().kind ==
|
||||
Void::Update::ResolutionErrorKind::exclusive_capability_conflict);
|
||||
Void::Testing::expect_eq(result.error().package, std::string{"gpu-a"});
|
||||
Void::Testing::expect_eq(result.error().related, std::string{"gpu-b"});
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_frozen_identity_includes_build_and_digest()
|
||||
{
|
||||
const Void::Update::PackageIdentity base{.id = "lib",
|
||||
.version = version("1.0.0+build.1"),
|
||||
.content_digest = "sha256:a"};
|
||||
auto changed_build = base;
|
||||
changed_build.version = version("1.0.0+build.2");
|
||||
auto changed_digest = base;
|
||||
changed_digest.content_digest = "sha256:b";
|
||||
|
||||
Void::Testing::expect(!(base == changed_build));
|
||||
Void::Testing::expect(!(base == changed_digest));
|
||||
Void::Testing::expect(base == base);
|
||||
// SemVer precedence alone treats both as equal; the frozen identity must not.
|
||||
Void::Testing::expect((base.version <=> changed_build.version) == 0);
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_same_precedence_distinct_digest_is_ambiguous()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("lib", "1.0.0+build.1", "sha256:a"),
|
||||
package("lib", "1.0.0+build.1", "sha256:b")};
|
||||
auto roots = selected({"lib"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind ==
|
||||
Void::Update::ResolutionErrorKind::ambiguous_package);
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_rejects_empty_fields()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("lib", "1.0.0", "")};
|
||||
auto roots = selected({"lib"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind ==
|
||||
Void::Update::ResolutionErrorKind::invalid_request);
|
||||
Void::Testing::expect_eq(result.error().field,
|
||||
std::string{"content_digest"});
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_enforces_package_limit()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("app", "1.0.0", "d-app", {}, {dependency("lib", "*")}),
|
||||
package("lib", "1.0.0", "d-lib")};
|
||||
auto roots = selected({"app"});
|
||||
Void::Update::ResolutionLimits limits;
|
||||
limits.max_packages = 1;
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed,
|
||||
.selected = roots,
|
||||
.limits = limits});
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind ==
|
||||
Void::Update::ResolutionErrorKind::limit_exceeded);
|
||||
}
|
||||
|
||||
Void::Testing::Test resolve_packages_deduplicates_identical_descriptions()
|
||||
{
|
||||
std::vector<PackageDescription> installed{
|
||||
package("lib", "1.0.0", "d-lib"),
|
||||
package("lib", "1.0.0", "d-lib")};
|
||||
auto roots = selected({"lib"});
|
||||
auto result = Void::Update::resolve_packages(
|
||||
ResolutionRequest{.installed = installed, .selected = roots});
|
||||
Void::Testing::require(result.has_value());
|
||||
Void::Testing::expect_eq(result->packages.size(), std::size_t{1});
|
||||
}
|
||||
Reference in new issue
Block a user