diff options
Diffstat (limited to 'test/testdir.cc')
| -rw-r--r-- | test/testdir.cc | 37 |
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_); +} |
