summaryrefslogtreecommitdiff
path: root/test/testdir.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-10-07 20:56:33 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-10-19 00:13:47 +0200
commit62a4abb9bf6551417130c3c6f9bba147930895ef (patch)
tree51c6359faa7193e82c6fa8ee5402edb24d450ab1 /test/testdir.cc
parent4f6e76493fb74f5385d5a14dce3a01c9901802ed (diff)
cfg: New module
Reads config files
Diffstat (limited to 'test/testdir.cc')
-rw-r--r--test/testdir.cc37
1 files changed, 37 insertions, 0 deletions
diff --git a/test/testdir.cc b/test/testdir.cc
new file mode 100644
index 0000000..d5aef22
--- /dev/null
+++ b/test/testdir.cc
@@ -0,0 +1,37 @@
+#include "testdir.hh"
+
+#include <cstdlib>
+#include <filesystem>
+#include <format>
+#include <gtest/gtest.h>
+#include <system_error>
+
+namespace {
+
+std::filesystem::path mktmpdir() {
+ std::error_code ec;
+ auto base = std::filesystem::temp_directory_path(ec);
+ if (ec)
+ return {};
+ for (int i = 0; i < 10; ++i) {
+ auto name = std::format(
+ "{}-{}",
+ ::testing::UnitTest::GetInstance()->current_test_info()->name(),
+ rand());
+ auto tmpdir = base / name;
+ if (std::filesystem::exists(tmpdir))
+ continue;
+ if (std::filesystem::create_directory(tmpdir, ec))
+ return tmpdir;
+ }
+ return {};
+}
+
+} // namespace
+
+TestDir::TestDir() : path_(mktmpdir()) {}
+
+TestDir::~TestDir() {
+ if (!path_.empty())
+ std::filesystem::remove_all(path_);
+}