Fresh, uniquely named directory for one test binary's temp files: portable mkdtemp semantics -- unpredictable name, atomic create that fails on a squatted name, owner-only permissions. Base comes from gtest's TempDir(), which honors TEST_TMPDIR when the CI runner provides a per-test private dir.
| 46 | // gtest's TempDir(), which honors TEST_TMPDIR when the CI runner |
| 47 | // provides a per-test private dir. |
| 48 | inline std::filesystem::path scratch_dir() { |
| 49 | namespace fs = std::filesystem; |
| 50 | const fs::path base{::testing::TempDir()}; |
| 51 | std::random_device rd; |
| 52 | for (int attempt = 0; attempt < 16; ++attempt) { |
| 53 | const std::string name = |
| 54 | std::format("oid_scratch_{:x}{:x}", rd(), rd()); |
| 55 | const fs::path dir = base / name; |
| 56 | if (fs::create_directory(dir)) { // atomic; false if name taken |
| 57 | fs::permissions( |
| 58 | dir, fs::perms::owner_all, fs::perm_options::replace); |
| 59 | return dir; |
| 60 | } |
| 61 | } |
| 62 | throw ScratchDirError{"scratch_dir: could not create a unique dir"}; |
| 63 | } |
| 64 | |
| 65 | } // namespace oid::test |
| 66 |