Gazebo Common

API Reference

5.7.0
gz/common/testing/TestPaths.hh
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2022 Open Source Robotics Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17 #ifndef GZ_COMMON_TESTING_TESTPATHS_HH_
18 #define GZ_COMMON_TESTING_TESTPATHS_HH_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "gz/common/Filesystem.hh"
25 #include "gz/common/Util.hh"
26 
27 #include "gz/common/testing/Export.hh"
28 
29 #ifndef TESTING_PROJECT_SOURCE_DIR
30 #define TESTING_PROJECT_SOURCE_DIR ""
31 #endif
32 
33 namespace gz::common::testing
34 {
35 
46 
49 enum class GZ_COMMON_TESTING_VISIBLE BuildType
50 {
53  kBazel
54 };
55 
62 class TestPaths
63 {
66  public: GZ_COMMON_TESTING_VISIBLE
67  explicit TestPaths(const std::string &_projectSourcePath =
69 
71  public: GZ_COMMON_TESTING_VISIBLE
72  virtual ~TestPaths() = 0;
73 
77  public: virtual bool GZ_COMMON_TESTING_VISIBLE
78  ProjectSourcePath(std::string &_sourceDir) = 0;
79 
83  public: virtual bool GZ_COMMON_TESTING_VISIBLE
84  TestTmpPath(std::string &_tmpDir) = 0;
85 
88 };
89 
102 GZ_COMMON_TESTING_VISIBLE
103 MakeTestTempDirectoryImpl(const std::string &_projectSourcePath,
104  const std::string &_prefix = "test",
105  const std::string &_subDir = "gz",
106  bool _cleanup = true);
107 
108 
121 MakeTestTempDirectory(const std::string &_prefix = "test",
122  const std::string &_subDir = "gz",
123  bool _cleanup = true)
124 {
126  _prefix,
127  _subDir,
128  _cleanup);
129 }
130 
136 BuildType
137 GZ_COMMON_TESTING_VISIBLE
139  const std::string &_projectSourcePath = kTestingProjectSourceDir);
140 
147 GZ_COMMON_TESTING_VISIBLE
149  const std::string &_projectSourcePath = kTestingProjectSourceDir);
150 
159 template <typename... Args>
160 std::string SourceFile(Args const &... args)
161 {
162  auto testPaths = TestPathFactory(kTestingProjectSourceDir);
163  assert(nullptr != testPaths);
164 
165  std::string dataDir;
166  testPaths->ProjectSourcePath(dataDir);
167  return common::joinPaths(dataDir, args...);
168 }
169 
178 template <typename... Args>
179 std::string TestFile(Args const &... args)
180 {
181  return SourceFile("test", args...);
182 }
183 
192 template <typename... Args>
193 std::string TempPath(Args const &... args)
194 {
195  auto testPaths = TestPathFactory(kTestingProjectSourceDir);
196  assert(nullptr != testPaths);
197 
198  std::string dataDir;
199  testPaths->TestTmpPath(dataDir);
200  return common::joinPaths(dataDir, args...);
201 }
202 } // namespace gz::common::testing
203 #endif // GZ_COMMON_TESTING_TESTPATHS_HH_