Files
Build-System/tests/SdkDeclarations.cpp
T
faumaray ab64147f9d
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
Implement VoidBuildSystem v1 (Plans 01-19) with VoidLibrary integration
2026-11-26 20:45:54 +03:00

302 lines
16 KiB
C++

#include <Void/Build.hpp>
#include <Void/Data/StrictJson.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <limits>
#include <optional>
#include <type_traits>
namespace {
Void::Build::ProfileContext context() {
return Void::Build::default_profiles().resolve("debug", Void::Build::CompilerFamily::Gcc,
"linux", "x86_64");
}
constexpr auto revision = "9931c5ffc4844a5e09e1b1db35b3d29c5097c3a5";
using Json = Void::Json::Strict::Value;
const Json &member(const Json &value, const std::string &key) {
return std::get<Json::Object>(value.value).at(key);
}
const Json &element(const Json &value, std::size_t index) {
return std::get<Json::Array>(value.value).at(index);
}
std::string text(const Json &value) { return std::get<std::string>(value.value); }
} // namespace
Void::Testing::Test default_profiles_and_overrides() {
const auto profiles = Void::Build::default_profiles();
const auto debug =
profiles.resolve("debug", Void::Build::CompilerFamily::Clang, "linux", "aarch64");
const auto release =
profiles.resolve("release", Void::Build::CompilerFamily::Gcc, "linux", "x86_64");
Void::Testing::expect_eq(profiles.records().size(), std::size_t{2});
Void::Testing::expect(debug.compiler == Void::Build::CompilerFamily::Clang);
Void::Testing::expect_eq(debug.architecture, std::string{"aarch64"});
Void::Testing::expect_eq(debug.compile_options.at(0), std::string{"-O0"});
Void::Testing::expect_eq(release.compile_options.at(0), std::string{"-O3"});
}
Void::Testing::Test rejects_invalid_profiles() {
auto profiles = Void::Build::default_profiles();
Void::Testing::expect_throw<std::invalid_argument>([&] { profiles.add("debug"); });
Void::Testing::expect_throw<std::invalid_argument>([&] { profiles.add(""); });
Void::Testing::expect_throw<std::invalid_argument>([&] { profiles.add("bad", {""}); });
Void::Testing::expect_throw<std::invalid_argument>([&] {
(void)profiles.resolve("unknown", Void::Build::CompilerFamily::Gcc, "linux", "x86_64");
});
Void::Testing::expect_throw<std::invalid_argument>(
[] { (void)Void::Build::parse_compiler("msvc"); });
Void::Testing::expect_throw<std::invalid_argument>(
[&] { (void)profiles.resolve("debug", Void::Build::CompilerFamily::Gcc, "", "x86_64"); });
}
Void::Testing::Test declaration_records_and_visibility() {
Void::Build::Project project{context()};
auto library = project.static_library("library");
library.sources({"one.cpp", "two.cpp"})
.include_directory("include", Void::Build::Visibility::Public)
.compile_option("-Wall");
auto executable = project.executable("app");
executable.link(library).link(library, Void::Build::Visibility::Interface);
Void::Testing::expect(library.record().kind == Void::Build::TargetKind::StaticLibrary);
Void::Testing::expect_eq(library.record().sources.size(), std::size_t{2});
Void::Testing::expect(library.record().include_directories.at(0).visibility ==
Void::Build::Visibility::Public);
Void::Testing::expect(executable.record().links.at(0).visibility ==
Void::Build::Visibility::Private);
Void::Testing::expect(executable.record().links.at(1).visibility ==
Void::Build::Visibility::Interface);
}
Void::Testing::Test sources_are_symbolic() {
Void::Build::Project project{context()};
auto target = project.executable("symbolic");
target.sources({"missing/../literal.cpp"}).sources(project.glob("nonexistent/**/*.cpp"));
Void::Testing::expect_eq(target.record().sources.at(0).path,
std::string{"missing/../literal.cpp"});
Void::Testing::expect(target.record().sources.at(0).kind == Void::Build::SourceKind::File);
Void::Testing::expect(target.record().sources.at(1).kind == Void::Build::SourceKind::Glob);
Void::Testing::expect_eq(target.record().sources.at(1).path,
std::string{"nonexistent/**/*.cpp"});
}
Void::Testing::Test handles_survive_record_growth() {
Void::Build::Project project{context()};
auto library = project.static_library("first");
auto dependency = project.dependency("original");
const auto imported = dependency.target("Original::Library");
for (int i = 0; i != 256; ++i) {
(void)project.executable("target" + std::to_string(i));
(void)project.dependency("dependency" + std::to_string(i));
}
dependency.git("https://example.invalid/library.git", Void::Build::Commit{revision}).cmake();
library.compile_option("-Wextra");
auto app = project.executable("last");
app.link(library).link(imported);
Void::Testing::expect_eq(project.targets().front().compile_options.at(0),
std::string{"-Wextra"});
Void::Testing::expect_eq(app.record().links.at(0).target, std::string{"first"});
Void::Testing::expect_eq(app.record().links.at(1).dependency, std::string{"original"});
Void::Testing::expect_eq(project.dependencies().front().revision, std::string{revision});
}
Void::Testing::Test rejects_cross_project_links() {
Void::Build::Project first{context()};
Void::Build::Project second{context()};
auto app = first.executable("app");
auto other = second.static_library("other");
auto imported = second.dependency("remote").target("Remote::Library");
Void::Testing::expect_throw<std::invalid_argument>([&] { app.link(other); });
Void::Testing::expect_throw<std::invalid_argument>([&] { app.link(imported); });
Void::Testing::expect(app.record().links.empty());
}
Void::Testing::Test rejects_expired_handles_and_owner_moves() {
static_assert(!std::is_move_constructible_v<Void::Build::Project>);
static_assert(!std::is_copy_constructible_v<Void::Build::Project>);
std::optional<Void::Build::Target> target;
std::optional<Void::Build::Dependency> dependency;
std::optional<Void::Build::ImportedTarget> imported;
{
Void::Build::Project project{context()};
target = project.executable("expired");
dependency = project.dependency("expired");
imported = dependency->target("Expired::Target");
}
Void::Build::Project replacement{context()};
auto app = replacement.executable("new");
Void::Testing::expect_throw<std::logic_error>([&] { target->compile_option("-g"); });
Void::Testing::expect_throw<std::logic_error>([&] { dependency->cmake(); });
Void::Testing::expect_throw<std::logic_error>([&] { (void)target->record(); });
Void::Testing::expect_throw<std::logic_error>([&] { app.link(*target); });
Void::Testing::expect_throw<std::logic_error>([&] { app.link(*imported); });
}
Void::Testing::Test rejects_empty_and_duplicate_identifiers() {
Void::Build::Project project{context()};
auto app = project.executable("app");
auto dep = project.dependency("dep");
Void::Testing::expect_throw<std::invalid_argument>(
[&] { (void)project.static_library("app"); });
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)project.dependency("dep"); });
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)project.executable(""); });
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)project.dependency(""); });
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)dep.target(""); });
Void::Testing::expect_throw<std::invalid_argument>([&] { app.include_directory(""); });
Void::Testing::expect_throw<std::invalid_argument>([&] { app.compile_option(""); });
Void::Testing::expect_throw<std::invalid_argument>(
[] { (void)Void::Build::SourceSet::file(""); });
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)project.glob(""); });
}
Void::Testing::Test rejects_contradictory_providers() {
Void::Build::Project project{context()};
auto git = project.dependency("git");
git.git("https://example.invalid/repo", Void::Build::Commit{revision}).cmake();
Void::Testing::expect_throw<std::invalid_argument>([&] { git.package("pkg", "^1"); });
Void::Testing::expect_throw<std::invalid_argument>(
[&] { git.git("url", Void::Build::Commit{revision}); });
Void::Testing::expect_throw<std::invalid_argument>([&] { git.cmake(); });
auto package = project.dependency("package");
package.package("pkg", "^1");
Void::Testing::expect_throw<std::invalid_argument>(
[&] { package.git("url", Void::Build::Commit{revision}); });
Void::Testing::expect_throw<std::invalid_argument>([&] { package.cmake(); });
auto builder = project.dependency("builder");
builder.cmake();
Void::Testing::expect_throw<std::invalid_argument>([&] { builder.package("pkg", "^1"); });
Void::Testing::expect_throw<std::invalid_argument>(
[&] { builder.git("url", Void::Build::Commit{"main"}); });
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)project.to_json(); });
}
Void::Testing::Test json_schema_types_and_escaping() {
Void::Build::Project project{context()};
std::string escaped = "quote\" slash\\ newline\n\r\t";
for (char c = 0; c < 32; ++c)
escaped += c;
auto dep = project.dependency("dependency");
dep.git("https://example.invalid/\"repo", Void::Build::Commit{revision})
.cmake({{"string", escaped},
{"literal", "text"},
{"bool", false},
{"integer", -42},
{"maximum", std::numeric_limits<std::int64_t>::max()}});
auto library = project.static_library("library");
library.sources(Void::Build::SourceSet::file(escaped))
.sources(project.glob("src/**/*.cpp"))
.include_directory("include", Void::Build::Visibility::Public)
.link(dep.target("Remote::Library"));
auto app = project.executable("app");
app.link(library, Void::Build::Visibility::Public).compile_option(escaped);
project.dependency("package").package("org/pkg", ">=1.2 <2");
const auto json = project.to_json();
Void::Testing::expect_eq(json, project.to_json());
const auto parsed = Void::Json::Strict::parse(json);
Void::Testing::expect(parsed.has_value());
if (!parsed)
return;
Void::Testing::expect_eq(std::get<std::int64_t>(member(*parsed, "schema_version").value),
std::int64_t{1});
const auto &dependency = element(member(*parsed, "dependencies"), 0);
const auto &options = member(dependency, "options");
Void::Testing::expect_eq(text(member(options, "string")), escaped);
Void::Testing::expect_eq(text(member(options, "literal")), std::string{"text"});
Void::Testing::expect(!std::get<bool>(member(options, "bool").value));
Void::Testing::expect_eq(std::get<std::int64_t>(member(options, "integer").value),
std::int64_t{-42});
Void::Testing::expect_eq(std::get<std::int64_t>(member(options, "maximum").value),
std::numeric_limits<std::int64_t>::max());
const auto &target = element(member(*parsed, "targets"), 0);
Void::Testing::expect_eq(text(member(element(member(target, "sources"), 0), "path")), escaped);
Void::Testing::expect_eq(text(member(element(member(target, "sources"), 1), "kind")),
std::string{"glob"});
Void::Testing::expect_eq(text(member(element(member(target, "links"), 0), "dependency")),
std::string{"dependency"});
Void::Testing::expect_eq(
text(member(member(element(member(*parsed, "dependencies"), 1), "source"), "package")),
std::string{"org/pkg"});
}
Void::Testing::Test json_preserves_valid_utf8() {
// Boundary scalar values for every UTF-8 width, including either side of surrogates.
const std::string unicode = "ASCII \xc2\x80\xdf\xbf\xe0\xa0\x80\xed\x9f\xbf"
"\xee\x80\x80\xef\xbf\xbf\xf0\x90\x80\x80\xf4\x8f\xbf\xbf";
Void::Build::Project project{context()};
project.executable(unicode).sources(Void::Build::SourceSet::file(unicode));
project.dependency("unicode")
.git("url", Void::Build::Commit{revision})
.cmake({{unicode, unicode}});
const auto json = project.to_json();
Void::Testing::expect(json.find(unicode) != std::string::npos);
const auto parsed = Void::Json::Strict::parse(json);
Void::Testing::expect(parsed.has_value());
if (!parsed)
return;
const auto &target = element(member(*parsed, "targets"), 0);
Void::Testing::expect_eq(text(member(target, "name")), unicode);
Void::Testing::expect_eq(text(member(element(member(target, "sources"), 0), "path")), unicode);
const auto &options = member(element(member(*parsed, "dependencies"), 0), "options");
Void::Testing::expect_eq(text(member(options, unicode)), unicode);
}
Void::Testing::Test json_rejects_malformed_utf8() {
const std::vector<std::string> invalid{
"\x80", "\xbf", "\xc0\x80", "\xc1\xbf", "\xc2",
"\xc2 ", "\xe0\x80\x80", "\xe0\x9f\xbf", "\xe1\x80", "\xe1 \x80",
"\xe1\x80 ", "\xed\xa0\x80", "\xed\xbf\xbf", "\xf0\x80\x80\x80", "\xf0\x8f\xbf\xbf",
"\xf0\x90\x80", "\xf1\x80\x80 ", "\xf4\x90\x80\x80", "\xf5\x80\x80\x80", "\xfe",
"\xff"};
for (const auto &bytes : invalid) {
const auto malformed = "prefix" + bytes + "suffix";
Void::Build::Project named{context()};
(void)named.executable(bytes);
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)named.to_json(); });
Void::Build::Project sourced{context()};
sourced.executable("source").sources(Void::Build::SourceSet::file(malformed));
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)sourced.to_json(); });
Void::Build::Project keyed{context()};
keyed.dependency("key")
.git("url", Void::Build::Commit{revision})
.cmake({{malformed, true}});
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)keyed.to_json(); });
Void::Build::Project valued{context()};
valued.dependency("value")
.git("url", Void::Build::Commit{revision})
.cmake({{"option", malformed}});
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)valued.to_json(); });
auto profile = context();
profile.compile_options.push_back(malformed);
Void::Build::Project configured{profile};
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)configured.to_json(); });
}
}
Void::Testing::Test independent_projects_and_contexts() {
auto initial = context();
Void::Build::Project first{initial};
initial.name = "mutated";
Void::Build::Project second{Void::Build::default_profiles().resolve(
"release", Void::Build::CompilerFamily::Clang, "linux", "aarch64")};
first.executable("first").compile_option("-g");
Void::Testing::expect_eq(first.context().name, std::string{"debug"});
Void::Testing::expect_eq(second.context().name, std::string{"release"});
Void::Testing::expect(second.targets().empty());
Void::Testing::expect(first.to_json() != second.to_json());
}
Void::Testing::Test rejects_option_errors_without_mutation() {
Void::Build::Project project{context()};
auto dep = project.dependency("library");
Void::Testing::expect_throw<std::invalid_argument>([&] { dep.cmake({{"", true}}); });
Void::Testing::expect(dep.record().builder.empty());
Void::Testing::expect_throw<std::invalid_argument>(
[] { (void)Void::Build::OptionValue{std::numeric_limits<std::uint64_t>::max()}; });
dep.cmake({{"enabled", true}}).git("url", Void::Build::Commit{revision});
Void::Testing::expect_eq(dep.record().options.size(), std::size_t{1});
}