File: utest.cpp

package info (click to toggle)
ros2-ament-index 1.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 556 kB
  • sloc: cpp: 655; python: 594; xml: 44; makefile: 16; sh: 5
file content (335 lines) | stat: -rw-r--r-- 12,281 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2015 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include <stdlib.h>

#include <list>
#include <map>
#include <stdexcept>
#include <string>

#include "ament_index_cpp/get_package_prefix.hpp"
#include "ament_index_cpp/get_package_share_directory.hpp"
#include "ament_index_cpp/get_packages_with_prefixes.hpp"
#include "ament_index_cpp/get_resource.hpp"
#include "ament_index_cpp/get_resources.hpp"
#include "ament_index_cpp/get_search_paths.hpp"
#include "ament_index_cpp/has_resource.hpp"

#ifndef AMENT_INDEX_CPP_TEST_PATH
#define AMENT_INDEX_CPP_TEST_PATH __FILE__
#endif

std::string generate_subfolder_path(std::string subfolder)
{
  // Get base path of this file
  std::string base_path = AMENT_INDEX_CPP_TEST_PATH;
  const std::string filename = "utest.cpp";
  base_path = base_path.substr(0, base_path.length() - filename.length() - 1);
  // Generate the base path of the subfolder in this directory
  return base_path + "/" + subfolder;
}

void set_ament_prefix_path(std::list<std::string> subfolders)
{
  std::string ament_prefix_path;
  // Generate a joined string of all absolute paths
  // Subfolders later in the list are searched later in the index
  for (auto subfolder : subfolders) {
    std::string path = generate_subfolder_path(subfolder);
    if (!ament_prefix_path.empty()) {
#ifndef _WIN32
      ament_prefix_path += ":";
#else
      ament_prefix_path += ";";
#endif
    }
    ament_prefix_path += path;
  }
  // Set environment variable
#ifndef _WIN32
  setenv("AMENT_PREFIX_NO_IMPLICIT_USR", "1", 1);
  int retcode = setenv("AMENT_PREFIX_PATH", ament_prefix_path.c_str(), 1);
#else
  errno_t retcode = _putenv_s("AMENT_PREFIX_PATH", ament_prefix_path.c_str());
#endif
  if (retcode) {
    throw std::runtime_error("Failed to set environment variable 'AMENT_PREFIX_PATH'");
  }
}

TEST(AmentIndexCpp, empty_search_paths) {
  std::list<std::string> subfolders;
  set_ament_prefix_path(subfolders);
  EXPECT_THROW(ament_index_cpp::get_search_paths(), std::runtime_error);
}

#ifndef _WIN32
TEST(AmentIndexCpp, implicit_usr_empty_var) {
  std::list<std::string> subfolders;
  set_ament_prefix_path(subfolders);
  setenv("AMENT_PREFIX_NO_IMPLICIT_USR", "", 1);
  std::list<std::string> search_paths = ament_index_cpp::get_search_paths();
  EXPECT_EQ(search_paths.size(), 1UL);
  EXPECT_EQ(search_paths.front(), "/usr");
}

TEST(AmentIndexCpp, implicit_usr_null_var) {
  std::list<std::string> subfolders;
  set_ament_prefix_path(subfolders);
  setenv("AMENT_PREFIX_NO_IMPLICIT_USR", "", 1);
  unsetenv("AMENT_PREFIX_PATH");
  std::list<std::string> search_paths = ament_index_cpp::get_search_paths();
  EXPECT_EQ(search_paths.size(), 1UL);
  EXPECT_EQ(search_paths.front(), "/usr");
}
#endif

TEST(AmentIndexCpp, search_paths) {
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  subfolders.push_back("prefix2");
  set_ament_prefix_path(subfolders);
  std::list<std::string> search_paths = ament_index_cpp::get_search_paths();
  EXPECT_EQ(search_paths.size(), 2UL);
}

TEST(AmentIndexCpp, not_existing_search_paths) {
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  subfolders.push_back("not_existing_prefix");
  set_ament_prefix_path(subfolders);
  std::list<std::string> search_paths = ament_index_cpp::get_search_paths();
  EXPECT_EQ(search_paths.size(), 1UL);
}

TEST(AmentIndexCpp, get_empty_resources) {
  EXPECT_THROW(ament_index_cpp::get_resources(""), std::runtime_error);
}

TEST(AmentIndexCpp, get_unknown_resources) {
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  set_ament_prefix_path(subfolders);
  std::map<std::string, std::string> resources =
    ament_index_cpp::get_resources("unknown_resource_type");
  EXPECT_EQ(resources.size(), 0UL);
}

TEST(AmentIndexCpp, get_resources) {
  // Ensure that if multiple resources of a particular type exist, all are found
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  set_ament_prefix_path(subfolders);
  // There are two resources of this type
  std::map<std::string, std::string> resources = ament_index_cpp::get_resources("resource_type1");
  EXPECT_EQ(resources.size(), 2UL);
  for (auto it : resources) {
    EXPECT_EQ(it.second, generate_subfolder_path("prefix1"));
    EXPECT_TRUE(it.first == "foo" || it.first == "bar");
  }
}

TEST(AmentIndexCpp, get_resources_overlay) {
  // Ensure that a resource type found in two prefixes returns two paths
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  subfolders.push_back("prefix2");
  set_ament_prefix_path(subfolders);
  // This resource type is found in prefix1 and prefix2
  std::map<std::string, std::string> resources = ament_index_cpp::get_resources("resource_type2");
  EXPECT_EQ(resources.size(), 2UL);
  for (auto it : resources) {
    EXPECT_TRUE(it.first == "foo" || it.first == "bar");
  }
}

TEST(AmentIndexCpp, get_resources_underlay) {
  // Ensure that a resource only found in one prefix only returns one path
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  subfolders.push_back("prefix2");
  set_ament_prefix_path(subfolders);
  // This resource type is only found in prefix2
  std::map<std::string, std::string> resources = ament_index_cpp::get_resources("resource_type3");
  EXPECT_EQ(resources.size(), 1UL);
  for (auto it : resources) {
    EXPECT_EQ(it.second, generate_subfolder_path("prefix2"));
    EXPECT_EQ(it.first, "bar");
  }
}

TEST(AmentIndexCpp, get_empty_resource) {
  std::string content;
  EXPECT_THROW(ament_index_cpp::get_resource("", "", content), std::runtime_error);
}

TEST(AmentIndexCpp, get_unknown_resource) {
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  set_ament_prefix_path(subfolders);
  std::string content;
  bool success = ament_index_cpp::get_resource("resource_type4", "bar", content);
  EXPECT_FALSE(success);
}

TEST(AmentIndexCpp, get_resource) {
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  set_ament_prefix_path(subfolders);
  std::string content;
  bool success = ament_index_cpp::get_resource("resource_type4", "foo", content);
  EXPECT_TRUE(success);
  EXPECT_EQ(content, "foo");
}

TEST(AmentIndexCpp, get_resource_underlay) {
  // Ensure that a resource can be found in the underlay if it doesn't exist in the overlay
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  subfolders.push_back("prefix2");
  set_ament_prefix_path(subfolders);
  std::string content;
  // This resource is only found in the underlay
  bool success = ament_index_cpp::get_resource("resource_type2", "bar", content);
  EXPECT_TRUE(success);
  EXPECT_EQ(content, "");
}

TEST(AmentIndexCpp, get_resource_overlay) {
  // Ensure that the resource in the overlay is found before the one in the underlay
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  subfolders.push_back("prefix2");
  set_ament_prefix_path(subfolders);
  std::string content;
  // This resource is in both the overlay and the underlay
  bool success = ament_index_cpp::get_resource("resource_type5", "foo", content);
  EXPECT_TRUE(success);
  EXPECT_EQ(content, "foo1");
}

TEST(AmentIndexCpp, get_resource_overlay_base_path) {
  // Ensure that a resource found in the overlay returns the correct path
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  subfolders.push_back("prefix2");
  set_ament_prefix_path(subfolders);
  std::string content;
  std::string base_path;
  // This resource is only found in the overlay
  bool success = ament_index_cpp::get_resource("resource_type2", "foo", content, &base_path);
  EXPECT_TRUE(success);
  EXPECT_EQ(base_path, generate_subfolder_path("prefix1"));
}

TEST(AmentIndexCpp, get_resource_underlay_base_path) {
  // Ensure that a resource found in the underlay returns the correct path
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");
  subfolders.push_back("prefix2");
  set_ament_prefix_path(subfolders);
  std::string content;
  std::string base_path;
  // This resource is only found in the underlay
  bool success = ament_index_cpp::get_resource("resource_type2", "bar", content, &base_path);
  EXPECT_TRUE(success);
  EXPECT_EQ(base_path, generate_subfolder_path("prefix2"));
}

TEST(AmentIndexCpp, get_package_prefix) {
  // Ensure that a known to exist package is found and that a known to not exist package is not.
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");  // only contains foo and bar packages
  subfolders.push_back("prefix2");  // only contains bar and baz packages
  set_ament_prefix_path(subfolders);
  // foo is found in prefix 1
  EXPECT_EQ(generate_subfolder_path("prefix1"), ament_index_cpp::get_package_prefix("foo"));
  // bar is in both, but prefix 1 takes precedence
  EXPECT_EQ(generate_subfolder_path("prefix1"), ament_index_cpp::get_package_prefix("bar"));
  // baz is found in prefix 2 only
  EXPECT_EQ(generate_subfolder_path("prefix2"), ament_index_cpp::get_package_prefix("baz"));
  // exception when package is not found
  EXPECT_THROW(
    ament_index_cpp::get_package_prefix("does_not_exist"),
    ament_index_cpp::PackageNotFoundError);
  // exception when the package name is empty
  EXPECT_THROW(
    ament_index_cpp::get_package_prefix(""),
    std::runtime_error);
}

TEST(AmentIndexCpp, get_package_share_directory) {
  // Ensure that a known to exist package is found and the share directory is correct.
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");  // only contains foo and bar packages
  subfolders.push_back("prefix2");  // only contains bar and baz packages
  set_ament_prefix_path(subfolders);
  // bar is in both, but prefix 1 takes precedence
  EXPECT_EQ(
    generate_subfolder_path("prefix1") + "/share/bar",
    ament_index_cpp::get_package_share_directory("bar"));
}

TEST(AmentIndexCpp, get_packages_with_prefixes) {
  // Ensure the list of packages and prefixes matches resource layout.
  std::list<std::string> subfolders;
  subfolders.push_back("prefix1");  // only contains foo and bar packages
  subfolders.push_back("prefix2");  // only contains bar and baz packages
  set_ament_prefix_path(subfolders);

  std::map<std::string, std::string> packages_with_prefixes =
    ament_index_cpp::get_packages_with_prefixes();
  // should be exactly three elements
  EXPECT_EQ(3UL, packages_with_prefixes.size());
  // foo is found in prefix 1
  EXPECT_EQ(generate_subfolder_path("prefix1"), packages_with_prefixes["foo"]);
  // bar is in both, but prefix 1 takes precedence
  EXPECT_EQ(generate_subfolder_path("prefix1"), packages_with_prefixes["bar"]);
  // baz is found in prefix 2 only
  EXPECT_EQ(generate_subfolder_path("prefix2"), packages_with_prefixes["baz"]);
}

TEST(AmentIndexCpp, has_empty_name) {
  EXPECT_THROW(
    ament_index_cpp::has_resource("type", ""),
    std::runtime_error);
}

TEST(AmentIndexCpp, has_empty_type) {
  EXPECT_THROW(
    ament_index_cpp::has_resource("", "name"),
    std::runtime_error);
}

TEST(AmentIndexCpp, has_unknown_resource) {
  bool success = ament_index_cpp::has_resource("resource_type4", "bar21");
  EXPECT_FALSE(success);
}

TEST(AmentIndexCpp, has_resource) {
  std::string result_path;

  EXPECT_TRUE(ament_index_cpp::has_resource("resource_type1", "foo", &result_path));
  EXPECT_EQ(result_path, generate_subfolder_path("prefix1"));

  EXPECT_TRUE(ament_index_cpp::has_resource("resource_type3", "bar"));

  EXPECT_TRUE(ament_index_cpp::has_resource("packages", "baz", &result_path));
  EXPECT_EQ(result_path, generate_subfolder_path("prefix2"));

  EXPECT_FALSE(ament_index_cpp::has_resource("resource_type1", "resource", &result_path));
}