Files
Library/tests/standalone/FileSystem/TempDir.cpp
T
2026-05-03 18:09:46 +03:00

71 lines
2.0 KiB
C++

#include <Void/FileSystem/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <filesystem>
#include <fstream>
#include <utility>
Void::Testing::Test temp_dir_creates_directory_under_temp()
{
std::filesystem::path captured;
{
Void::FileSystem::TempDir tmp;
captured = tmp.path();
Void::Testing::expect(std::filesystem::is_directory(captured));
Void::Testing::expect(captured.parent_path() ==
std::filesystem::temp_directory_path());
}
// Removed on destruction.
Void::Testing::expect(!std::filesystem::exists(captured));
}
Void::Testing::Test temp_dir_removes_recursive_contents()
{
std::filesystem::path captured;
{
Void::FileSystem::TempDir tmp;
captured = tmp.path();
std::filesystem::create_directories(tmp / "nested/deep");
std::ofstream{tmp / "nested/deep/leaf.txt"} << "data";
Void::Testing::expect(
std::filesystem::exists(tmp / "nested/deep/leaf.txt"));
}
Void::Testing::expect(!std::filesystem::exists(captured));
}
Void::Testing::Test temp_dir_move_transfers_ownership()
{
std::filesystem::path captured;
{
Void::FileSystem::TempDir a;
captured = a.path();
Void::FileSystem::TempDir b{std::move(a)};
// `a` is moved-from; `b` owns the directory.
Void::Testing::expect_eq(b.path(), captured);
Void::Testing::expect(std::filesystem::is_directory(b.path()));
}
Void::Testing::expect(!std::filesystem::exists(captured));
}
Void::Testing::Test temp_dir_open_factory_returns_result()
{
std::filesystem::path captured;
{
auto r = Void::FileSystem::TempDir::open("void_factory_test_");
Void::Testing::expect(r.has_value());
captured = r->path();
Void::Testing::expect(std::filesystem::is_directory(captured));
}
// `r` is destroyed at end of scope, which destroys the contained
// TempDir and removes the directory.
Void::Testing::expect(!std::filesystem::exists(captured));
}
Void::Testing::Test temp_dir_two_concurrent_dirs_have_distinct_paths()
{
Void::FileSystem::TempDir a;
Void::FileSystem::TempDir b;
Void::Testing::expect(a.path() != b.path());
}