#include "TestExecutable.hpp" #include "TestSupport.hpp" #include "providers/ProviderFixture.hpp" #include #include #include namespace { using namespace provider_test; using Void::LanguageServer::Documents::path_to_file_uri; using Void::Testing::expect; using Void::Testing::expect_eq; using Void::Testing::require; using Void::Testing::require_eq; std::string message(std::string_view method, Json::Object params, std::optional id) { Json::Object object{ {"jsonrpc", str("2.0")}, {"method", str(method)}, {"params", Json{std::move(params)}}}; if (id) object.emplace("id", Json{*id}); auto bytes = Protocol::serialize_json(Json{std::move(object)}); require(bytes.has_value()); return test::frame(*bytes); } Json folder(const Fixture &fixture) { return Json{ Json::Object{{"uri", str(path_to_file_uri(fixture.root))}, {"name", str("Project")}}}; } std::string initialization(Json::Array folders) { return message("initialize", {{"capabilities", Json{Json::Object{}}}, {"workspaceFolders", Json{std::move(folders)}}}, 1) + test::frame(test::initialized); } std::string status(const Fixture &fixture, std::int64_t id, const fs::path &document = {}) { const auto &source = document.empty() ? fixture.source : document; return message("voidLanguageServer/providerStatus", {{"workspaceUri", str(path_to_file_uri(fixture.root))}, {"textDocument", Json{Json::Object{{"uri", str(path_to_file_uri(source))}}}}}, id); } std::vector transcript(std::string wire, std::vector args = {}) { using namespace std::chrono_literals; Void::Process::SpawnOptions options; options.executable = test::server_executable(); options.arguments = std::move(args); options.search_path = false; auto child = Void::Process::spawn(options); require(child.has_value()); wire += test::frame(test::shutdown) + test::frame(test::exit); Void::Process::CaptureLimits limits; limits.max_stdin_bytes = 65536; limits.max_stdout_bytes = 131072; limits.max_stderr_bytes = 16384; auto result = Void::Process::communicate_blocking(*child, wire, limits, 5s); require(result.has_value()); require(result->status.exited && !result->timed_out && !result->cancelled && !result->overflowed); expect_eq(result->status.code, 0); expect(result->err.empty()); return test::responses(result->out); } const std::string &string_field(const Json &record, std::string_view name) { return std::get(test::field(record, name).value); } void expect_ready(const Json &reply, const Fixture &fixture, std::string_view provider) { const auto &result = test::field(reply, "result"); expect_eq(string_field(result, "workspaceUri"), path_to_file_uri(fixture.root)); expect_eq(string_field(result, "provider"), std::string{provider}); expect_eq(string_field(result, "state"), std::string{"ready"}); expect_eq(std::get(test::field(result, "contextCount").value), std::int64_t{2}); expect_eq(std::get(test::field(result, "alternatives").value).size(), std::size_t{2}); expect(std::holds_alternative(test::field(result, "selected").value)); } } // namespace VOID_TEST(ProviderProcess, native_and_compatibility_workspaces_share_one_stdio_session) { Fixture native, ordinary; native.publish(); const auto executable = ordinary.root / "marker-compiler"; write_bytes(executable, "#!/bin/sh\nprintf called > \"$0.called\"\n"); fs::permissions(executable, fs::perms::owner_exec, fs::perm_options::add); auto rows = ordinary.rows(); for (auto &row : rows) { auto &argv = std::get(std::get(row.value).at("arguments").value); argv.front() = str(executable.string()); } write_json(ordinary.compdb, Json{std::move(rows)}); const auto replies = transcript(initialization({folder(native), folder(ordinary)}) + status(native, 10) + status(ordinary, 11)); require_eq(replies.size(), std::size_t{4}); test::expect_sync_capabilities(test::field(replies.front(), "result")); expect_ready(replies[1], native, "vbs"); const auto &result = test::field(replies[1], "result"); expect_eq(string_field(result, "nativeGeneration"), std::string{Fixture::first}); expect_eq(string_field(result, "profile"), std::string{"debug"}); expect_ready(replies[2], ordinary, "compdb"); expect(!fs::exists(ordinary.root / "marker-compiler.called")); } VOID_TEST(ProviderProcess, stale_native_selection_owns_one_root_with_an_available_database) { Fixture native, ordinary; native.publish(); auto stale = native.valid_status(); stale["status"] = str("stale"); stale["diagnostic"] = str("generation unavailable"); stale.erase("entry_count"); stale.erase("readiness_warnings"); write_json(native.editor() / "status.json", Json{std::move(stale)}); write_json(native.compdb, Json{native.rows()}); write_json(ordinary.compdb, Json{ordinary.rows()}); const auto wire = initialization({folder(native), folder(ordinary)}) + status(native, 10) + status(ordinary, 11); for (const auto &args : std::vector>{ {}, {"--workspace-root", native.root.string(), "--provider", "vbs"}}) { const auto replies = transcript(wire, args); require_eq(replies.size(), std::size_t{4}); const auto &result = test::field(replies[1], "result"); expect_eq(string_field(result, "provider"), std::string{"vbs"}); expect_eq(string_field(result, "providerHealth"), std::string{"stale"}); expect_eq(std::get(test::field(result, "contextCount").value), std::int64_t{0}); expect_ready(replies[2], ordinary, "compdb"); } } VOID_TEST(ProviderProcess, missing_external_database_directory_does_not_disable_native_selection) { Fixture native, external; native.publish(); const auto future_db = external.root / "future-build/compile_commands.json"; const std::vector launcher{"--workspace-root", native.root.string(), "--compdb", future_db.string()}; const auto wire = initialization({folder(native)}) + status(native, 9); for (auto provider : {"auto", "vbs", "compdb"}) { auto args = launcher; args.insert(args.end(), {"--provider", provider}); const auto replies = transcript(wire, args); require_eq(replies.size(), std::size_t{3}); if (std::string_view{provider} != "compdb") { expect_ready(replies[1], native, "vbs"); } else { const auto &result = test::field(replies[1], "result"); expect_eq(string_field(result, "state"), std::string{"degraded"}); expect_eq(string_field(result, "provider"), std::string{"compdb"}); } } } VOID_TEST(ProviderProcess, database_binds_the_physical_translation_unit) { Fixture f; const auto owner = f.root / "real/main.cpp"; const auto alias = f.root / "alias.cpp"; write_bytes(owner, "int real;\n"); fs::create_directory(f.root / "real/sub"); fs::create_directory_symlink("real/sub", f.root / "link"); fs::create_symlink("real/main.cpp", alias); Json::Array rows; for (const auto *file : {"link/../main.cpp", "alias.cpp"}) { rows.emplace_back( Json::Object{{"directory", str(f.root.string())}, {"file", str(file)}, {"arguments", Json{Json::Array{str("c++"), str("-c"), str(file)}}}}); } write_json(f.compdb, Json{std::move(rows)}); const auto replies = transcript(initialization({folder(f)}) + status(f, 10) + status(f, 11, owner) + status(f, 12, alias)); require_eq(replies.size(), std::size_t{5}); const auto &innocent = test::field(replies[1], "result"); expect(std::holds_alternative(test::field(innocent, "selected").value)); expect(std::get(test::field(innocent, "alternatives").value).empty()); for (const auto i : {2, 3}) { expect_ready(replies[i], f, "compdb"); const auto &selection = test::field(test::field(replies[i], "result"), "selected"); expect_eq(string_field(selection, "owningTu"), path_to_file_uri(owner)); } } VOID_TEST(ProviderProcess, untraversable_document_spellings_do_not_select_a_real_file) { Fixture f; write_json(f.compdb, Json{f.rows()}); fs::create_symlink("missing-target", f.root / "dangling"); write_bytes(f.root / "ordinary", "file\n"); auto wire = initialization({folder(f)}); std::int64_t id = 10; for (const auto *parent : {"missing", "dangling", "ordinary"}) wire += status(f, id++, f.root / parent / ".." / "main.cpp"); const auto replies = transcript(wire + status(f, id)); require_eq(replies.size(), std::size_t{6}); for (auto i : {1, 2, 3}) { const auto &record = test::field(replies[i], "result"); expect_eq(string_field(record, "state"), std::string{"ready"}); expect(std::holds_alternative(test::field(record, "selected").value)); } expect_ready(replies[4], f, "compdb"); } VOID_TEST(ProviderProcess, malformed_unused_external_locator_does_not_shadow_native_metadata) { Fixture native, external; native.publish(); fs::create_directory(external.root / "existing"); write_bytes(external.compdb, "[]"); const auto wire = initialization({folder(native)}) + status(native, 9); for (const auto &spelling : {(external.root / "existing/../compile_commands.json").native(), external.compdb.native() + "/"}) { for (const auto *provider : {"auto", "vbs", "compdb"}) { const auto replies = transcript(wire, {"--workspace-root", native.root.native(), "--provider", provider, "--compdb", spelling}); require_eq(replies.size(), std::size_t{3}); if (std::string_view{provider} == "compdb") { const auto &record = test::field(replies[1], "result"); expect_eq(string_field(record, "state"), std::string{"degraded"}); expect_eq(string_field(record, "provider"), std::string{"compdb"}); } else { expect_ready(replies[1], native, "vbs"); } } } }