summaryrefslogtreecommitdiff
path: root/test/utils/hermeticity/local.hpp
blob: 5e844b50cd5e51bb444c0f40b9ecee5ed459e4ac (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
#ifndef INCLUDED_SRC_TEST_UTILS_HERMETICITY_LOCAL_HPP
#define INCLUDED_SRC_TEST_UTILS_HERMETICITY_LOCAL_HPP

#include "src/buildtool/common/statistics.hpp"
#include "src/buildtool/execution_api/local/config.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/logging/logger.hpp"

class HermeticLocalTestFixture {
  public:
    HermeticLocalTestFixture() noexcept {
        Statistics::Instance().Reset();
        CreateAndSetCleanDiskCache(id_++);
    }

  private:
    static inline int id_{};

    static void CreateAndSetCleanDiskCache(int case_id) {
        auto test_dir = FileSystemManager::GetCurrentDirectory();
        auto case_dir = test_dir / "tmp" / ("case_" + std::to_string(case_id));

        if (FileSystemManager::RemoveDirectory(case_dir, true) and
            FileSystemManager::CreateDirectoryExclusive(case_dir) and
            LocalExecutionConfig::SetBuildRoot(case_dir)) {
            Logger::Log(LogLevel::Debug,
                        "created test-local cache dir {}",
                        case_dir.string());
        }
        else {
            Logger::Log(LogLevel::Error,
                        "failed to create a test-local cache dir {}",
                        case_dir.string());
            std::exit(EXIT_FAILURE);
        }
    }
};

#endif  // INCLUDED_SRC_TEST_UTILS_HERMETICITY_LOCAL_HPP