#include #include #include #include #include #include Void::Testing::Test read_file_returns_contents() { Void::FileSystem::TempDir tmp; const auto path = tmp / "data.txt"; { std::ofstream f{path}; f << "hello\nworld"; } auto r = Void::FileSystem::read_file(path); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, std::string{"hello\nworld"}); } Void::Testing::Test read_file_fails_on_missing() { auto r = Void::FileSystem::read_file("/this/path/does/not/exist"); Void::Testing::expect(!r.has_value()); Void::Testing::expect(r.error().message().find("read_file") != std::string::npos); } Void::Testing::Test write_file_creates_and_overwrites() { Void::FileSystem::TempDir tmp; const auto path = tmp / "out.txt"; auto r1 = Void::FileSystem::write_file(path, "first"); Void::Testing::expect(r1.has_value()); auto r2 = Void::FileSystem::write_file(path, "second"); Void::Testing::expect(r2.has_value()); auto read_back = Void::FileSystem::read_file(path); Void::Testing::expect(read_back.has_value()); Void::Testing::expect_eq(*read_back, std::string{"second"}); } Void::Testing::Test atomic_write_replaces_target() { Void::FileSystem::TempDir tmp; const auto path = tmp / "snapshot.json"; { std::ofstream f{path}; f << "old contents"; } auto r = Void::FileSystem::atomic_write(path, "new contents"); Void::Testing::expect(r.has_value()); auto read_back = Void::FileSystem::read_file(path); Void::Testing::expect(read_back.has_value()); Void::Testing::expect_eq(*read_back, std::string{"new contents"}); // Staging file should not be left behind on success. auto staging = path; staging += ".tmp"; Void::Testing::expect(!std::filesystem::exists(staging)); } Void::Testing::Test ensure_directory_creates_nested_dirs() { Void::FileSystem::TempDir tmp; const auto nested = tmp / "a/b/c/d"; auto r = Void::FileSystem::ensure_directory(nested); Void::Testing::expect(r.has_value()); Void::Testing::expect(std::filesystem::is_directory(nested)); // Idempotent — second call still succeeds. auto r2 = Void::FileSystem::ensure_directory(nested); Void::Testing::expect(r2.has_value()); } Void::Testing::Test remove_path_handles_missing_and_recursive() { // Missing path is a no-op (success). auto r1 = Void::FileSystem::remove_path("/nonexistent/path"); Void::Testing::expect(r1.has_value()); // Recursive removal. Void::FileSystem::TempDir tmp; const auto nested = tmp / "a/b"; (void)Void::FileSystem::ensure_directory(nested); (void)Void::FileSystem::write_file(nested / "leaf.txt", "x"); auto r2 = Void::FileSystem::remove_path(tmp.path() / "a"); Void::Testing::expect(r2.has_value()); Void::Testing::expect(!std::filesystem::exists(tmp.path() / "a")); } Void::Testing::Test filesystem_file_lock_symlink_swap_reflink_and_timestamps() { Void::FileSystem::TempDir tmp{"void_fs_vl010_"}; // 1. RAII advisory FileLock (shared & exclusive, blocking & try_lock) { const auto lock_path = tmp / "resource.lock"; auto ex = Void::FileSystem::FileLock::lock( lock_path, Void::FileSystem::LockMode::Exclusive); Void::Testing::require(ex.has_value()); Void::Testing::expect(ex->locked()); auto contended_ex = Void::FileSystem::FileLock::try_lock( lock_path, Void::FileSystem::LockMode::Exclusive); Void::Testing::require(contended_ex.has_value()); Void::Testing::expect(!contended_ex->has_value()); auto contended_sh = Void::FileSystem::FileLock::try_lock( lock_path, Void::FileSystem::LockMode::Shared); Void::Testing::require(contended_sh.has_value()); Void::Testing::expect(!contended_sh->has_value()); ex->unlock(); Void::Testing::expect(!ex->locked()); auto sh1 = Void::FileSystem::FileLock::try_lock( lock_path, Void::FileSystem::LockMode::Shared); auto sh2 = Void::FileSystem::FileLock::try_lock( lock_path, Void::FileSystem::LockMode::Shared); Void::Testing::require(sh1.has_value() && sh1->has_value()); Void::Testing::require(sh2.has_value() && sh2->has_value()); } // 2. Atomic symlink swap over missing, existing, and dangling links { const auto link_path = tmp / "current"; Void::Testing::require( Void::FileSystem::atomic_symlink_swap(link_path, "v1") .has_value()); Void::Testing::expect_eq( std::filesystem::read_symlink(link_path).string(), std::string{"v1"}); Void::Testing::require( Void::FileSystem::atomic_symlink_swap(link_path, "v2") .has_value()); Void::Testing::expect_eq( std::filesystem::read_symlink(link_path).string(), std::string{"v2"}); } // 3. reflink_copy_file isolates copied file (never hardlinked) { const auto src = tmp / "origin.bin"; const auto dst = tmp / "cloned.bin"; const std::string payload(128ULL * 1024ULL, 'K'); Void::Testing::require( Void::FileSystem::write_file(src, payload).has_value()); auto cp = Void::FileSystem::reflink_copy_file(src, dst); Void::Testing::require(cp.has_value()); Void::Testing::expect_eq(cp->bytes, payload.size()); Void::Testing::expect_eq( Void::FileSystem::read_file(dst).value(), payload); struct stat st_src {}; struct stat st_dst {}; Void::Testing::require(::lstat(src.c_str(), &st_src) == 0); Void::Testing::require(::lstat(dst.c_str(), &st_dst) == 0); Void::Testing::expect(st_src.st_ino != st_dst.st_ino); Void::Testing::expect_eq(st_src.st_nlink, nlink_t{1}); Void::Testing::expect_eq(st_dst.st_nlink, nlink_t{1}); // overwrite == false must fail cleanly when dst already exists Void::FileSystem::CopyOptions no_overwrite; no_overwrite.overwrite = false; Void::Testing::expect( !Void::FileSystem::reflink_copy_file(src, dst, no_overwrite) .has_value()); // Mutating dst must never alter src Void::Testing::require( Void::FileSystem::write_file(dst, "mutated").has_value()); Void::Testing::expect_eq( Void::FileSystem::read_file(src).value(), payload); } // 4. Canonical timestamp setter with AT_SYMLINK_NOFOLLOW { const auto target = tmp / "real.txt"; const auto link = tmp / "alias.txt"; Void::Testing::require( Void::FileSystem::write_file(target, "data").has_value()); Void::Testing::require( Void::FileSystem::atomic_symlink_swap(link, target).has_value()); constexpr std::int64_t kTargetMtime = 1'700'000'000LL; constexpr std::int64_t kLinkMtime = 1'600'000'123LL; Void::Testing::require( Void::FileSystem::set_canonical_mtime(target, kTargetMtime) .has_value()); Void::Testing::require( Void::FileSystem::set_canonical_mtime(link, kLinkMtime) .has_value()); struct stat st_target {}; struct stat st_link {}; Void::Testing::require(::lstat(target.c_str(), &st_target) == 0); Void::Testing::require(::lstat(link.c_str(), &st_link) == 0); Void::Testing::expect_eq(static_cast(st_target.st_mtim.tv_sec), kTargetMtime); Void::Testing::expect_eq(static_cast(st_link.st_mtim.tv_sec), kLinkMtime); } }