99 lines
2.8 KiB
C++
99 lines
2.8 KiB
C++
#include <Void/FileSystem/Path.hpp>
|
|
#include <Void/FileSystem/TempDir.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
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"));
|
|
}
|