1 /**
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef PANDA_TESTS_PANDA_RUN_COMMON_H
16 #define PANDA_TESTS_PANDA_RUN_COMMON_H
17
18 #include "os/exec.h"
19
20 #include <gtest/gtest.h>
21 #include <string>
22 #include <vector>
23 #include <unistd.h>
24
25 namespace ark::cli::test {
26
Separator()27 inline std::string Separator()
28 {
29 #ifdef _WIN32
30 return "\\";
31 #else
32 return "/";
33 #endif
34 }
35
36 class PandaTest : public testing::Test {
37 public:
38 PandaTest() = default;
39
40 public:
41 template <typename... Args>
RunPandaFile(Args...args)42 inline auto RunPandaFile(Args... args) const
43 {
44 // NOTE(mgonopolskiy): add an ability to specifty env to the Exec function
45 setenv(ASAN_OPTIONS.data(), ASAN_NO_LEAKS.data(), OVERWRITE);
46 auto res = os::exec::Exec(GetPanda().c_str(), args...);
47 unsetenv(ASAN_OPTIONS.data());
48 return res;
49 }
50
51 public:
SetEnv(const std::string & path)52 inline void SetEnv(const std::string &path)
53 {
54 auto pos = path.rfind(Separator());
55 pandaPath_ = path.substr(0, pos) + Separator() + ".." + Separator() + "bin" + Separator() + "ark";
56 sourcePath_ = path.substr(0, pos) + Separator() + ".." + Separator() + "tests" + Separator() + "panda" +
57 Separator() + "bin";
58 }
59
GetPanda()60 inline std::string GetPanda() const
61 {
62 return pandaPath_;
63 }
64
GetBinPath()65 inline std::string GetBinPath() const
66 {
67 return sourcePath_;
68 }
69
70 public:
71 virtual std::string GetDirName() const = 0;
72
73 private:
74 static constexpr size_t OVERWRITE = 1;
75 static constexpr std::string_view ASAN_OPTIONS = "ASAN_OPTIONS";
76 static constexpr std::string_view ASAN_NO_LEAKS = "detect_leaks=0";
77
78 private:
79 std::string pandaPath_;
80 std::string sourcePath_;
81 };
82
83 } // namespace ark::cli::test
84
85 #endif // PANDA_TESTS_PANDA_RUN_COMMON_H
86