William A. Kennington III | 953de36 | 2022-07-13 17:32:55 -0700 | [diff] [blame] | 1 | #include <stdplus/gtest/tmp.hpp> |
| 2 | |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 3 | #include <filesystem> |
William A. Kennington III | 6417c63 | 2023-07-17 02:56:52 -0700 | [diff] [blame] | 4 | #include <format> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 5 | |
William A. Kennington III | 953de36 | 2022-07-13 17:32:55 -0700 | [diff] [blame] | 6 | namespace stdplus |
| 7 | { |
| 8 | namespace gtest |
| 9 | { |
| 10 | |
| 11 | TestWithTmp::TestWithTmp() : |
William A. Kennington III | 6417c63 | 2023-07-17 02:56:52 -0700 | [diff] [blame] | 12 | casedir(std::format( |
William A. Kennington III | 953de36 | 2022-07-13 17:32:55 -0700 | [diff] [blame] | 13 | "{}/{}", SuiteTmpDir(), |
| 14 | ::testing::UnitTest::GetInstance()->current_test_info()->name())) |
| 15 | { |
| 16 | std::filesystem::create_directory(CaseTmpDir()); |
| 17 | } |
| 18 | |
| 19 | TestWithTmp::~TestWithTmp() |
| 20 | { |
| 21 | std::filesystem::remove_all(CaseTmpDir()); |
| 22 | } |
| 23 | |
| 24 | void TestWithTmp::SetUpTestSuite() |
| 25 | { |
| 26 | std::filesystem::create_directory(SuiteTmpDir()); |
| 27 | } |
| 28 | |
| 29 | void TestWithTmp::TearDownTestSuite() |
| 30 | { |
| 31 | std::filesystem::remove_all(SuiteTmpDir()); |
| 32 | } |
| 33 | |
| 34 | std::string TestWithTmp::SuiteTmpDir() |
| 35 | { |
| 36 | const char* dir = getenv("TMPDIR"); |
| 37 | if (dir == nullptr) |
| 38 | { |
| 39 | dir = "/tmp"; |
| 40 | } |
William A. Kennington III | 6417c63 | 2023-07-17 02:56:52 -0700 | [diff] [blame] | 41 | return std::format( |
William A. Kennington III | 953de36 | 2022-07-13 17:32:55 -0700 | [diff] [blame] | 42 | "{}/{}-{}", dir, |
| 43 | ::testing::UnitTest::GetInstance()->current_test_suite()->name(), |
| 44 | getpid()); |
| 45 | } |
| 46 | |
| 47 | } // namespace gtest |
| 48 | } // namespace stdplus |