Debian Codegraph @

tests/testutils.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/tests/testutils.cpp`/include "./testutils.h"cxx . . $ `<file>/tests/testutils.h`/
2
3 #include "../conversion/stringbuilder.h"cxx . . $ `<file>/conversion/stringbuilder.h`/
4 #include "../conversion/stringconversion.h"cxx . . $ `<file>/conversion/stringconversion.h`/
5 #include "../io/ansiescapecodes.h"cxx . . $ `<file>/io/ansiescapecodes.h`/
6 #include "../io/misc.h"cxx . . $ `<file>/io/misc.h`/
7 #include "../io/nativefilestream.h"cxx . . $ `<file>/io/nativefilestream.h`/
8 #include "../misc/parseerror.h"cxx . . $ `<file>/misc/parseerror.h`/
9
10 #include <cerrno>
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring>
14 #include <fstream>
15 #include <initializer_list>
16 #include <iostream>
17
18 #ifdef PLATFORM_UNIXcxx . . $ `application/global.h:56:9`!
19 #ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEMcxx . . $ `<built-in>:484:9`!
20 #include <filesystem>
21 #endif
22 #include <poll.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26 #endif
27
28 #ifdef CPP_UTILITIES_BOOST_PROCESS
29 #include <boost/asio/buffers_iterator.hpp>
30 #include <boost/asio/io_context.hpp>
31 #include <boost/asio/streambuf.hpp>
32 #if BOOST_VERSION >= 108600
33 #include <boost/process/v1/async.hpp>
34 #include <boost/process/v1/child.hpp>
35 #include <boost/process/v1/env.hpp>
36 #include <boost/process/v1/environment.hpp>
37 #include <boost/process/v1/group.hpp>
38 #include <boost/process/v1/io.hpp>
39 #include <boost/process/v1/search_path.hpp>
40 #else
41 #include <boost/process/async.hpp>
42 #include <boost/process/child.hpp>
43 #include <boost/process/env.hpp>
44 #include <boost/process/environment.hpp>
45 #include <boost/process/group.hpp>
46 #include <boost/process/io.hpp>
47 #include <boost/process/search_path.hpp>
48 namespace boost::process {
49 namespace v1 = boost::process;
50 }
51 #endif
52 #endif
53
54 #ifdef PLATFORM_WINDOWS
55 #include <windows.h>
56 #endif
57
58 using namespace std;
59 using namespace CppUtilitiescxx . . $ CppUtilities/::EscapeCodes;
60
61 /*!
62 * \brief Contains classes and functions utilizing creating of test applications.
63 */
64 namespace CppUtilitiescxx . . $ CppUtilities/ {
65
66 /// \cond
67 static bool fileSystemItemExistscxx . . $ CppUtilities/fileSystemItemExists(7c222eea44a24d43).(const stringcxx . . $ std/string# &path)
68 {
69 #ifdef PLATFORM_UNIXcxx . . $ `application/global.h:56:9`!
70 struct stat res;
71 return stat(path.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), &res) == 0;
72 #else
73 const auto widePath(convertMultiByteToWide(path));
74 if (!widePath.first) {
75 return false;
76 }
77 const auto fileType(GetFileAttributesW(widePath.first.get()));
78 return fileType != INVALID_FILE_ATTRIBUTES;
79 #endif
80 }
81
82 static bool fileExistscxx . . $ CppUtilities/fileExists(7c222eea44a24d43).(const stringcxx . . $ std/string# &path)
83 {
84 #ifdef PLATFORM_UNIXcxx . . $ `application/global.h:56:9`!
85 struct stat res;
86 return stat(path.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), &res) == 0 && !S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`!(res.st_mode);
87 #else
88 const auto widePath(convertMultiByteToWide(path));
89 if (!widePath.first) {
90 return false;
91 }
92 const auto fileType(GetFileAttributesW(widePath.first.get()));
93 return (fileType != INVALID_FILE_ATTRIBUTES) && !(fileType & FILE_ATTRIBUTE_DIRECTORY) && !(fileType & FILE_ATTRIBUTE_DEVICE);
94 #endif
95 }
96
97 static bool dirExistscxx . . $ CppUtilities/dirExists(7c222eea44a24d43).(const stringcxx . . $ std/string# &path)
98 {
99 #ifdef PLATFORM_UNIXcxx . . $ `application/global.h:56:9`!
100 struct stat res;
101 return stat(path.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), &res) == 0 && S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`!(res.st_mode);
102 #else
103 const auto widePath(convertMultiByteToWide(path));
104 if (!widePath.first) {
105 return false;
106 }
107 const auto fileType(GetFileAttributesW(widePath.first.get()));
108 return (fileType != INVALID_FILE_ATTRIBUTES) && (fileType & FILE_ATTRIBUTE_DIRECTORY);
109 #endif
110 }
111
112 static bool makeDircxx . . $ CppUtilities/makeDir(7c222eea44a24d43).(const stringcxx . . $ std/string# &path)
113 {
114 #ifdef PLATFORM_UNIXcxx . . $ `application/global.h:56:9`!
115 return mkdir(path.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), S_IRWXUcxx . . $ `<external>/531885356569766532/stat.h:172:9`! | S_IRWXGcxx . . $ `<external>/531885356569766532/stat.h:184:9`! | S_IROTHcxx . . $ `<external>/531885356569766532/stat.h:186:9`! | S_IXOTHcxx . . $ `<external>/531885356569766532/stat.h:188:9`!) == 0;
116 #else
117 const auto widePath(convertMultiByteToWide(path));
118 if (!widePath.first) {
119 return false;
120 }
121 return CreateDirectoryW(widePath.first.get(), nullptr) || GetLastError() == ERROR_ALREADY_EXISTS;
122 #endif
123 }
124 /// \endcond
125
126 TestApplicationcxx . . $ CppUtilities/TestApplication# *TestApplicationcxx . . $ CppUtilities/TestApplication#::s_instancecxx . . $ CppUtilities/TestApplication#s_instance. = nullptr;
127
128 /*!
129 * \class TestApplication
130 * \brief The TestApplication class simplifies writing test applications that require opening test files.
131 * \remarks Only one instance is allowed at a time (singletone class).
132 */
133
134 /*!
135 * \brief Constructs a TestApplication instance without further arguments.
136 * \remarks This constructor skips parsing CLI arguments. Other initialization like reading environment variables
137 * for test file paths and working directories is still done.
138 * \throws Throws std::runtime_error if an instance has already been created.
139 */
140 TestApplicationcxx . . $ CppUtilities/TestApplication#::TestApplicationcxx . . $ CppUtilities/TestApplication#TestApplication(49f6e7a06ebc5aa8).()
141 : TestApplicationcxx . . $ CppUtilities/TestApplication#(0, nullptr)
142 {
143 }
144
145 /*!
146 * \brief Constructs a TestApplication instance for the specified arguments.
147 * \throws Throws std::runtime_error if an instance has already been created.
148 */
149 TestApplicationcxx . . $ CppUtilities/TestApplication#::TestApplicationcxx . . $ CppUtilities/TestApplication#TestApplication(5151383285df94d8).(int argc, const char *const *argv)
150 : m_listArgcxx . . $ CppUtilities/TestApplication#m_listArg.("list", 'l', "lists available test units")
151 , m_runArgcxx . . $ CppUtilities/TestApplication#m_runArg.("run", 'r', "runs the tests")
152 , m_testFilesPathArgcxx . . $ CppUtilities/TestApplication#m_testFilesPathArg.("test-files-path", 'p', "specifies the path of the directory with test files", { "path" })
153 , m_applicationPathArgcxx . . $ CppUtilities/TestApplication#m_applicationPathArg.("app-path", 'a', "specifies the path of the application to be tested", { "path" })
154 , m_workingDirArgcxx . . $ CppUtilities/TestApplication#m_workingDirArg.("working-dir", 'w', "specifies the directory to store working copies of test files", { "path" })
155 , m_unitsArgcxx . . $ CppUtilities/TestApplication#m_unitsArg.("units", 'u', "specifies the units to test; omit to test all units", { "unit1", "unit2", "unit3" })
156 {
157 // check whether there is already an instance
158 if (s_instancecxx . . $ CppUtilities/TestApplication#s_instance.) {
159 throw runtime_errorcxx . . $ std/runtime_error#("only one TestApplication instance allowed at a time");
160 }
161 s_instancecxx . . $ CppUtilities/TestApplication#s_instance. = this;
162
163 // handle specified arguments (if present)
164 if (argc && argv) {
165 // setup argument parser
166 m_testFilesPathArgcxx . . $ CppUtilities/TestApplication#m_testFilesPathArg..setRequiredValueCountcxx . . $ CppUtilities/Argument#setRequiredValueCount(2c44cd906ae4e829).(Argumentcxx . . $ CppUtilities/Argument#::varValueCountcxx . . $ CppUtilities/Argument#varValueCount.);
167 m_unitsArgcxx . . $ CppUtilities/TestApplication#m_unitsArg..setRequiredValueCountcxx . . $ CppUtilities/Argument#setRequiredValueCount(2c44cd906ae4e829).(Argumentcxx . . $ CppUtilities/Argument#::varValueCountcxx . . $ CppUtilities/Argument#varValueCount.);
168 m_runArgcxx . . $ CppUtilities/TestApplication#m_runArg..setImplicitcxx . . $ CppUtilities/Argument#setImplicit(b45ddcbc8423892c).(true);
169 m_runArgcxx . . $ CppUtilities/TestApplication#m_runArg..setSubArgumentscxx . . $ CppUtilities/Argument#setSubArguments(fd9df7cc6299ed0c).({ &m_testFilesPathArgcxx . . $ CppUtilities/TestApplication#m_testFilesPathArg., &m_applicationPathArgcxx . . $ CppUtilities/TestApplication#m_applicationPathArg., &m_workingDirArgcxx . . $ CppUtilities/TestApplication#m_workingDirArg., &m_unitsArgcxx . . $ CppUtilities/TestApplication#m_unitsArg. });
170 m_parsercxx . . $ CppUtilities/TestApplication#m_parser..setMainArgumentscxx . . $ CppUtilities/ArgumentParser#setMainArguments(fd9df7cc6299ed0c).({ &m_runArgcxx . . $ CppUtilities/TestApplication#m_runArg., &m_listArgcxx . . $ CppUtilities/TestApplication#m_listArg., &m_parsercxx . . $ CppUtilities/TestApplication#m_parser..noColorArgcxx . . $ CppUtilities/ArgumentParser#noColorArg(a5cff874ad1ea402).(), &m_parsercxx . . $ CppUtilities/TestApplication#m_parser..helpArgcxx . . $ CppUtilities/ArgumentParser#helpArg(f85ec61c65e3ee59).() });
171
172 // parse arguments
173 try {
174 m_parsercxx . . $ CppUtilities/TestApplication#m_parser..parseArgscxx . . $ CppUtilities/ArgumentParser#parseArgs(539add8bb24c60d4).(argc, argv, ParseArgumentBehaviorcxx . . $ CppUtilities/ParseArgumentBehavior#::CheckConstraintscxx . . $ CppUtilities/ParseArgumentBehavior#CheckConstraints. |cxx . . $ CppUtilities/`operator|`(e25733aab63f2018). ParseArgumentBehaviorcxx . . $ CppUtilities/ParseArgumentBehavior#::InvokeCallbackscxx . . $ CppUtilities/ParseArgumentBehavior#InvokeCallbacks.);
175 } catch (const ParseErrorcxx . . $ CppUtilities/ParseError# &failure) {
176 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/`operator<<`(3b9e0a4d94d289c5). failure;
177 m_validcxx . . $ CppUtilities/TestApplication#m_valid. = false;
178 return;
179 }
180
181 // print help
182 if (m_parsercxx . . $ CppUtilities/TestApplication#m_parser..helpArgcxx . . $ CppUtilities/ArgumentParser#helpArg(f85ec61c65e3ee59).().isPresentcxx . . $ CppUtilities/Argument#isPresent(50ce9a9e25b4a850).()) {
183 exit(0);
184 }
185 }
186
187 // set paths for testfiles
188 // -> set paths set via CLI argument
189 if (m_testFilesPathArgcxx . . $ CppUtilities/TestApplication#m_testFilesPathArg..isPresentcxx . . $ CppUtilities/Argument#isPresent(50ce9a9e25b4a850).()) {
190 for (const char *const testFilesPath : m_testFilesPathArgcxx . . $ CppUtilities/TestApplication#m_testFilesPathArg..valuescxx . . $ CppUtilities/Argument#values(f310c0743d72f851).()) {
191 if (*testFilesPath) {
192 m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths..emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).(argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).(testFilesPath, '/'));
193 } else {
194 m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths..emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).("./");
195 }
196 }
197 }
198 // -> read TEST_FILE_PATH environment variable
199 bool hasTestFilePathFromEnv;
200 if (auto testFilePathFromEnv = readTestfilePathFromEnvcxx . . $ CppUtilities/TestApplication#readTestfilePathFromEnv(efd28698952f529a).(); (hasTestFilePathFromEnv = !testFilePathFromEnv.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).())) {
201 m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths..emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(testFilePathFromEnv));
202 }
203 // -> find source directory
204 if (auto testFilePathFromSrcDirRef = readTestfilePathFromSrcRefcxx . . $ CppUtilities/TestApplication#readTestfilePathFromSrcRef(c1a9bfb60daa162d).(); !testFilePathFromSrcDirRef.emptycxx . . $ std/vector#empty(3482b152b9333168).()) {
205 m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths..insertcxx . . $ std/vector#insert(134ef7853f0b3ebe).(m_testFilesPathscxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(459a7e7b9fbe7640)..endcxx . . $ std/vector#end(55a6de06420d4903).(), stdcxx . . $ std/::make_move_iteratorcxx . . $ std/make_move_iterator(7b29529569bae719).(testFilePathFromSrcDirRef.begincxx . . $ std/vector#begin(55a6de06420d4903).()),
206 stdcxx . . $ std/::make_move_iteratorcxx . . $ std/make_move_iterator(7b29529569bae719).(testFilePathFromSrcDirRef.endcxx . . $ std/vector#end(55a6de06420d4903).()));
207 }
208 // -> try testfiles directory in working directory
209 m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths..emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).("./testfiles/");
210 for (const auto &testFilesPath : m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths.) {
211 cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(88df5bef833f7f83). testFilesPath <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '\n';
212 }
213
214 // set path for working-copy
215 if (m_workingDirArgcxx . . $ CppUtilities/TestApplication#m_workingDirArg..isPresentcxx . . $ CppUtilities/Argument#isPresent(50ce9a9e25b4a850).()) {
216 if (*m_workingDirArgcxx . . $ CppUtilities/TestApplication#m_workingDirArg..valuescxx . . $ CppUtilities/Argument#values(f310c0743d72f851).().frontcxx . . $ std/vector#front(af0004c3ce7cab93).()) {
217 (m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir. =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). m_workingDirArgcxx . . $ CppUtilities/TestApplication#m_workingDirArg..valuescxx . . $ CppUtilities/Argument#values(f310c0743d72f851).().frontcxx . . $ std/vector#front(af0004c3ce7cab93).()) +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). '/';
218 } else {
219 m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir. =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). "./";
220 }
221 } else if (const char *const workingDirEnv = getenv("WORKING_DIR")) {
222 if (*workingDirEnv) {
223 m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir. =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).(workingDirEnv, '/');
224 }
225 } else {
226 if ((m_testFilesPathArgcxx . . $ CppUtilities/TestApplication#m_testFilesPathArg..isPresentcxx . . $ CppUtilities/Argument#isPresent(50ce9a9e25b4a850).() && !m_testFilesPathArgcxx . . $ CppUtilities/TestApplication#m_testFilesPathArg..valuescxx . . $ CppUtilities/Argument#values(f310c0743d72f851).().emptycxx . . $ std/vector#empty(3482b152b9333168).()) || hasTestFilePathFromEnv) {
227 m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir. =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths..frontcxx . . $ std/vector#front(6baabfa6fc7b3a8e).() +cxx . . $ std/operator+(40fc1db9226233c6). "workingdir/";
228 } else {
229 m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir. =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). "./testfiles/workingdir/";
230 }
231 }
232 cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Directory used to store working copies:\n" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir. <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '\n';
233
234 // clear list of all additional profiling files created when forking the test application
235 if (const char *const profrawListFile = getenv("LLVM_PROFILE_LIST_FILE")) {
236 ofstreamcxx . . $ std/basic_ofstream#basic_ofstream(b8cd37071ce335df).(profrawListFile, ios_basecxx . . $ std/ios_base#::trunccxx . . $ std/ios_base#trunc.);
237 }
238
239 m_validcxx . . $ CppUtilities/TestApplication#m_valid. = true;
240 }
241
242 /*!
243 * \brief Destroys the TestApplication.
244 */
245 TestApplicationcxx . . $ CppUtilities/TestApplication#::~cxx . . $ CppUtilities/TestApplication#`~TestApplication`(ced63f7c635d850d).TestApplicationcxx . . $ CppUtilities/TestApplication#()
246 {
247 s_instancecxx . . $ CppUtilities/TestApplication#s_instance. = nullptr;
248 }
249
250 /*!
251 * \brief Returns the full path of the test file with the specified \a relativeTestFilePath.
252 *
253 * The specified \a relativeTestFilePath is considered to be a path to a test file which is relative
254 * to at least one of the considered test file search directories.
255 *
256 * The following directories are searched for test files in the given order:
257 * 1. The directories specified as CLI argument.
258 * 2. The directory set via the environment variable `TEST_FILE_PATH`.
259 * 3. The subdirectory "testfiles" within the source directory, if it could be determined via "srcref"-file.
260 * 4. The subdirectory "testfiles" within present working directory.
261 */
262 stdcxx . . $ std/::stringcxx . . $ std/string# TestApplicationcxx . . $ CppUtilities/TestApplication#::testFilePathcxx . . $ CppUtilities/TestApplication#testFilePath(ed3976f106e8bf8e).(const stdcxx . . $ std/::stringcxx . . $ std/string# &relativeTestFilePath) const
263 {
264 stdcxx . . $ std/::stringcxx . . $ std/string# pathcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
265 for (const auto &testFilesPath : m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths.) {
266 if (fileExistscxx . . $ CppUtilities/fileExists(7c222eea44a24d43).(path =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). testFilesPath +cxx . . $ std/operator+(2764d1739bf7913d). relativeTestFilePath)) {
267 return pathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
268 }
269 }
270 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("The test file \"" %cxx . . $ CppUtilities/`operator%`(26aef0acf8463766). relativeTestFilePath %cxx . . $ CppUtilities/`operator%`(a1b9ce40112533e5). "\" can not be located. Was looking under:\n"
271 +cxx . . $ CppUtilities/operator+(1e9fe778ba38e471). joinStringscxx . . $ CppUtilities/joinStrings(27d6fdf6f9f61b56).(m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths., "\n"cxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792)., false, " - "cxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792)., relativeTestFilePath));
272 }
273
274 /*!
275 * \brief Returns the full path of the test directory with the specified \a relativeTestDirPath.
276 *
277 * This is the same as TestApplication::testFilePath() but for directories. Check out the documentation of
278 * TestApplication::testFilePath() for details about the lookup.
279 */
280 stdcxx . . $ std/::stringcxx . . $ std/string# TestApplicationcxx . . $ CppUtilities/TestApplication#::testDirPathcxx . . $ CppUtilities/TestApplication#testDirPath(ed3976f106e8bf8e).(const stdcxx . . $ std/::stringcxx . . $ std/string# &relativeTestDirPath) const
281 {
282 stdcxx . . $ std/::stringcxx . . $ std/string# pathcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
283 for (const auto &testFilesPath : m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths.) {
284 if (dirExistscxx . . $ CppUtilities/dirExists(7c222eea44a24d43).(path =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). testFilesPath +cxx . . $ std/operator+(2764d1739bf7913d). relativeTestDirPath)) {
285 return pathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
286 }
287 }
288 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("The test directory \"" %cxx . . $ CppUtilities/`operator%`(26aef0acf8463766). relativeTestDirPath %cxx . . $ CppUtilities/`operator%`(a1b9ce40112533e5). "\" can not be located. Was looking under:\n"
289 +cxx . . $ CppUtilities/operator+(1e9fe778ba38e471). joinStringscxx . . $ CppUtilities/joinStrings(27d6fdf6f9f61b56).(m_testFilesPathscxx . . $ CppUtilities/TestApplication#m_testFilesPaths., "\n"cxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792)., false, " - "cxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792)., relativeTestDirPath));
290 }
291
292 /*!
293 * \brief Returns the full path to a working copy of the test file with the specified \a relativeTestFilePath.
294 *
295 * The specified \a mode controls whether a working copy is actually created or whether just the path is returned.
296 *
297 * \remarks The test file is located using testFilePath().
298 */
299 stringcxx . . $ std/string# TestApplicationcxx . . $ CppUtilities/TestApplication#::workingCopyPathcxx . . $ CppUtilities/TestApplication#workingCopyPath(c3c5822f4886442b).(const stringcxx . . $ std/string# &relativeTestFilePath, WorkingCopyModecxx . . $ CppUtilities/WorkingCopyMode# mode) const
300 {
301 return workingCopyPathAscxx . . $ CppUtilities/TestApplication#workingCopyPathAs(e656189f96ce13ac).(relativeTestFilePath, relativeTestFilePath, mode);
302 }
303
304 /*!
305 * \brief Returns the full path to a working copy of the test file with the specified \a relativeTestFilePath.
306 *
307 * The specified \a mode controls whether a working copy is actually created or whether just the path is returned. If only the
308 * path is returned, the \a relativeTestFilePath is ignored.
309 *
310 * In contrast to workingCopyPath(), this method allows to adjust the relative path of the working copy within the working copy
311 * directory via \a relativeWorkingCopyPath.
312 *
313 * \remarks
314 * - The test file specified via \a relativeTestFilePath is located using testFilePath().
315 * - The name of the working copy file specified via \a relativeWorkingCopyPath will be adjusted if it already exists in the file
316 * system and can not be truncated.
317 */
318 stringcxx . . $ std/string# TestApplicationcxx . . $ CppUtilities/TestApplication#::workingCopyPathAscxx . . $ CppUtilities/TestApplication#workingCopyPathAs(e656189f96ce13ac).(
319 const stdcxx . . $ std/::stringcxx . . $ std/string# &relativeTestFilePath, const stdcxx . . $ std/::stringcxx . . $ std/string# &relativeWorkingCopyPath, WorkingCopyModecxx . . $ CppUtilities/WorkingCopyMode# mode) const
320 {
321 // ensure working directory is present
322 auto workingCopyPath = stdcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).::stringcxx . . $ std/string#();
323 if (!dirExistscxx . . $ CppUtilities/dirExists(7c222eea44a24d43).(m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir.) && !makeDircxx . . $ CppUtilities/makeDir(7c222eea44a24d43).(m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir.)) {
324 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Errorcxx . . $ CppUtilities/EscapeCodes/Phrases#Error. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable to create working copy for \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). relativeTestFilePath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\": can't create working directory \""
325 <<cxx . . $ std/`operator<<`(88df5bef833f7f83). m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\"." <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
326 return workingCopyPathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
327 }
328
329 // ensure subdirectory exists
330 const auto parts = splitStringcxx . . $ CppUtilities/splitString(1ed638e56031d731).<vectorcxx . . $ std/vector#<stringcxx . . $ std/string#>>(relativeWorkingCopyPath, "/"cxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792)., EmptyPartsTreatcxx . . $ CppUtilities/EmptyPartsTreat#::Omitcxx . . $ CppUtilities/EmptyPartsTreat#Omit.);
331 if (!parts.emptycxx . . $ std/vector#empty(3482b152b9333168).()) {
332 // create subdirectory level by level
333 stringcxx . . $ std/string# currentLevelcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
334 currentLevel.reservecxx . . $ std/__cxx11/basic_string#reserve(9dac5f4bebd06212).(m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir..sizecxx . . $ std/__cxx11/basic_string#size(6f8bd5c0ea8fb29a).() + relativeWorkingCopyPath.sizecxx . . $ std/__cxx11/basic_string#size(6f8bd5c0ea8fb29a).() + 1);
335 currentLevel.assigncxx . . $ std/__cxx11/basic_string#assign(c0b47be1fddc0e75).(m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir.);
336 for (auto i = parts.cbegincxx . . $ std/vector#cbegin(d1e1699016b9d7e6).(), end = parts.endcxx . . $ std/vector#end(d1e1699016b9d7e6).() -cxx . . $ __gnu_cxx/__normal_iterator#operator-(192bf940fa7bb3b2). 1; i !=cxx . . $ __gnu_cxx/`operator!=`(30ee64f2df4eef8b). end; ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(1613faa01bc3d014).i) {
337 if (currentLevel.backcxx . . $ std/__cxx11/basic_string#back(da5fe8e926c570ad).() != '/') {
338 currentLevel +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). '/';
339 }
340 currentLevel +=cxx . . $ std/__cxx11/basic_string#`operator+=`(c0b47be1fddc0e75). *cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(f9c37c6e427d43d6).i;
341
342 // continue if subdirectory level already exists or we can successfully create the directory
343 if (dirExistscxx . . $ CppUtilities/dirExists(7c222eea44a24d43).(currentLevel) || makeDircxx . . $ CppUtilities/makeDir(7c222eea44a24d43).(currentLevel)) {
344 continue;
345 }
346 // fail otherwise
347 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Errorcxx . . $ CppUtilities/EscapeCodes/Phrases#Error. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable to create working copy for \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). relativeWorkingCopyPath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\": can't create directory \""
348 <<cxx . . $ std/`operator<<`(88df5bef833f7f83). currentLevel <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\" (inside working directory)." <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
349 return workingCopyPathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
350 }
351 }
352
353 workingCopyPath =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir. +cxx . . $ std/operator+(2764d1739bf7913d). relativeWorkingCopyPath;
354 switch (mode) {
355 case WorkingCopyModecxx . . $ CppUtilities/WorkingCopyMode#::NoCopycxx . . $ CppUtilities/WorkingCopyMode#NoCopy.:
356 // just return the path if we don't want to actually create a copy
357 return workingCopyPathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
358 case WorkingCopyModecxx . . $ CppUtilities/WorkingCopyMode#::Cleanupcxx . . $ CppUtilities/WorkingCopyMode#Cleanup.:
359 // ensure the file does not exist in cleanup mode
360 if (stdcxx . . $ std/::removecxx . . $ std/remove(f5ecf3191ed0cd93).(workingCopyPath.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).()) != 0 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
361 const auto error = stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!);
362 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Errorcxx . . $ CppUtilities/EscapeCodes/Phrases#Error. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable to delete \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). workingCopyPath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\": " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). error <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
363 workingCopyPath.clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).();
364 }
365 return workingCopyPathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
366 default:;
367 }
368
369 // copy the file
370 const auto origFilePath = testFilePathcxx . . $ CppUtilities/TestApplication#testFilePath(ed3976f106e8bf8e).(relativeTestFilePath);
371 size_t workingCopyPathAttempt = 0;
372 NativeFileStreamcxx . . $ CppUtilities/NativeFileStream# origFilecxx . . $ CppUtilities/NativeFileStream#NativeFileStream(49f6e7a06ebc5aa8)., workingCopycxx . . $ CppUtilities/NativeFileStream#NativeFileStream(49f6e7a06ebc5aa8).;
373 origFile.opencxx . . $ CppUtilities/NativeFileStream#open(be6c264dafcf0985).(origFilePath, ios_basecxx . . $ std/ios_base#::incxx . . $ std/ios_base#in. |cxx . . $ std/`operator|`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::binarycxx . . $ std/ios_base#binary.);
374 if (origFile.failcxx . . $ std/basic_ios#fail(50ce9a9e25b4a850).()) {
375 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Errorcxx . . $ CppUtilities/EscapeCodes/Phrases#Error. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable to create working copy for \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). relativeTestFilePath
376 <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\": an IO error occurred when opening original file \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). origFilePath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\"." <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
377 cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "error: " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).;
378 workingCopyPath.clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).();
379 return workingCopyPathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
380 }
381 workingCopy.opencxx . . $ CppUtilities/NativeFileStream#open(be6c264dafcf0985).(workingCopyPath, ios_basecxx . . $ std/ios_base#::outcxx . . $ std/ios_base#out. |cxx . . $ std/`operator|`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::binarycxx . . $ std/ios_base#binary. |cxx . . $ std/`operator|`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::trunccxx . . $ std/ios_base#trunc.);
382 while (workingCopy.failcxx . . $ std/basic_ios#fail(50ce9a9e25b4a850).() && fileSystemItemExistscxx . . $ CppUtilities/fileSystemItemExists(7c222eea44a24d43).(workingCopyPath)) {
383 // adjust the working copy path if the target file already exists and can not be truncated
384 workingCopyPath =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).(m_workingDircxx . . $ CppUtilities/TestApplication#m_workingDir., relativeWorkingCopyPath, '.', ++workingCopyPathAttempt);
385 workingCopy.clearcxx . . $ std/basic_ios#clear(3e5fb2eb616d497d).();
386 workingCopy.opencxx . . $ CppUtilities/NativeFileStream#open(be6c264dafcf0985).(workingCopyPath, ios_basecxx . . $ std/ios_base#::outcxx . . $ std/ios_base#out. |cxx . . $ std/`operator|`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::binarycxx . . $ std/ios_base#binary. |cxx . . $ std/`operator|`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::trunccxx . . $ std/ios_base#trunc.);
387 }
388 if (workingCopy.failcxx . . $ std/basic_ios#fail(50ce9a9e25b4a850).()) {
389 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Errorcxx . . $ CppUtilities/EscapeCodes/Phrases#Error. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable to create working copy for \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). relativeTestFilePath
390 <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\": an IO error occurred when opening target file \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). workingCopyPath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\"." <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
391 cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "error: " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).;
392 workingCopyPath.clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).();
393 return workingCopyPathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
394 }
395 workingCopy <<cxx . . $ std/basic_ostream#`operator<<`(6145c063e91b2743). origFile.rdbufcxx . . $ std/basic_ios#rdbuf(fae8afa8316d60d6).();
396 workingCopy.closecxx . . $ CppUtilities/NativeFileStream#close(49f6e7a06ebc5aa8).();
397 if (!origFile.failcxx . . $ std/basic_ios#fail(50ce9a9e25b4a850).() && !workingCopy.failcxx . . $ std/basic_ios#fail(50ce9a9e25b4a850).()) {
398 return workingCopyPathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
399 }
400
401 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Errorcxx . . $ CppUtilities/EscapeCodes/Phrases#Error. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable to create working copy for \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). relativeTestFilePath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\": ";
402 if (origFile.failcxx . . $ std/basic_ios#fail(50ce9a9e25b4a850).()) {
403 cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "an IO error occurred when reading original file \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). origFilePath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\"";
404 workingCopyPath.clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).();
405 return workingCopyPathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
406 }
407 if (workingCopy.failcxx . . $ std/basic_ios#fail(50ce9a9e25b4a850).()) {
408 if (origFile.failcxx . . $ std/basic_ios#fail(50ce9a9e25b4a850).()) {
409 cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " and ";
410 }
411 cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " an IO error occurred when writing to target file \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). workingCopyPath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\".";
412 }
413 cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "error: " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).;
414 workingCopyPath.clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).();
415 return workingCopyPathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
416 }
417
418 #ifdef CPP_UTILITIES_HAS_EXEC_APPcxx . . $ `tests/testutils.h:14:9`!
419
420 #if defined(CPP_UTILITIES_BOOST_PROCESS)
421 inline static std::string streambufToString(boost::asio::streambuf &buf)
422 {
423 const auto begin = boost::asio::buffers_begin(buf.data());
424 return std::string(begin, begin + static_cast<std::ptrdiff_t>(buf.size()));
425 }
426 #endif
427
428 /*!
429 * \brief Executes an application with the specified \a args.
430 * \remarks Provides internal implementation of execApp() and execHelperApp().
431 */
432 static int execAppInternalcxx . . $ CppUtilities/execAppInternal(7c3ce75a1852d96e).(const char *appPath, const char *const *args, stdcxx . . $ std/::stringcxx . . $ std/string# &output, stdcxx . . $ std/::stringcxx . . $ std/string# &errors, bool suppressLogging, int timeout,
433 const stdcxx . . $ std/::stringcxx . . $ std/string# &newProfilingPath, bool enableSearchPath = false)
434 {
435 // print log message
436 if (!suppressLogging) {
437 // print actual appPath and skip first argument instead
438 coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '-' <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ' <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). appPath;
439 if (*args) {
440 for (const char *const *i = args + 1; *i; ++i) {
441 coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ' <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). *i;
442 }
443 }
444 coutcxx . . $ std/cout. <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).;
445 }
446
447 #if defined(CPP_UTILITIES_BOOST_PROCESS)
448 auto path = enableSearchPath ? boost::process::v1::search_path(appPath) : boost::process::v1::filesystem::path(appPath);
449 auto ctx = boost::asio::io_context();
450 auto group = boost::process::v1::group();
451 auto argsAsVector =
452 #if defined(PLATFORM_WINDOWS)
453 std::vector<std::wstring>();
454 #else
455 std::vector<std::string>();
456 #endif
457 if (*args) {
458 for (const char *const *arg = args + 1; *arg; ++arg) {
459 #if defined(PLATFORM_WINDOWS)
460 auto ec = std::error_code();
461 argsAsVector.emplace_back(convertMultiByteToWide(ec, std::string_view(*arg)));
462 if (ec) {
463 throw std::runtime_error(argsToString("unable to convert arg \"", *arg, "\" to wide string"));
464 }
465 #else
466 argsAsVector.emplace_back(*arg);
467 #endif
468 }
469 }
470 auto outputBuffer = boost::asio::streambuf(), errorBuffer = boost::asio::streambuf();
471 auto env = boost::process::v1::environment(boost::this_process::environment());
472 if (!newProfilingPath.empty()) {
473 env["LLVM_PROFILE_FILE"] = newProfilingPath;
474 }
475 auto child = boost::process::v1::child(
476 ctx, group, path, argsAsVector, env, boost::process::v1::std_out > outputBuffer, boost::process::v1::std_err > errorBuffer);
477 if (timeout > 0) {
478 ctx.run_for(std::chrono::milliseconds(timeout));
479 } else {
480 ctx.run();
481 }
482 output = streambufToString(outputBuffer);
483 errors = streambufToString(errorBuffer);
484 child.wait();
485 group.wait();
486 return child.exit_code();
487
488 #elif defined(PLATFORM_UNIXcxx . . $ `application/global.h:56:9`!)
489 // create pipes
490 int coutPipes[2], cerrPipes[2];
491 if (pipe(coutPipes) != 0 || pipe(cerrPipes) != 0) {
492 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#(argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).("Unable to create pipe: ", stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)));
493 }
494 const auto readCoutPipe = coutPipes[0], writeCoutPipe = coutPipes[1];
495 const auto readCerrPipe = cerrPipes[0], writeCerrPipe = cerrPipes[1];
496
497 // create child process
498 if (const auto child = fork()) {
499 // parent process: read stdout and stderr from child
500 close(writeCoutPipe);
501 close(writeCerrPipe);
502
503 try {
504 if (child == -1) {
505 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#(argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).("Unable to create fork: ", stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)));
506 }
507
508 // init file descriptor set for poll
509 struct pollfdcxx . . $ pollfd# fileDescriptorSet[2];
510 fileDescriptorSet[0].fdcxx . . $ pollfd#fd. = readCoutPipe;
511 fileDescriptorSet[1].fdcxx . . $ pollfd#fd. = readCerrPipe;
512 fileDescriptorSet[0].eventscxx . . $ pollfd#events. = fileDescriptorSet[1].eventscxx . . $ pollfd#events. = POLLINcxx . . $ `<external>/17223861378049612178/poll.h:25:9`!;
513
514 // init variables for reading
515 char buffer[512];
516 output.clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).();
517 errors.clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).();
518
519 // poll as long as at least one pipe is open
520 do {
521 const auto retpoll = poll(fileDescriptorSet, 2, timeout);
522 if (retpoll == 0) {
523 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Poll timed out");
524 }
525 if (retpoll < 0) {
526 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#(argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).("Poll failed: ", stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)));
527 }
528 if (fileDescriptorSet[0].reventscxx . . $ pollfd#revents. & POLLINcxx . . $ `<external>/17223861378049612178/poll.h:25:9`!) {
529 const auto count = read(readCoutPipe, buffer, sizeof(buffer));
530 if (count > 0) {
531 output.appendcxx . . $ std/__cxx11/basic_string#append(4f422887dc0eafa2).(buffer, static_cast<size_t>(count));
532 }
533 } else if (fileDescriptorSet[0].reventscxx . . $ pollfd#revents. & POLLHUPcxx . . $ `<external>/17223861378049612178/poll.h:48:9`!) {
534 close(readCoutPipe);
535 fileDescriptorSet[0].fdcxx . . $ pollfd#fd. = -1;
536 }
537 if (fileDescriptorSet[1].reventscxx . . $ pollfd#revents. & POLLINcxx . . $ `<external>/17223861378049612178/poll.h:25:9`!) {
538 const auto count = read(readCerrPipe, buffer, sizeof(buffer));
539 if (count > 0) {
540 errors.appendcxx . . $ std/__cxx11/basic_string#append(4f422887dc0eafa2).(buffer, static_cast<size_t>(count));
541 }
542 } else if (fileDescriptorSet[1].reventscxx . . $ pollfd#revents. & POLLHUPcxx . . $ `<external>/17223861378049612178/poll.h:48:9`!) {
543 close(readCerrPipe);
544 fileDescriptorSet[1].fdcxx . . $ pollfd#fd. = -1;
545 }
546 } while (fileDescriptorSet[0].fdcxx . . $ pollfd#fd. >= 0 || fileDescriptorSet[1].fdcxx . . $ pollfd#fd. >= 0);
547 } catch (...) {
548 // ensure all pipes are closed in the error case
549 close(readCoutPipe);
550 close(readCerrPipe);
551 throw;
552 }
553
554 // get return code
555 int childReturnCode;
556 waitpid(child, &childReturnCode, 0);
557 waitpid(-child, nullptr, 0);
558 return childReturnCode;
559 } else {
560 // child process
561 // -> set pipes to be used for stdout/stderr
562 if (dup2(writeCoutPipe, STDOUT_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:211:9`!) == -1 || dup2(writeCerrPipe, STDERR_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:212:9`!) == -1) {
563 stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Errorcxx . . $ CppUtilities/EscapeCodes/Phrases#Error. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable to duplicate file descriptor: " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
564 stdcxx . . $ std/::exit(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!);
565 }
566 close(readCoutPipe);
567 close(writeCoutPipe);
568 close(readCerrPipe);
569 close(writeCerrPipe);
570
571 // -> create process group
572 if (setpgid(0, 0)) {
573 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Errorcxx . . $ CppUtilities/EscapeCodes/Phrases#Error. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable create process group: " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
574 exit(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!);
575 }
576
577 // -> modify environment variable LLVM_PROFILE_FILE to apply new path for profiling output
578 if (!newProfilingPath.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).()) {
579 setenv("LLVM_PROFILE_FILE", newProfilingPath.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), true);
580 }
581
582 // -> execute application
583 if (enableSearchPath) {
584 execvp(appPath, const_cast<char *const *>(args));
585 } else {
586 execv(appPath, const_cast<char *const *>(args));
587 }
588 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Errorcxx . . $ CppUtilities/EscapeCodes/Phrases#Error. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable to execute \"" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). appPath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\": " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
589 exit(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!);
590 }
591
592 #else
593 throw std::runtime_error("lauching test applications is not supported on this platform");
594 #endif
595 }
596
597 /*!
598 * \brief Executes the application to be tested with the specified \a args and stores the standard output and
599 * errors in \a stdout and \a stderr.
600 * \throws Throws std::runtime_error when the application can not be executed.
601 * \remarks
602 * - The specified \a args must be 0 terminated. The first argument is the application name.
603 * - \a stdout and \a stderr are cleared before.
604 */
605 int TestApplicationcxx . . $ CppUtilities/TestApplication#::execAppcxx . . $ CppUtilities/TestApplication#execApp(846fd0e56e89d1b1).(const char *const *args, stringcxx . . $ std/string# &output, stringcxx . . $ std/string# &errors, bool suppressLogging, int timeout) const
606 {
607 // increase counter used for giving profiling files unique names
608 static unsigned int invocationCount = 0;
609 ++invocationCount;
610
611 // determine the path of the application to be tested
612 const char *appPath = m_applicationPathArgcxx . . $ CppUtilities/TestApplication#m_applicationPathArg..firstValuecxx . . $ CppUtilities/Argument#firstValue(a9f41ea0e82d88cf).();
613 auto fallbackAppPath = stringcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).();
614 if (!appPath || !*appPath) {
615 // try to find the path by removing "_tests"-suffix from own executable path
616 // (the own executable path is the path of the test application and its name is usually the name of the application
617 // to be tested with "_tests"-suffix)
618 const char *const testAppPath = m_parsercxx . . $ CppUtilities/TestApplication#m_parser..executablecxx . . $ CppUtilities/ArgumentParser#executable(a9f41ea0e82d88cf).();
619 const auto testAppPathLength = strlen(testAppPath);
620 if (testAppPathLength > 6 && !strcmp(testAppPath + testAppPathLength - 6, "_tests")) {
621 fallbackAppPath.assigncxx . . $ std/__cxx11/basic_string#assign(4f422887dc0eafa2).(testAppPath, testAppPathLength - 6);
622 appPath = fallbackAppPath.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).();
623 // TODO: it would not hurt to verify whether "fallbackAppPath" actually exists and is executable
624 } else {
625 throw runtime_errorcxx . . $ std/runtime_error#("Unable to execute application to be tested: no application path specified");
626 }
627 }
628
629 // determine new path for profiling output (to not override profiling output of parent and previous invocations)
630 const auto newProfilingPath = [appPath] {
631 auto path = stringcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).();
632 const char *const llvmProfileFile = getenv("LLVM_PROFILE_FILE");
633 if (!llvmProfileFile) {
634 return pathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
635 }
636 // replace eg. "/some/path/tageditor_tests.profraw" with "/some/path/tageditor0.profraw"
637 const char *const llvmProfileFileEnd = strstr(llvmProfileFile, ".profraw");
638 if (!llvmProfileFileEnd) {
639 return pathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
640 }
641 const auto llvmProfileFileWithoutExtension = stringcxx . . $ std/__cxx11/basic_string#basic_string(47be8e3519ed771f).(llvmProfileFile, llvmProfileFileEnd);
642 // extract application name from path
643 const char *appName = strrchr(appPath, '/');
644 appName = appName ? appName + 1 : appPath;
645 // concat new path
646 path =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).(llvmProfileFileWithoutExtension, '_', appName, invocationCount, ".profraw");
647 // append path to profiling list file
648 if (const char *const profrawListFile = getenv("LLVM_PROFILE_LIST_FILE")) {
649 ofstreamcxx . . $ std/basic_ofstream#basic_ofstream(b8cd37071ce335df).(profrawListFile, ios_basecxx . . $ std/ios_base#::appcxx . . $ std/ios_base#app.) <<cxx . . $ std/`operator<<`(88ce98de4a1d46d7). path <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).;
650 }
651 return pathcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
652 }(cxx . . $ CppUtilities/TestApplication#execApp(846fd0e56e89d1b1).$anonymous_type_0#`operator()`(219584694fe2b248).);
653
654 return execAppInternalcxx . . $ CppUtilities/execAppInternal(7c3ce75a1852d96e).(appPath, args, output, errors, suppressLogging, timeout, newProfilingPath);
655 }
656
657 /*!
658 * \brief Executes an application with the specified \a args.
659 * \remarks
660 * - Intended to invoke helper applications (eg. to setup test files). Use execApp() and TestApplication::execApp() to
661 * invoke the application to be tested itself.
662 */
663 int execHelperAppcxx . . $ CppUtilities/execHelperApp(d8bf812d8f21d316).(const char *appPath, const char *const *args, stdcxx . . $ std/::stringcxx . . $ std/string# &output, stdcxx . . $ std/::stringcxx . . $ std/string# &errors, bool suppressLogging, int timeout)
664 {
665 return execAppInternalcxx . . $ CppUtilities/execAppInternal(7c3ce75a1852d96e).(appPath, args, output, errors, suppressLogging, timeout, stringcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).());
666 }
667
668 /*!
669 * \brief Executes an application with the specified \a args.
670 *
671 * Searches for the location of \a appName among the directories specified by the PATH environment variable.
672 *
673 * \remarks
674 * - Intended to invoke helper applications (eg. to setup test files). Use execApp() and TestApplication::execApp() to
675 * invoke the application to be tested itself.
676 */
677 int execHelperAppInSearchPathcxx . . $ CppUtilities/execHelperAppInSearchPath(d8bf812d8f21d316).(
678 const char *appName, const char *const *args, stdcxx . . $ std/::stringcxx . . $ std/string# &output, stdcxx . . $ std/::stringcxx . . $ std/string# &errors, bool suppressLogging, int timeout)
679 {
680 return execAppInternalcxx . . $ CppUtilities/execAppInternal(7c3ce75a1852d96e).(appName, args, output, errors, suppressLogging, timeout, stringcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).(), true);
681 }
682 #endif
683
684 /*!
685 * \brief Reads the path of the test file directory from the environment variable TEST_FILE_PATH.
686 */
687 stringcxx . . $ std/string# TestApplicationcxx . . $ CppUtilities/TestApplication#::readTestfilePathFromEnvcxx . . $ CppUtilities/TestApplication#readTestfilePathFromEnv(efd28698952f529a).()
688 {
689 const char *const testFilesPathEnv = getenv("TEST_FILE_PATH");
690 if (!testFilesPathEnv || !*testFilesPathEnv) {
691 return stringcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).();
692 }
693 return argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).(testFilesPathEnv, '/');
694 }
695
696 /*!
697 * \brief Reads the path of the test file directory from the "srcdirref" file.
698 * \remarks That file is supposed to contain the path the the source directory. It is supposed to be stored by the build system in the
699 * same directory as the test executable. The CMake modules contained of these utilities ensure that's the case.
700 */
701 stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> TestApplicationcxx . . $ CppUtilities/TestApplication#::readTestfilePathFromSrcRefcxx . . $ CppUtilities/TestApplication#readTestfilePathFromSrcRef(c1a9bfb60daa162d).()
702 {
703 // find the path of the current executable on platforms supporting "/proc/self/exe"; otherwise assume the current working directory
704 // is the executable path
705 auto res = stdcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#>();
706 auto binaryPath = stdcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).::stringcxx . . $ std/string#();
707 #if defined(CPP_UTILITIES_USE_STANDARD_FILESYSTEMcxx . . $ `<built-in>:484:9`!) && defined(PLATFORM_UNIXcxx . . $ `application/global.h:56:9`!)
708 try {
709 binaryPath =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::read_symlinkcxx . . $ std/filesystem/read_symlink(4ac62a867fe4334a).("/proc/self/exe"cxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d).).parent_pathcxx . . $ std/filesystem/__cxx11/path#parent_path(159787a050b29eb0).();
710 binaryPath +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). '/';
711 } catch (const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::filesystem_errorcxx . . $ std/filesystem/__cxx11/filesystem_error# &e) {
712 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Warningcxx . . $ CppUtilities/EscapeCodes/Phrases#Warning. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unable to detect binary path for finding \"srcdirref\": " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). e.whatcxx . . $ std/filesystem/__cxx11/filesystem_error#what(ba32b354807b2b81).() <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
713 }
714 #endif
715 const auto srcdirrefPath = binaryPath +cxx . . $ std/operator+(40fc1db9226233c6). "srcdirref";
716 try {
717 // read "srcdirref" file which should contain the path of the source directory
718 const auto srcDirContent = readFilecxx . . $ CppUtilities/readFile(a65ccaa3e10e72de).(srcdirrefPath, 2 * 1024);
719 if (srcDirContent.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).()) {
720 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Warningcxx . . $ CppUtilities/EscapeCodes/Phrases#Warning. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "The file \"srcdirref\" is empty." <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
721 return rescxx . . $ std/vector#vector(6f559d22b039ccdb).;
722 }
723
724 // check whether the referenced source directories contain a "testfiles" directory
725 const auto srcPaths = splitStringSimplecxx . . $ CppUtilities/splitStringSimple(31ac5907b7ebe93f).<stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::string_viewcxx . . $ std/string_view#>>(srcDirContent, "\n"cxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792).);
726 for (const auto &srcPath : srcPaths) {
727 auto testfilesPath = argsToStringcxx . . $ CppUtilities/argsToString(2bd17df601864384).(srcPath, "/testfiles/");
728 if (dirExistscxx . . $ CppUtilities/dirExists(7c222eea44a24d43).(testfilesPath)) {
729 res.emplace_backcxx . . $ std/vector#emplace_back(a1bcdfdabc3a37df).(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(testfilesPath));
730 } else {
731 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Warningcxx . . $ CppUtilities/EscapeCodes/Phrases#Warning.
732 <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "The source directory referenced by the file \"srcdirref\" does not contain a \"testfiles\" directory or does not exist."
733 <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Endcxx . . $ CppUtilities/EscapeCodes/Phrases#End. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Referenced source directory: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). testfilesPath <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).;
734 }
735 }
736 return rescxx . . $ std/vector#vector(6f559d22b039ccdb).;
737
738 } catch (const stdcxx . . $ std/::ios_basecxx . . $ std/ios_base#::failurecxx . . $ std/ios_base#failure# &e) {
739 cerrcxx . . $ std/cerr. <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::Warningcxx . . $ CppUtilities/EscapeCodes/Phrases#Warning. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "The file \"" <<cxx . . $ std/`operator<<`(88df5bef833f7f83). srcdirrefPath <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\" can not be opened: " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). e.whatcxx . . $ std/ios_base#failure#what(ba32b354807b2b81).() <<cxx . . $ CppUtilities/EscapeCodes/`operator<<`(75f7075e21abbe9a). Phrasescxx . . $ CppUtilities/EscapeCodes/Phrases#::EndFlushcxx . . $ CppUtilities/EscapeCodes/Phrases#EndFlush.;
740 }
741 return rescxx . . $ std/vector#vector(6f559d22b039ccdb).;
742 }
743 } // namespace CppUtilities