289 lines
15 KiB
C++
289 lines
15 KiB
C++
#include <Void/Data/StrictJson.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
#include <vbs/build.hpp>
|
|
|
|
#include <limits>
|
|
#include <optional>
|
|
#include <type_traits>
|
|
|
|
namespace {
|
|
vbs::ProfileContext context() {
|
|
return vbs::default_profiles().resolve("debug", vbs::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 = vbs::default_profiles();
|
|
const auto debug = profiles.resolve("debug", vbs::CompilerFamily::clang, "linux", "aarch64");
|
|
const auto release = profiles.resolve("release", vbs::CompilerFamily::gcc, "linux", "x86_64");
|
|
Void::Testing::expect_eq(profiles.records().size(), std::size_t{2});
|
|
Void::Testing::expect(debug.compiler == vbs::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 = vbs::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", vbs::CompilerFamily::gcc, "linux", "x86_64"); });
|
|
Void::Testing::expect_throw<std::invalid_argument>([] { (void)vbs::parse_compiler("msvc"); });
|
|
Void::Testing::expect_throw<std::invalid_argument>(
|
|
[&] { (void)profiles.resolve("debug", vbs::CompilerFamily::gcc, "", "x86_64"); });
|
|
}
|
|
|
|
Void::Testing::Test declaration_records_and_visibility() {
|
|
vbs::Project project{context()};
|
|
auto library = project.static_library("library");
|
|
library.sources({"one.cpp", "two.cpp"})
|
|
.include_directory("include", vbs::Visibility::public_)
|
|
.compile_option("-Wall");
|
|
auto executable = project.executable("app");
|
|
executable.link(library).link(library, vbs::Visibility::interface_);
|
|
Void::Testing::expect(library.record().kind == vbs::TargetKind::static_library);
|
|
Void::Testing::expect_eq(library.record().sources.size(), std::size_t{2});
|
|
Void::Testing::expect(library.record().include_directories.at(0).visibility ==
|
|
vbs::Visibility::public_);
|
|
Void::Testing::expect(executable.record().links.at(0).visibility == vbs::Visibility::private_);
|
|
Void::Testing::expect(executable.record().links.at(1).visibility ==
|
|
vbs::Visibility::interface_);
|
|
}
|
|
|
|
Void::Testing::Test sources_are_symbolic() {
|
|
vbs::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 == vbs::SourceKind::file);
|
|
Void::Testing::expect(target.record().sources.at(1).kind == vbs::SourceKind::glob);
|
|
Void::Testing::expect_eq(target.record().sources.at(1).path,
|
|
std::string{"nonexistent/**/*.cpp"});
|
|
}
|
|
|
|
Void::Testing::Test handles_survive_record_growth() {
|
|
vbs::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", vbs::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() {
|
|
vbs::Project first{context()};
|
|
vbs::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<vbs::Project>);
|
|
static_assert(!std::is_copy_constructible_v<vbs::Project>);
|
|
std::optional<vbs::Target> target;
|
|
std::optional<vbs::Dependency> dependency;
|
|
std::optional<vbs::ImportedTarget> imported;
|
|
{
|
|
vbs::Project project{context()};
|
|
target = project.executable("expired");
|
|
dependency = project.dependency("expired");
|
|
imported = dependency->target("Expired::Target");
|
|
}
|
|
vbs::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() {
|
|
vbs::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)vbs::SourceSet::file(""); });
|
|
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)project.glob(""); });
|
|
}
|
|
|
|
Void::Testing::Test rejects_contradictory_providers() {
|
|
vbs::Project project{context()};
|
|
auto git = project.dependency("git");
|
|
git.git("https://example.invalid/repo", vbs::Commit{revision}).cmake();
|
|
Void::Testing::expect_throw<std::invalid_argument>([&] { git.package("pkg", "^1"); });
|
|
Void::Testing::expect_throw<std::invalid_argument>(
|
|
[&] { git.git("url", vbs::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", vbs::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", vbs::Commit{"main"}); });
|
|
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)project.to_json(); });
|
|
}
|
|
|
|
Void::Testing::Test json_schema_types_and_escaping() {
|
|
vbs::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", vbs::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(vbs::SourceSet::file(escaped))
|
|
.sources(project.glob("src/**/*.cpp"))
|
|
.include_directory("include", vbs::Visibility::public_)
|
|
.link(dep.target("Remote::Library"));
|
|
auto app = project.executable("app");
|
|
app.link(library, vbs::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";
|
|
vbs::Project project{context()};
|
|
project.executable(unicode).sources(vbs::SourceSet::file(unicode));
|
|
project.dependency("unicode").git("url", vbs::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";
|
|
vbs::Project named{context()};
|
|
(void)named.executable(bytes);
|
|
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)named.to_json(); });
|
|
|
|
vbs::Project sourced{context()};
|
|
sourced.executable("source").sources(vbs::SourceSet::file(malformed));
|
|
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)sourced.to_json(); });
|
|
|
|
vbs::Project keyed{context()};
|
|
keyed.dependency("key").git("url", vbs::Commit{revision}).cmake({{malformed, true}});
|
|
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)keyed.to_json(); });
|
|
|
|
vbs::Project valued{context()};
|
|
valued.dependency("value").git("url", vbs::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);
|
|
vbs::Project configured{profile};
|
|
Void::Testing::expect_throw<std::invalid_argument>([&] { (void)configured.to_json(); });
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test independent_projects_and_contexts() {
|
|
auto initial = context();
|
|
vbs::Project first{initial};
|
|
initial.name = "mutated";
|
|
vbs::Project second{
|
|
vbs::default_profiles().resolve("release", vbs::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() {
|
|
vbs::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)vbs::OptionValue{std::numeric_limits<std::uint64_t>::max()}; });
|
|
dep.cmake({{"enabled", true}}).git("url", vbs::Commit{revision});
|
|
Void::Testing::expect_eq(dep.record().options.size(), std::size_t{1});
|
|
}
|