blob: d5e2ab247324f0e6b17b64a39ca2ad6de75cef34 [file] [log] [blame]
William A. Kennington III953de362022-07-13 17:32:55 -07001#include <stdplus/gtest/tmp.hpp>
2
Patrick Williamsd1984dd2023-05-10 16:12:44 -05003#include <filesystem>
William A. Kennington III6417c632023-07-17 02:56:52 -07004#include <format>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05005
William A. Kennington III953de362022-07-13 17:32:55 -07006namespace stdplus
7{
8namespace gtest
9{
10
11TestWithTmp::TestWithTmp() :
William A. Kennington III6417c632023-07-17 02:56:52 -070012 casedir(std::format(
William A. Kennington III953de362022-07-13 17:32:55 -070013 "{}/{}", SuiteTmpDir(),
14 ::testing::UnitTest::GetInstance()->current_test_info()->name()))
15{
16 std::filesystem::create_directory(CaseTmpDir());
17}
18
19TestWithTmp::~TestWithTmp()
20{
21 std::filesystem::remove_all(CaseTmpDir());
22}
23
24void TestWithTmp::SetUpTestSuite()
25{
26 std::filesystem::create_directory(SuiteTmpDir());
27}
28
29void TestWithTmp::TearDownTestSuite()
30{
31 std::filesystem::remove_all(SuiteTmpDir());
32}
33
34std::string TestWithTmp::SuiteTmpDir()
35{
36 const char* dir = getenv("TMPDIR");
37 if (dir == nullptr)
38 {
39 dir = "/tmp";
40 }
William A. Kennington III6417c632023-07-17 02:56:52 -070041 return std::format(
William A. Kennington III953de362022-07-13 17:32:55 -070042 "{}/{}-{}", dir,
43 ::testing::UnitTest::GetInstance()->current_test_suite()->name(),
44 getpid());
45}
46
47} // namespace gtest
48} // namespace stdplus