plugin_search_path_t.cc
Go to the documentation of this file.
1 #ifndef _POSIX_C_SOURCE
2 /* For setenv() */
3 #define _POSIX_C_SOURCE 200112L
4 #endif
5 
6 #include "catch2/catch.hpp"
7 
9 #include "cetlib/getenv.h"
10 #include "cetlib/os_libpath.h"
11 #include "cetlib/plugin_libpath.h"
12 #include "cetlib/search_path.h"
13 #include "cetlib/split_path.h"
14 
15 #include <cstdlib>
16 #include <set>
17 #include <string>
18 
19 using cet::os_libpath;
21 using cet::search_path;
23 using std::string;
24 
25 namespace {
26  string const test_path{"/dev/null/nopath:/dev/null/xpath"};
27 
28  std::size_t
29  path_entries(std::string const& path)
30  {
31  return (path.empty() ? 0ull :
32  std::count(path.cbegin(), path.cend(), ':') + 1ull);
33  }
34 
35  std::size_t const tp_sz = path_entries(test_path);
36 
37  string
38  remove_dups_from_path(string const& path)
39  {
40  string result;
41  std::vector<string> path_bits;
42  std::set<string> paths_used;
43  cet::split_path(path, path_bits);
44  for (auto const& bit : path_bits) {
45  if (paths_used.find(bit) == paths_used.end()) {
46  if (!result.empty()) {
47  result.append(":");
48  }
49  result.append(bit);
50  paths_used.insert(bit);
51  }
52  }
53  return result;
54  }
55 }
56 
57 TEST_CASE("Tests")
58 {
59  // Setup.
60  unsetenv(plugin_libpath());
61  string const default_libpath{cet::getenv(os_libpath(), std::nothrow)};
62  if (default_libpath.empty()) {
63  // Need *something*.
64  setenv(os_libpath(), "/dev/null/libpath", 1);
65  }
66  auto const libpath_before = cet::getenv(os_libpath());
67 
68  ////////////////////////////////////
69  // Tests.
70  ////////////////////////////////////
71 
72  // Test using non-empty plugin_libpath().
73  SECTION("Standard")
74  {
75  setenv(plugin_libpath(), test_path.c_str(), 1);
76  string const expected{remove_dups_from_path(cet::getenv(plugin_libpath()) +
77  ":" + libpath_before)};
79  CHECK(tp_sz == sp.size());
80  auto const libpath = cet::getenv(os_libpath());
81  CHECK(expected == libpath);
82  CHECK(tp_sz < path_entries(libpath));
83  }
84 
85  // Test fallback for null plugin_libpath().
86  SECTION("Standard Null")
87  {
88  auto const sp{
90  auto const libpath = cet::getenv(os_libpath());
91  CHECK(path_entries(libpath) == sp.size());
92  CHECK(remove_dups_from_path(libpath_before) == libpath);
93  }
94 
95  // Test lack of fallback for empty plugin_libpath().
96  SECTION("Standard Empty")
97  {
98  setenv(plugin_libpath(), "", 1);
100  CHECK(1ull == sp.size()); // "."
101  CHECK(remove_dups_from_path(libpath_before) == cet::getenv(os_libpath()));
102  }
103 
104  // Test non-empty arbitrary environment variable.
105  SECTION("Custom")
106  {
107  setenv("SIMPLE_VAR", test_path.c_str(), 1);
108  auto const sp{plugin_search_path(search_path{"SIMPLE_VAR"})};
109  auto const libpath = cet::getenv(os_libpath());
110  CHECK(tp_sz == sp.size());
111  string const expected{cet::getenv("SIMPLE_VAR") + ":" + libpath_before};
112  CHECK(remove_dups_from_path(expected) == libpath);
113  CHECK(tp_sz < path_entries(libpath));
114  }
115 
116  // Test null arbitrary environment variable.
117  SECTION("Custom Null")
118  {
119  auto const sp{plugin_search_path(search_path{"MISSING_VAR", std::nothrow})};
120  CHECK(1ull == sp.size()); // "."
121  CHECK(remove_dups_from_path(libpath_before) == cet::getenv(os_libpath()));
122  }
123 
124  // Test empty arbitrary environment variable.
125  SECTION("Custom Empty")
126  {
127  setenv("EMPTY_VAR", "", 1);
128  auto const sp{plugin_search_path(search_path{"EMPTY_VAR"})};
129  CHECK(1ull == sp.size()); // "."
130  CHECK(remove_dups_from_path(libpath_before) == cet::getenv(os_libpath()));
131  }
132 
133  // Teardown.
134  setenv(os_libpath(), default_libpath.c_str(), 1);
135 }
static QCString result
constexpr char const * os_libpath()
Definition: os_libpath.h:12
const char expected[]
Definition: Exception_t.cc:22
std::string string
Definition: nybbler.cc:12
constexpr char const * plugin_libpath()
std::string getenv(std::string const &name)
Definition: getenv.cc:15
TEST_CASE("Tests")
search_path plugin_search_path()
void split_path(std::string const &path, std::vector< std::string > &components)
Definition: split_path.cc:13