Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/Runner/TemporaryDirectory.cpp
blob: 5b2ce5fa4866bc6e800643c4e6a58334e785a72a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1
#include "TemporaryDirectory.hpp"

#include "Process.hpp"

#include <Progmasoft/Catch3/Runner.hpp>

#include <cstdlib>
#include <filesystem>
#include <mutex>
#include <system_error>

namespace Progmasoft::Catch3::Runner
{
    namespace
    {
        constexpr std::array<const char *, 3> kVariables = {
            "TMPDIR", "TMP", "TEMP"};

        std::mutex &
        CurrentMutex()
        {
            static auto *mutex = new std::mutex();
            return *mutex;
        }

        std::string &
        Current()
        {
            static auto *directory = new std::string();
            return *directory;
        }

        void
        SetVariable(const char *name, const std::optional<std::string> &value)
        {
#if defined(_WIN32)
            // An empty value removes the variable.
            _putenv_s(name, value ? value->c_str() : "");
#else
            if (value)
            {
                setenv(name, value->c_str(), 1);
            }
            else
            {
                unsetenv(name);
            }
#endif
        }
    } // namespace

    TemporaryDirectoryScope::TemporaryDirectoryScope(
        const std::string &directory)
    {
        if (directory.empty())
        {
            return;
        }
        std::error_code error;
        std::filesystem::create_directories(directory, error);
        if (error)
        {
            return;
        }
        active_ = true;
        for (std::size_t index = 0; index < kVariables.size(); ++index)
        {
            if (HasEnvironment(kVariables[index]))
            {
                previous_[index] = ReadEnvironment(kVariables[index]);
            }
            SetVariable(kVariables[index], directory);
        }
        const std::lock_guard lock{CurrentMutex()};
        Current() = directory;
    }

    TemporaryDirectoryScope::~TemporaryDirectoryScope()
    {
        if (!active_)
        {
            return;
        }
        for (std::size_t index = 0; index < kVariables.size(); ++index)
        {
            SetVariable(kVariables[index], previous_[index]);
        }
        const std::lock_guard lock{CurrentMutex()};
        Current().clear();
    }
} // namespace Progmasoft::Catch3::Runner

namespace Progmasoft::Catch3
{
    std::filesystem::path
    TestTemporaryDirectory()
    {
        {
            const std::lock_guard lock{Runner::CurrentMutex()};
            if (!Runner::Current().empty())
            {
                return std::filesystem::path{Runner::Current()};
            }
        }
        std::error_code error;
        auto directory = std::filesystem::temp_directory_path(error);
        return error ? std::filesystem::path{"."} : directory;
    }
} // namespace Progmasoft::Catch3