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