#include #include #include #include #include #include namespace { namespace fs = std::filesystem; using namespace Void::LanguageServer::Build; using Void::Testing::expect; using Void::Testing::require; struct Files { Void::FileSystem::TempDir directory{"vls_provider_files_"}; const fs::path root = fs::canonical(directory.path()) / "workspace"; const fs::path other = root.parent_path() / "external"; ProviderScope scope{.workspace = {root, "one"}}; Files() { fs::create_directories(root); fs::create_directories(other); } void bind() { auto bound = bind_provider_scope(scope); require(bound.has_value()); scope = std::move(*bound); } static void write(const fs::path &path, std::string_view bytes) { fs::create_directories(path.parent_path()); std::ofstream file{path, std::ios::binary}; file << bytes; file.close(); require(file.good()); } auto read(const fs::path &path, FilePurpose purpose = FilePurpose::database, std::size_t limit = 1024) const { return read_provider_file(scope, path, purpose, limit); } }; } // namespace VOID_TEST(ProviderFiles, hashes_and_revalidates_a_stable_regular_file) { Files f; f.bind(); auto path = f.root / "compile_commands.json"; Files::write(path, "abc"); auto captured = f.read(path); require(captured.has_value()); expect(*captured->bytes == "abc"); expect(captured->content_id == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); expect(captured->logical_path == path && captured->canonical_path == path); Files::write(f.root / "unrelated", "data"); expect(provider_file_current(f.scope, *captured)); Files::write(path, "abcd"); expect(!provider_file_current(f.scope, *captured)); expect(*captured->bytes == "abc"); } VOID_TEST(ProviderFiles, root_pins_survive_as_identifiers_and_reject_replacement) { Files f; auto path = f.other / "selected.json"; f.scope.exact_database = path; f.bind(); fs::rename(f.other, f.other.string() + "-old"); Files::write(path, "new"); auto result = f.read(path); require(!result); expect(result.error().code == "changed"); fs::rename(f.root, f.root.string() + "-old"); Files::write(f.root / "x", "x"); auto replaced = f.read(f.root / "x"); require(!replaced); expect(replaced.error().code == "changed"); } VOID_TEST(ProviderFiles, new_incarnation_keeps_the_granted_directories_and_retires_old_evidence) { Files f; const auto db = f.other / "selected.json"; f.scope.exact_database = db; f.bind(); Files::write(db, "original"); const auto first = f.read(db); require(first.has_value()); auto second = reuse_provider_scope(f.scope, "two"); require(second.has_value()); auto next = read_provider_file(*second, db, FilePurpose::database, 1024); require(next.has_value()); expect(same_provider_object(*first, *next)); expect(!provider_file_current(*second, *first)); fs::rename(f.other, f.other.string() + "-first"); Files::write(db, "replacement"); expect(!provider_file_current(*second, *next)); auto third = reuse_provider_scope(f.scope, "three"); require(third.has_value()); auto replaced = read_provider_file(*third, db, FilePurpose::database, 1024); require(!replaced); expect(replaced.error().code == "changed"); Files::write(f.root / "inside", "valid"); expect(read_provider_file(*third, f.root / "inside", FilePurpose::database, 1024).has_value()); } VOID_TEST(ProviderFiles, external_file_grants_are_exact_and_purpose_bound) { Files f; const auto db = f.other / "compile_commands.json", neighbor = f.other / "flags.rsp"; f.scope.exact_database = db; f.bind(); Files::write(db, "[]"); Files::write(neighbor, "-DOUTSIDE"); require(f.read(db).has_value()); expect(!f.read(db, FilePurpose::selection)); expect(!f.read(neighbor)); expect(!f.read(neighbor, FilePurpose::response)); auto tampered = f.scope; tampered.exact_database = neighbor; expect(!read_provider_file(tampered, neighbor, FilePurpose::database, 1024)); fs::remove(db); fs::create_symlink(neighbor, db); expect(!f.read(db)); } VOID_TEST(ProviderFiles, native_build_roots_admit_only_editor_metadata_shapes) { Files f; f.scope.build_root = f.other; f.bind(); const auto editor = f.other / "profiles/debug/editor"; const auto db = editor / "generations/0123456789abcdef/compile_commands.json"; Files::write(editor / "status.json", "{}"); Files::write(db, "[]"); Files::write(f.other / "compile_commands.json", "alias"); expect(f.read(editor / "status.json", FilePurpose::status).has_value()); expect(f.read(db, FilePurpose::native_database).has_value()); expect(!f.read(db) && !f.read(db, FilePurpose::response)); expect(!f.read(f.other / "compile_commands.json", FilePurpose::native_database)); expect(!f.read(editor / "generations/not-an-id/compile_commands.json", FilePurpose::native_database)); } VOID_TEST(ProviderFiles, ordinary_workspace_link_retains_logical_and_physical_identity) { Files f; f.bind(); const auto actual = f.root / "data/db", alias = f.root / "editor/compile_commands.json"; Files::write(actual, "[]"); fs::create_directories(alias.parent_path()); fs::create_symlink("../data/db", alias); auto first = f.read(alias); require(first.has_value()); expect(first->logical_path == alias && first->canonical_path == actual); auto hard = f.root / "also.db"; fs::create_hard_link(actual, hard); auto via_alias = f.read(alias), via_hard = f.read(hard); require(via_alias && via_hard); expect(same_provider_object(*via_alias, *via_hard)); fs::remove(alias); fs::create_symlink(hard, alias); expect(!provider_file_current(f.scope, *via_alias)); Files::write(f.root / "replacement", "[]"); fs::rename(f.root / "replacement", actual); auto replacement = f.read(actual); require(replacement.has_value()); expect(!same_provider_object(*replacement, *via_hard)); } VOID_TEST(ProviderFiles, missing_selection_is_distinct_from_dangling_link_or_reference) { Files f; f.bind(); const auto selector = f.root / ".vbs/editor_selection.json"; auto missing = f.read(selector, FilePurpose::selection); require(!missing); expect(missing.error().code == "missing"); fs::create_directories(selector.parent_path()); fs::create_symlink(f.other / "not-present", selector); auto dangling = f.read(selector, FilePurpose::selection); require(!dangling); expect(dangling.error().code != "missing"); auto status = f.read(f.root / "out/profiles/debug/editor/status.json", FilePurpose::status); require(!status); expect(status.error().code == "missing_reference"); } VOID_TEST(ProviderFiles, bounds_and_file_kinds_are_checked_before_payload_reads) { Files f; f.scope.limits.max_response_file_bytes = 2; f.bind(); const auto path = f.root / "flags.rsp"; Files::write(path, "abc"); auto too_large = f.read(path, FilePurpose::response); require(!too_large); expect(too_large.error().code == "too_large"); f.scope.limits.max_response_file_bytes = 1024; expect(!f.read(path, FilePurpose::response)); const auto fifo = f.root / "pipe"; require(::mkfifo(fifo.c_str(), 0600) == 0); auto nonregular = f.read(fifo); require(!nonregular); expect(nonregular.error().code == "not_regular"); fs::create_symlink(f.other, f.root / "escape"); expect(!f.read(f.root / "escape/flags.rsp", FilePurpose::response)); }