Debian Codegraph @

tests/framework/util/env_var_wrapper.h

raw
1 /*
2 * Copyright (c) 2025 The Khronos Group Inc.
3 * Copyright (c) 2025 Valve Corporation
4 * Copyright (c) 2025 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
15 *
16 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
24 *
25 */
26
27 #pragma once
28
29 #include <string>
30 #include <vector>
31
32 #include "test_defines.h"
33
34 // Environment variable list separator - not for filesystem paths
35 #if defined(_WIN32)
36 static const char OS_ENV_VAR_LIST_SEPARATOR = ';';
37 #elif TESTING_COMMON_UNIX_PLATFORMS
38 static const char OS_ENV_VAR_LIST_SEPARATOR = ':';
39 #else
40 #error "Unhandled platform in env_var_wrapper.h!"
41 #endif
42
43 // get_env_var() - returns the contents of env-var `name` as a std::string
44 // If report_failure is true, then it will log to stderr that it didn't find the env-var
45 std::string get_env_var(std::string const& name, bool report_failure = true);
46
47 // split_env_var_as_list() - split env_var_contents into separate elements using the platform
48 // specific env-var list separator.
49 std::vector<std::string> split_env_var_as_list(std::string const& env_var_contents);
50
51 /*
52 * Wrapper around Environment Variables with common operations
53 * Since Environment Variables leak between tests, there needs to be RAII code to remove them during test cleanup
54
55 */
56
57 // Wrapper to set & remove env-vars automatically
58 struct EnvVarWrapper {
59 // Constructor which unsets the env-var
60 EnvVarWrapper(std::string const& name) noexcept;
61 // Constructor which set the env-var to the specified value
62 EnvVarWrapper(std::string const& name, std::string const& value) noexcept;
63 ~EnvVarWrapper() noexcept;
64
65 // delete copy operators
66 EnvVarWrapper(const EnvVarWrapper&) = delete;
67 EnvVarWrapper& operator=(const EnvVarWrapper&) = delete;
68
69 void set_new_value(std::string const& value);
70 void add_to_list(std::string const& list_item);
71 #if defined(_WIN32)
72 void add_to_list(std::wstring const& list_item);
73 #endif
74 void remove_value() const;
75 const char* get() const;
76 const char* value() const;
77
78 private:
79 std::string name;
80 std::string cur_value;
81 std::string initial_value;
82
83 void set_env_var() const;
84 void remove_env_var() const;
85 };