Files
Library/tests/standalone/System/DirectoryWatcher.cpp
T

176 lines
6.9 KiB
C++

#include <Void/System/DirectoryWatcher.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <algorithm>
#include <chrono>
#include <filesystem>
#include <string>
#include <vector>
namespace
{
[[nodiscard]] std::vector<Void::FileSystem::DirectoryEvent>
collect_events(Void::FileSystem::DirectoryWatcher& watcher,
std::chrono::milliseconds budget)
{
std::vector<Void::FileSystem::DirectoryEvent> all;
const auto deadline = std::chrono::steady_clock::now() + budget;
while (std::chrono::steady_clock::now() < deadline) {
auto batch = watcher.poll(std::chrono::milliseconds{40});
Void::Testing::require(batch.has_value());
for (auto& ev : *batch) {
all.push_back(std::move(ev));
}
if (!all.empty() && batch->empty()) {
break;
}
}
return all;
}
} // namespace
Void::Testing::Test directory_watcher_tracks_recursive_trees_ignores_and_renames()
{
Void::FileSystem::TempDir tmp{"void_dir_watch_"};
const auto ignored_dir = tmp / ".git";
Void::Testing::require(
Void::FileSystem::ensure_directory(ignored_dir).has_value());
Void::FileSystem::DirectoryWatcherOptions opts;
opts.ignore = [](const std::filesystem::path& p, bool) {
for (const auto& part : p) {
if (part == ".git" || part == "build") {
return true;
}
}
return false;
};
auto w_res = Void::FileSystem::DirectoryWatcher::create(tmp.path(), opts);
Void::Testing::require(w_res.has_value());
auto watcher = std::move(*w_res);
Void::Testing::expect_eq(watcher.watch_count(), std::size_t{1});
// Ignored subtree changes must produce zero events
Void::Testing::require(
Void::FileSystem::write_file(ignored_dir / "HEAD", "ref").has_value());
auto ignored_events = watcher.poll(std::chrono::milliseconds{40});
Void::Testing::require(ignored_events.has_value());
Void::Testing::expect(ignored_events->empty());
// Create nested subdirectories + file immediately -> auto-watched recursively
const auto sub = tmp / "src/core";
Void::Testing::require(Void::FileSystem::ensure_directory(sub).has_value());
Void::Testing::require(
Void::FileSystem::write_file(sub / "main.cpp", "int main(){}")
.has_value());
const auto create_events =
collect_events(watcher, std::chrono::milliseconds{500});
const bool saw_file_create_or_mod = std::any_of(
create_events.begin(), create_events.end(),
[&](const Void::FileSystem::DirectoryEvent& ev) {
return ev.path.filename() == "main.cpp" &&
(ev.kind == Void::FileSystem::DirectoryEventKind::Created ||
ev.kind == Void::FileSystem::DirectoryEventKind::Modified);
});
Void::Testing::expect(saw_file_create_or_mod);
Void::Testing::expect(watcher.watch_count() >= 3U);
// Modify inside auto-watched nested directory
Void::Testing::require(
Void::FileSystem::write_file(sub / "main.cpp", "int main(){return 0;}")
.has_value());
const auto mod_events =
collect_events(watcher, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
mod_events.begin(), mod_events.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind == Void::FileSystem::DirectoryEventKind::Modified &&
ev.path.filename() == "main.cpp";
}));
// File rename pairing (IN_MOVED_FROM / IN_MOVED_TO)
std::filesystem::rename(sub / "main.cpp", sub / "entry.cpp");
const auto ren_events =
collect_events(watcher, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
ren_events.begin(), ren_events.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind == Void::FileSystem::DirectoryEventKind::Renamed &&
ev.old_path.filename() == "main.cpp" &&
ev.path.filename() == "entry.cpp";
}));
// Directory rename rewrites watch paths so modifications inside new path
// still report the new directory path.
const auto renamed_dir = tmp / "src/engine";
std::filesystem::rename(sub, renamed_dir);
(void)collect_events(watcher, std::chrono::milliseconds{300});
Void::Testing::require(
Void::FileSystem::write_file(renamed_dir / "entry.cpp", "// updated")
.has_value());
const auto post_move_events =
collect_events(watcher, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
post_move_events.begin(), post_move_events.end(),
[&](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind == Void::FileSystem::DirectoryEventKind::Modified &&
ev.path == (watcher.root() / "src/engine/entry.cpp");
}));
// Directory replacement / deletion storm
std::filesystem::remove_all(tmp / "src");
const auto del_events =
collect_events(watcher, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
del_events.begin(), del_events.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind == Void::FileSystem::DirectoryEventKind::Deleted;
}));
}
Void::Testing::Test directory_watcher_signals_rescan_on_overflow_and_limit()
{
Void::FileSystem::TempDir tmp{"void_dir_ovf_"};
// 1. Simulated queue overflow emits RescanRequired and wake recovers
{
auto w_res = Void::FileSystem::DirectoryWatcher::create(tmp.path());
Void::Testing::require(w_res.has_value());
w_res->simulate_overflow_for_test();
auto evs = w_res->poll(std::chrono::milliseconds{100});
Void::Testing::require(evs.has_value());
Void::Testing::expect(std::any_of(
evs->begin(), evs->end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind ==
Void::FileSystem::DirectoryEventKind::RescanRequired;
}));
Void::Testing::require(w_res->rescan().has_value());
}
// 2. max_watches exhaustion emits RescanRequired
{
Void::FileSystem::DirectoryWatcherOptions tight;
tight.max_watches = 1U;
auto w_res =
Void::FileSystem::DirectoryWatcher::create(tmp.path(), tight);
Void::Testing::require(w_res.has_value());
Void::Testing::require(
Void::FileSystem::ensure_directory(tmp / "extra_dir").has_value());
const auto evs = collect_events(*w_res, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
evs.begin(), evs.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind ==
Void::FileSystem::DirectoryEventKind::RescanRequired;
}));
}
}