Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
create_temporary_directory.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "The class is in a detail namespace; non-detail declaration matches the filename"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_FILESYSTEM_CREATE_TEMPORARY_DIRECTORY_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_FILESYSTEM_CREATE_TEMPORARY_DIRECTORY_HPP_
9
10#include <cstdlib>
11#include <string>
12#include <tuple>
13#include <utility>
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/filesystem/path_string.hpp"
17#include "arene/base/filesystem/temporary_directory.hpp"
18#include "arene/base/optional.hpp"
19// parasoft-end-suppress AUTOSAR-A16_2_2-a
20
21// parasoft-begin-suppress AUTOSAR-A7_1_5-a "False positive: these use trailing return types, not auto specifiers"
22// parasoft-begin-suppress CERT_C-EXP37-a "False positive: rule does not mention naming all parameters"
23// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
24// parasoft-begin-suppress CERT_C-POS34-a "Environment variables are Bazel's API for passing test directories"
25// parasoft-begin-suppress AUTOSAR-M18_0_3-d "Environment variables are Bazel's API for passing test directories"
26// parasoft-begin-suppress CERT_C-CON33-a "These thread-unsafe functions are only used in single-threaded tests"
27
28namespace arene {
29namespace base {
30namespace testing {
31namespace filesystem {
32
33namespace detail {
34
35/// @brief helper utility providing a RAII wrapper to restore an environment
36/// variable at end of scope
37///
38class env_var_restorer {
39 /// @brief name of the environment variable to restore upon going out of scope
40 std::string var_name_;
41 /// @brief value to write to @c var_name_ upon going out of scope
42 arene::base::optional<std::string> old_val_;
43
44 public:
45 /// @brief store the current value of a target environment variable, including
46 /// unset ones
47 /// @param var_name name of the target environment variable
48 ///
49 explicit env_var_restorer(std::string var_name)
50 : var_name_(std::move(var_name)) {
51 // NOLINTNEXTLINE(concurrency-mt-unsafe)
52 if (char const* const old_val{getenv(var_name_.c_str())}) {
53 old_val_.emplace(old_val);
54 }
55 }
56
57 /// @brief restore the target environment variable
58 ///
59 ~env_var_restorer() {
60 if (old_val_) {
61 // NOLINTNEXTLINE(concurrency-mt-unsafe)
62 std::ignore = setenv(var_name_.c_str(), old_val_->c_str(), 1);
63 } else {
64 // NOLINTNEXTLINE(concurrency-mt-unsafe)
65 std::ignore = unsetenv(var_name_.c_str());
66 }
67 }
68
69 /// @brief this utility cannot be copied or moved
70 env_var_restorer(env_var_restorer const&) = delete;
71 /// @brief this utility cannot be copied or moved
72 env_var_restorer(env_var_restorer&&) = delete;
73 /// @brief this utility cannot be copied or moved
74 auto operator=(env_var_restorer const&) -> env_var_restorer& = delete;
75 /// @brief this utility cannot be copied or moved
76 auto operator=(env_var_restorer&&) -> env_var_restorer& = delete;
77};
78} // namespace detail
79
80/// @brief construct a temporary directory within the test runtime
81///
82/// Constructs a temporary directory with special handling when the test binary
83/// is invoked with Bazel.
84///
85/// When a test is run under Bazel, Bazel defines the environment variable @c
86/// TEST_TMPDIR. This is guaranteed by Bazel to be unique for each invocation of
87/// <tt>bazel test</tt> and the directory where tests should create files.
88///
89/// This function creates a temporary directory under a root path where:
90/// * the root path is @c TEST_TMPDIR if it is defined;
91/// * otherwise, the root path is @c TMPDIR if it is defined;
92/// * otherwise, the default root path used by @c temporary_directory
93///
94/// @return the created @c temporary_directory value
95///
97 if (char const* const test_tmpdir{getenv("TEST_TMPDIR")}) { // NOLINT(concurrency-mt-unsafe)
98 // parasoft-begin-suppress AUTOSAR-M0_1_3-a "False positive: this is a scope guard, it's used by destroying it"
99 detail::env_var_restorer const restorer{"TMPDIR"};
100 // parasoft-end-suppress AUTOSAR-M0_1_3-a
101
102 // parasoft-begin-suppress AUTOSAR-A27_0_4-d "This is a system API that always takes C-style strings"
103 std::ignore = unsetenv("TMPDIR"); // NOLINT(concurrency-mt-unsafe)
104 // parasoft-end-suppress AUTOSAR-A27_0_4-d
105
106 return arene::base::filesystem::temporary_directory{arene::base::filesystem::path_string{test_tmpdir}};
107 }
108
109 return arene::base::filesystem::temporary_directory{};
110}
111
112} // namespace filesystem
113} // namespace testing
114} // namespace base
115} // namespace arene
116
117// parasoft-end-suppress AUTOSAR-A7_1_5-a
118// parasoft-end-suppress CERT_C-EXP37-a
119// parasoft-end-suppress AUTOSAR-M2_10_1-a
120// parasoft-end-suppress CERT_C-POS34-a
121// parasoft-end-suppress AUTOSAR-M18_0_3-d
122// parasoft-end-suppress CERT_C-CON33-a
123
124#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_FILESYSTEM_CREATE_TEMPORARY_DIRECTORY_HPP_
Definition create_temporary_directory.hpp:31
auto create_temporary_directory() -> arene::base::filesystem::temporary_directory
construct a temporary directory within the test runtime
Definition create_temporary_directory.hpp:96
Definition customization.hpp:36
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10