Repos
C++
712 B27 lines Opens in site
Directory
Raw
Download
#pragma once
// Spec: SPEC.md NFR-TEST-01. Unique temp directory removed on destruction (real filesystem, no mocks).
#include <unistd.h>
#include <cstdlib>
#include <filesystem>
#include <string>
namespace dp::test {
class TempDir {
public:
TempDir() {
std::string pattern = (std::filesystem::temp_directory_path() / "dpapp-test-XXXXXX").string();
if (!::mkdtemp(pattern.data())) std::abort();
path_ = pattern + "/";
}
~TempDir() { std::error_code ec; std::filesystem::remove_all(path_, ec); }
TempDir(const TempDir&) = delete;
TempDir& operator=(const TempDir&) = delete;
const std::string& path() const { return path_; }
private:
std::string path_;
};
} // namespace dp::test