1 /**
2 * Copyright (c) 2021-2023 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
16 #ifndef PANDA_TESTS_PANDA_RUN_COMMON_H
17 #define PANDA_TESTS_PANDA_RUN_COMMON_H
18
19 #include "os/exec.h"
20
21 #include <gtest/gtest.h>
22 #include <string>
23 #include <vector>
24 #include <unistd.h>
25
26 namespace panda::cli::test {
27
Separator()28 inline std::string Separator()
29 {
30 #ifdef _WIN32
31 return "\\";
32 #else
33 return "/";
34 #endif
35 }
36
37 class PandaTest : public testing::Test {
38 public:
39 PandaTest() = default;
40
41 public:
42 template <typename... Args>
RunPandaFile(Args...args)43 inline auto RunPandaFile(Args... args) const
44 {
45 // NOTE(mgonopolskiy): add an ability to specifty env to the Exec function
46 setenv(ASAN_OPTIONS.data(), ASAN_NO_LEAKS.data(), OVERWRITE);
47 auto res = os::exec::Exec(GetPanda().c_str(), args...);
48 unsetenv(ASAN_OPTIONS.data());
49 return res;
50 }
51
52 public:
SetEnv(const std::string & path)53 inline void SetEnv(const std::string &path)
54 {
55 auto pos = path.rfind(Separator());
56 pandaPath_ = path.substr(0, pos) + Separator() + ".." + Separator() + "bin" + Separator() + "ark";
57 sourcePath_ = path.substr(0, pos) + Separator() + ".." + Separator() + "tests" + Separator() + "panda" +
58 Separator() + "bin";
59 }
60
GetPanda()61 inline std::string GetPanda() const
62 {
63 return pandaPath_;
64 }
65
GetBinPath()66 inline std::string GetBinPath() const
67 {
68 return sourcePath_;
69 }
70
71 public:
72 virtual std::string GetDirName() const = 0;
73
74 private:
75 static constexpr size_t OVERWRITE = 1;
76 static constexpr std::string_view ASAN_OPTIONS = "ASAN_OPTIONS";
77 static constexpr std::string_view ASAN_NO_LEAKS = "detect_leaks=0";
78
79 private:
80 std::string pandaPath_;
81 std::string sourcePath_;
82 };
83
84 } // namespace panda::cli::test
85
86 #endif // PANDA_TESTS_PANDA_RUN_COMMON_H
87