Files
Build-System/tests/DaemonTests.cpp
T
faumaray d6bbd95ced
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 5s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 22s
feat(daemon): support workspace member describe and build
2026-12-03 21:19:15 +03:00

862 lines
35 KiB
C++

#include <Void/Build/Daemon/Daemon.hpp>
#include <Void/Data/StrictJson.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <optional>
#include <string>
#include <system_error>
#include <thread>
#include <utility>
#include <variant>
#include <vector>
namespace {
using JsonValue = Void::Json::Strict::Value;
using JsonObject = JsonValue::Object;
std::optional<JsonValue> parse_json(const std::string &line) {
auto parsed = Void::Json::Strict::parse(line, 16);
if (!parsed) {
return std::nullopt;
}
return std::move(*parsed);
}
const JsonObject *as_object(const JsonValue &value) {
return std::get_if<JsonObject>(&value.value);
}
const JsonValue *field(const JsonObject &object, std::string_view name) {
const auto entry = object.find(name);
return entry == object.end() ? nullptr : &entry->second;
}
bool bool_field(const JsonObject &object, std::string_view name, bool fallback = false) {
const auto *value = field(object, name);
if (value == nullptr) {
return fallback;
}
const auto *flag = std::get_if<bool>(&value->value);
return flag != nullptr ? *flag : fallback;
}
std::int64_t int_field(const JsonObject &object, std::string_view name) {
const auto *value = field(object, name);
if (value == nullptr) {
return -1;
}
const auto *number = std::get_if<std::int64_t>(&value->value);
return number != nullptr ? *number : -1;
}
std::string str_field(const JsonObject &object, std::string_view name) {
const auto *value = field(object, name);
if (value == nullptr) {
return {};
}
const auto *text = std::get_if<std::string>(&value->value);
return text != nullptr ? *text : std::string{};
}
std::optional<JsonObject>
request_object(const std::filesystem::path &socket, std::string_view request_json,
std::chrono::milliseconds timeout = std::chrono::seconds{120}) {
auto response = Void::Build::Daemon::request(socket, request_json, timeout);
if (!response) {
return std::nullopt;
}
auto parsed = parse_json(*response);
if (!parsed) {
return std::nullopt;
}
const auto *object = as_object(*parsed);
if (object == nullptr) {
return std::nullopt;
}
return *object;
}
std::vector<std::string> string_array_field(const JsonObject &object, std::string_view name) {
std::vector<std::string> result;
const auto *value = field(object, name);
if (value == nullptr) {
return result;
}
const auto *array = std::get_if<JsonValue::Array>(&value->value);
if (array == nullptr) {
return result;
}
for (const auto &entry : *array) {
if (const auto *text = std::get_if<std::string>(&entry.value)) {
result.push_back(*text);
}
}
return result;
}
bool wait_for_path(const std::filesystem::path &path, std::chrono::milliseconds timeout) {
const auto deadline = std::chrono::steady_clock::now() + timeout;
while (std::chrono::steady_clock::now() < deadline) {
if (std::filesystem::exists(path)) {
return true;
}
std::this_thread::sleep_for(std::chrono::milliseconds{10});
}
return std::filesystem::exists(path);
}
Void::Build::Daemon::DaemonOptions make_options(const std::filesystem::path &root) {
Void::Build::Daemon::DaemonOptions options;
options.project_root = root / "project";
options.build_root = root / "build";
options.poll_interval = std::chrono::milliseconds{10};
std::error_code ec;
std::filesystem::create_directories(options.project_root, ec);
std::filesystem::create_directories(options.build_root, ec);
return options;
}
} // namespace
VOID_TEST(Daemon, lifecycle_ping_status_gc_shutdown_and_unknown_op) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Testing::expect_eq(socket, options.build_root / ".vbs" / "vbs-daemon.sock");
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto ping = Void::Build::Daemon::request(socket, R"({"op":"ping"})");
Void::Testing::require(ping.has_value());
const auto ping_json = parse_json(*ping);
Void::Testing::require(ping_json.has_value());
const auto *ping_object = as_object(*ping_json);
Void::Testing::require(ping_object != nullptr);
Void::Testing::expect(bool_field(*ping_object, "ok"));
Void::Testing::expect_eq(str_field(*ping_object, "op"), std::string{"ping"});
auto status = Void::Build::Daemon::request(socket, R"({"op":"status"})");
Void::Testing::require(status.has_value());
const auto status_json = parse_json(*status);
Void::Testing::require(status_json.has_value());
const auto *status_object = as_object(*status_json);
Void::Testing::require(status_object != nullptr);
Void::Testing::expect(bool_field(*status_object, "ok"));
Void::Testing::expect(int_field(*status_object, "pid") > 0);
Void::Testing::expect(int_field(*status_object, "watched_directories") >= 1);
Void::Testing::expect(int_field(*status_object, "sequence") >= 0);
auto gc = Void::Build::Daemon::request(socket, R"({"op":"gc","max_bytes":0})");
Void::Testing::require(gc.has_value());
const auto gc_json = parse_json(*gc);
Void::Testing::require(gc_json.has_value());
const auto *gc_object = as_object(*gc_json);
Void::Testing::require(gc_object != nullptr);
Void::Testing::expect(bool_field(*gc_object, "ok"));
Void::Testing::expect(int_field(*gc_object, "removed_blobs") >= 0);
Void::Testing::expect(int_field(*gc_object, "retained_blobs") >= 0);
auto unknown = Void::Build::Daemon::request(socket, R"({"op":"not-an-op"})");
Void::Testing::require(unknown.has_value());
const auto unknown_json = parse_json(*unknown);
Void::Testing::require(unknown_json.has_value());
const auto *unknown_object = as_object(*unknown_json);
Void::Testing::require(unknown_object != nullptr);
Void::Testing::expect(!bool_field(*unknown_object, "ok", true));
Void::Testing::expect(field(*unknown_object, "error") != nullptr);
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::require(shutdown.has_value());
const auto shutdown_json = parse_json(*shutdown);
Void::Testing::require(shutdown_json.has_value());
const auto *shutdown_object = as_object(*shutdown_json);
Void::Testing::require(shutdown_object != nullptr);
Void::Testing::expect(bool_field(*shutdown_object, "ok"));
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::expect(!std::filesystem::exists(socket));
}
VOID_TEST(Daemon, second_create_reports_already_running) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
auto first = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(first.has_value());
const auto socket = first->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = first->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto second = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::expect(!second.has_value());
if (!second) {
Void::Testing::expect(second.error().message().find("already running") !=
std::string::npos);
}
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::expect(!std::filesystem::exists(socket));
}
VOID_TEST(Daemon, describe_evaluates_project_graph) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
{
std::ofstream descriptor{options.project_root / "build.cpp"};
descriptor << R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &project) {
auto app = project.executable("app");
app.sources({"main.cpp"});
}
)cpp";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{options.project_root / "main.cpp"};
source << "int main() { return 0; }\n";
Void::Testing::require(source.good());
}
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto describe =
Void::Build::Daemon::request(socket, R"({"op":"describe"})", std::chrono::seconds{120});
Void::Testing::require(describe.has_value());
const auto describe_json = parse_json(*describe);
Void::Testing::require(describe_json.has_value());
const auto *describe_object = as_object(*describe_json);
Void::Testing::require(describe_object != nullptr);
Void::Testing::expect(bool_field(*describe_object, "ok"));
Void::Testing::expect_eq(str_field(*describe_object, "op"), std::string{"describe"});
Void::Testing::expect_eq(str_field(*describe_object, "profile"), std::string{"debug"});
Void::Testing::expect(int_field(*describe_object, "targets") >= 1);
Void::Testing::expect(int_field(*describe_object, "actions") >= 1);
const auto target_names = string_array_field(*describe_object, "target_names");
Void::Testing::expect(std::ranges::find(target_names, std::string{"app"}) !=
target_names.end());
auto bad_profile = request_object(socket, R"({"op":"describe","profile":"does-not-exist"})");
Void::Testing::require(bad_profile.has_value());
Void::Testing::expect(!bool_field(*bad_profile, "ok", true));
Void::Testing::expect(field(*bad_profile, "error") != nullptr);
auto numeric_profile = request_object(socket, R"({"op":"describe","profile":123})");
Void::Testing::require(numeric_profile.has_value());
Void::Testing::expect(!bool_field(*numeric_profile, "ok", true));
Void::Testing::expect(str_field(*numeric_profile, "error").find("must be a string") !=
std::string::npos);
auto empty_profile = request_object(socket, R"({"op":"describe","profile":""})");
Void::Testing::require(empty_profile.has_value());
Void::Testing::expect(!bool_field(*empty_profile, "ok", true));
Void::Testing::expect(str_field(*empty_profile, "error").find("must not be empty") !=
std::string::npos);
const std::string oversized_profile_request{R"({"op":"describe","profile":")" +
std::string(129, 'p') + "\"}"};
auto oversized_profile = request_object(socket, oversized_profile_request);
Void::Testing::require(oversized_profile.has_value());
Void::Testing::expect(!bool_field(*oversized_profile, "ok", true));
Void::Testing::expect(str_field(*oversized_profile, "error").find("too long") !=
std::string::npos);
auto numeric_compiler = request_object(socket, R"({"op":"describe","compiler":5})");
Void::Testing::require(numeric_compiler.has_value());
Void::Testing::expect(!bool_field(*numeric_compiler, "ok", true));
Void::Testing::expect(str_field(*numeric_compiler, "error").find("must be a string") !=
std::string::npos);
auto invalid_compiler = request_object(socket, R"({"op":"describe","compiler":"msvc"})");
Void::Testing::require(invalid_compiler.has_value());
Void::Testing::expect(!bool_field(*invalid_compiler, "ok", true));
Void::Testing::expect(str_field(*invalid_compiler, "error").find("unknown compiler") !=
std::string::npos);
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::expect(!std::filesystem::exists(socket));
}
VOID_TEST(Daemon, build_executes_project_graph_and_reuses_cache) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
{
std::ofstream descriptor{options.project_root / "build.cpp"};
descriptor << R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &project) {
auto app = project.executable("app");
app.sources({"main.cpp"});
}
)cpp";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{options.project_root / "main.cpp"};
source << "int main() { return 0; }\n";
Void::Testing::require(source.good());
}
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto build = request_object(socket, R"({"op":"build"})");
Void::Testing::require(build.has_value());
Void::Testing::expect(bool_field(*build, "ok"));
Void::Testing::expect_eq(str_field(*build, "op"), std::string{"build"});
Void::Testing::expect(int_field(*build, "executed") >= 1);
Void::Testing::expect(int_field(*build, "failed") == 0);
Void::Testing::expect(
std::filesystem::exists(options.build_root / "profiles" / "debug" / "out" / "bin" / "app"));
auto cached = request_object(socket, R"({"op":"build"})");
Void::Testing::require(cached.has_value());
Void::Testing::expect(bool_field(*cached, "ok"));
Void::Testing::expect(int_field(*cached, "executed") == 0);
Void::Testing::expect(int_field(*cached, "cached") >= 1);
auto bad_targets = request_object(socket, R"({"op":"build","targets":"app"})");
Void::Testing::require(bad_targets.has_value());
Void::Testing::expect(!bool_field(*bad_targets, "ok", true));
Void::Testing::expect(str_field(*bad_targets, "error").find("must be an array") !=
std::string::npos);
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::expect(!std::filesystem::exists(socket));
}
VOID_TEST(Daemon, build_materializes_path_dependency) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
const auto helper = temp->path() / "helper";
std::filesystem::create_directories(helper);
{
std::ofstream descriptor{helper / "build.cpp"};
descriptor << R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &project) {
project.static_library("helper")
.cxx_standard(Void::Build::CxxStandard::Cxx26)
.sources({"helper.cpp"});
}
)cpp";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{helper / "helper.cpp"};
source << "int helper_value() { return 3; }\n";
Void::Testing::require(source.good());
}
{
std::ofstream descriptor{options.project_root / "build.cpp"};
descriptor << "#include <Void/Build.hpp>\n"
"void build(Void::Build::Project &project) {\n"
" auto dep = project.dependency(\"helper\")\n"
" .path(\"" +
helper.string() +
"\")\n"
" .native_builder();\n"
" auto app = project.executable(\"app\");\n"
" app.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
" .sources({\"main.cpp\"})\n"
" .link(dep.target(\"helper\"));\n"
"}\n";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{options.project_root / "main.cpp"};
source << "int helper_value();\nint main() { return helper_value() == 3 ? 0 : 1; }\n";
Void::Testing::require(source.good());
}
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto build = request_object(socket, R"({"op":"build"})", std::chrono::seconds{120});
Void::Testing::require(build.has_value());
Void::Testing::expect(bool_field(*build, "ok"));
Void::Testing::expect(int_field(*build, "dependencies") >= 1);
Void::Testing::expect(
std::filesystem::exists(options.build_root / "profiles" / "debug" / "out" / "bin" / "app"));
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
}
VOID_TEST(Daemon, ide_query_serves_workspace_targets_and_compdb) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
{
std::ofstream descriptor{options.project_root / "build.cpp"};
descriptor << R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &project) {
auto app = project.executable("app");
app.sources({"main.cpp"});
}
)cpp";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{options.project_root / "main.cpp"};
source << "int main() { return 0; }\n";
Void::Testing::require(source.good());
}
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto raw =
Void::Build::Daemon::request(socket, R"({"op":"ide_query"})", std::chrono::seconds{120});
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::require(raw.has_value());
const auto parsed = parse_json(*raw);
Void::Testing::require(parsed.has_value());
const auto *object = as_object(*parsed);
Void::Testing::require(object != nullptr);
Void::Testing::expect(bool_field(*object, "ok"));
Void::Testing::expect_eq(str_field(*object, "op"), std::string{"ide_query"});
const std::string compdb = str_field(*object, "compdb");
Void::Testing::expect(!compdb.empty());
Void::Testing::expect(std::filesystem::exists(compdb));
const auto *query = field(*object, "query");
Void::Testing::require(query != nullptr);
const auto *query_object = as_object(*query);
Void::Testing::require(query_object != nullptr);
Void::Testing::expect_eq(int_field(*query_object, "schema_version"), std::int64_t{1});
Void::Testing::expect_eq(str_field(*query_object, "profile"), std::string{"debug"});
Void::Testing::expect(raw->find("\"name\":\"app\"") != std::string::npos);
}
VOID_TEST(Daemon, compdb_export_publishes_compilation_database) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
{
std::ofstream descriptor{options.project_root / "build.cpp"};
descriptor << R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &project) {
auto app = project.executable("app");
app.sources({"main.cpp"});
}
)cpp";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{options.project_root / "main.cpp"};
source << "int main() { return 0; }\n";
Void::Testing::require(source.good());
}
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto raw = Void::Build::Daemon::request(socket, R"({"op":"compdb_export"})",
std::chrono::seconds{120});
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::require(raw.has_value());
const auto parsed = parse_json(*raw);
Void::Testing::require(parsed.has_value());
const auto *object = as_object(*parsed);
Void::Testing::require(object != nullptr);
Void::Testing::expect(bool_field(*object, "ok"));
Void::Testing::expect_eq(str_field(*object, "op"), std::string{"compdb"});
Void::Testing::expect(int_field(*object, "entries") >= 1);
const std::string compdb = str_field(*object, "compdb");
Void::Testing::expect(!compdb.empty());
Void::Testing::expect(std::filesystem::exists(compdb));
}
VOID_TEST(Daemon, ide_generate_writes_editor_configs) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
{
std::ofstream descriptor{options.project_root / "build.cpp"};
descriptor << R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &project) {
auto app = project.executable("app");
app.sources({"main.cpp"});
}
)cpp";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{options.project_root / "main.cpp"};
source << "int main() { return 0; }\n";
Void::Testing::require(source.good());
}
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto invalid = request_object(socket, R"({"op":"ide_generate","editor":"bogus"})",
std::chrono::seconds{120});
Void::Testing::require(invalid.has_value());
Void::Testing::expect(!bool_field(*invalid, "ok", true));
auto raw = Void::Build::Daemon::request(
socket, R"({"op":"ide_generate","editor":"clangd","force":true})",
std::chrono::seconds{120});
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::require(raw.has_value());
const auto parsed = parse_json(*raw);
Void::Testing::require(parsed.has_value());
const auto *object = as_object(*parsed);
Void::Testing::require(object != nullptr);
Void::Testing::expect(bool_field(*object, "ok"));
Void::Testing::expect_eq(str_field(*object, "op"), std::string{"ide_generate"});
Void::Testing::expect_eq(str_field(*object, "editor"), std::string{"clangd"});
Void::Testing::expect(!string_array_field(*object, "written").empty());
Void::Testing::expect(std::filesystem::exists(options.project_root / ".clangd"));
}
VOID_TEST(Daemon, watch_poll_reports_changed_paths) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
{
std::ofstream probe{options.project_root / "probe.txt"};
probe << "changed\n";
Void::Testing::require(probe.good());
}
bool saw_probe = false;
for (int attempt = 0; attempt < 100 && !saw_probe; ++attempt) {
auto poll = request_object(socket, R"({"op":"watch_poll"})");
Void::Testing::require(poll.has_value());
for (const auto &path : string_array_field(*poll, "changed")) {
if (path.find("probe.txt") != std::string::npos) {
saw_probe = true;
}
}
if (!saw_probe) {
std::this_thread::sleep_for(std::chrono::milliseconds{20});
}
}
Void::Testing::expect(saw_probe);
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
}
VOID_TEST(Daemon, watch_wait_long_polls_until_change_and_times_out) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto quiet = request_object(socket, R"({"op":"watch_wait","timeout_ms":100})");
Void::Testing::require(quiet.has_value());
Void::Testing::expect(bool_field(*quiet, "ok"));
Void::Testing::expect(bool_field(*quiet, "timed_out"));
Void::Testing::expect(string_array_field(*quiet, "changed").empty());
std::jthread writer{[&] {
std::this_thread::sleep_for(std::chrono::milliseconds{100});
std::ofstream probe{options.project_root / "probe.txt"};
probe << "changed\n";
}};
auto waited = request_object(socket, R"({"op":"watch_wait","timeout_ms":3000})");
writer.join();
Void::Testing::require(waited.has_value());
Void::Testing::expect(bool_field(*waited, "ok"));
Void::Testing::expect(!bool_field(*waited, "timed_out", true));
bool saw_probe = false;
for (const auto &path : string_array_field(*waited, "changed")) {
if (path.find("probe.txt") != std::string::npos) {
saw_probe = true;
}
}
Void::Testing::expect(saw_probe);
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
}
VOID_TEST(Daemon, describe_empty_project_reports_zero_targets) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
{
std::ofstream descriptor{options.project_root / "build.cpp"};
descriptor << R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &project) {}
)cpp";
Void::Testing::require(descriptor.good());
}
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto describe = request_object(socket, R"({"op":"describe"})");
Void::Testing::require(describe.has_value());
Void::Testing::expect(bool_field(*describe, "ok"));
Void::Testing::expect_eq(int_field(*describe, "targets"), std::int64_t{0});
Void::Testing::expect_eq(int_field(*describe, "actions"), std::int64_t{0});
Void::Testing::expect(string_array_field(*describe, "target_names").empty());
Void::Testing::expect(field(*describe, "target_names_truncated") == nullptr);
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::expect(!std::filesystem::exists(socket));
}
VOID_TEST(Daemon, describe_bounds_target_names_for_large_projects) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
{
std::ofstream descriptor{options.project_root / "build.cpp"};
descriptor << R"cpp(#include <string>
#include <Void/Build.hpp>
void build(Void::Build::Project &project) {
for (int i = 0; i < 140; ++i) {
auto target = project.executable(std::to_string(i) + std::string(240, 'a'));
target.sources({"main.cpp"});
}
}
)cpp";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{options.project_root / "main.cpp"};
source << "int main() { return 0; }\n";
Void::Testing::require(source.good());
}
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto raw =
Void::Build::Daemon::request(socket, R"({"op":"describe"})", std::chrono::seconds{120});
Void::Testing::require(raw.has_value());
// 128 names x 200 bytes plus JSON overhead stays well under 32 KiB.
Void::Testing::expect(raw->size() < 32U * 1024U);
const auto parsed = parse_json(*raw);
Void::Testing::require(parsed.has_value());
const auto *object = as_object(*parsed);
Void::Testing::require(object != nullptr);
Void::Testing::expect(bool_field(*object, "ok"));
Void::Testing::expect_eq(int_field(*object, "targets"), std::int64_t{140});
Void::Testing::expect(int_field(*object, "actions") >= 140);
const auto names = string_array_field(*object, "target_names");
Void::Testing::expect_eq(names.size(), std::size_t{128});
for (const auto &name : names) {
Void::Testing::expect(name.size() <= 200U);
}
Void::Testing::expect(bool_field(*object, "target_names_truncated"));
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::expect(!std::filesystem::exists(socket));
}
VOID_TEST(Daemon, describe_and_build_workspace_members) {
auto temp = Void::FileSystem::TempDir::open("vbs_daemon_");
Void::Testing::require(temp.has_value());
const auto options = make_options(temp->path());
const auto ws = options.project_root;
{
std::ofstream workspace{ws / "workspace.cpp"};
workspace << "#include <Void/Build.hpp>\n"
"void workspace(Void::Build::Workspace &workspace) {\n"
" workspace.member(\"lib\", \"packages/lib\");\n"
" workspace.member(\"app\", \"packages/app\");\n"
"}\n";
Void::Testing::require(workspace.good());
}
std::filesystem::create_directories(ws / "packages" / "lib" / "src");
std::filesystem::create_directories(ws / "packages" / "app" / "src");
{
std::ofstream descriptor{ws / "packages" / "lib" / "build.cpp"};
descriptor << R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &project) {
project.static_library("libcore").sources({"src/core.cpp"});
}
)cpp";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{ws / "packages" / "lib" / "src" / "core.cpp"};
source << "int ws_core() { return 7; }\n";
Void::Testing::require(source.good());
}
{
std::ofstream descriptor{ws / "packages" / "app" / "build.cpp"};
descriptor << R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &project) {
project.executable("ws_app").sources({"src/main.cpp"});
}
)cpp";
Void::Testing::require(descriptor.good());
}
{
std::ofstream source{ws / "packages" / "app" / "src" / "main.cpp"};
source << "int main() { return 0; }\n";
Void::Testing::require(source.good());
}
auto server = Void::Build::Daemon::DaemonServer::create(options);
Void::Testing::require(server.has_value());
const auto socket = server->socket_path();
Void::Result::Result<void, Void::Result::Error> run_result;
std::jthread worker{[&] { run_result = server->run(); }};
Void::Testing::require(wait_for_path(socket, std::chrono::seconds{5}));
auto describe_raw =
Void::Build::Daemon::request(socket, R"({"op":"describe"})", std::chrono::seconds{120});
Void::Testing::require(describe_raw.has_value());
Void::Testing::expect(describe_raw->find("\"workspace\":true") != std::string::npos);
Void::Testing::expect(describe_raw->find("\"name\":\"lib\"") != std::string::npos);
Void::Testing::expect(describe_raw->find("\"name\":\"app\"") != std::string::npos);
auto no_member = request_object(socket, R"({"op":"build"})", std::chrono::seconds{120});
Void::Testing::require(no_member.has_value());
Void::Testing::expect(!bool_field(*no_member, "ok", true));
Void::Testing::expect(str_field(*no_member, "error").find("member") != std::string::npos);
auto build_raw = Void::Build::Daemon::request(socket, R"({"op":"build","member":"app"})",
std::chrono::seconds{120});
auto shutdown = Void::Build::Daemon::request(socket, R"({"op":"shutdown"})");
Void::Testing::expect(shutdown.has_value());
worker.join();
Void::Testing::expect(run_result.has_value());
Void::Testing::require(build_raw.has_value());
const auto parsed = parse_json(*build_raw);
Void::Testing::require(parsed.has_value());
const auto *object = as_object(*parsed);
Void::Testing::require(object != nullptr);
Void::Testing::expect(bool_field(*object, "ok"));
Void::Testing::expect_eq(str_field(*object, "member"), std::string{"app"});
Void::Testing::expect(std::filesystem::exists(options.build_root / "profiles" / "debug" /
"out" / "bin" / "ws_app"));
Void::Testing::expect(!std::filesystem::exists(socket));
}