blob: 4d06de559ab0c2541fdcc362dc69e9bb45360b8d [file] [log] [blame]
William A. Kennington III953de362022-07-13 17:32:55 -07001#include <fmt/format.h>
2
William A. Kennington III953de362022-07-13 17:32:55 -07003#include <stdplus/gtest/tmp.hpp>
4
Patrick Williamsd1984dd2023-05-10 16:12:44 -05005#include <filesystem>
6
William A. Kennington III953de362022-07-13 17:32:55 -07007namespace stdplus
8{
9namespace gtest
10{
11
12TestWithTmp::TestWithTmp() :
13 casedir(fmt::format(
14 "{}/{}", SuiteTmpDir(),
15 ::testing::UnitTest::GetInstance()->current_test_info()->name()))
16{
17 std::filesystem::create_directory(CaseTmpDir());
18}
19
20TestWithTmp::~TestWithTmp()
21{
22 std::filesystem::remove_all(CaseTmpDir());
23}
24
25void TestWithTmp::SetUpTestSuite()
26{
27 std::filesystem::create_directory(SuiteTmpDir());
28}
29
30void TestWithTmp::TearDownTestSuite()
31{
32 std::filesystem::remove_all(SuiteTmpDir());
33}
34
35std::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