filepath_maker_test.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // filepath_maker_test
4 //
5 // ======================================================================
6 
7 #define BOOST_TEST_MODULE (filepath_maker test)
8 #include "boost/test/unit_test.hpp"
9 
10 #include "boost/filesystem.hpp"
11 #include "cetlib/filepath_maker.h"
12 #include "cetlib/filesystem.h"
13 #include "cetlib/getenv.h"
14 #include "cetlib_except/exception.h"
15 
16 #include <string>
17 
18 namespace bfs = boost::filesystem;
19 using namespace std::string_literals;
20 
21 namespace {
22  std::string const path{"FILEPATH_MAKER_TEST_FILES"};
23  std::string const file_in_current_dir{"filepath_maker_test.txt"};
24 
25  inline std::string
26  current_dir()
27  {
28  return cet::getenv("CURRENT_DIR");
29  }
30 
31  inline std::string
32  current_nested_dir()
33  {
34  return current_dir() + "/filepath_maker-files";
35  }
36 
37  void
38  check_exception(cet::filepath_maker& maker, std::string const& filepath)
39  {
40  BOOST_CHECK_EXCEPTION(
41  maker(filepath), cet::exception, [](cet::exception const& e) {
42  return e.category() == "search_path";
43  });
44  }
45 }
46 
47 BOOST_AUTO_TEST_SUITE(filepath_maker_test)
48 
49 // operator() does not translate the input to a full path at all
50 BOOST_AUTO_TEST_CASE(filepath_maker_t)
51 {
53  auto const files = {"a.txt", "./b.txt", "/c/d.txt"};
54  for (auto const& filename : files) {
55  BOOST_TEST(filename == maker(filename));
56  }
57 }
58 
59 BOOST_AUTO_TEST_CASE(filepath_lookup_t1)
60 {
61  // Check that
63  auto const files = {"a.txt"s, "b.txt"s, "c.txt"s};
64  for (auto const& filename : files) {
65  auto const fullPath1 = maker(filename);
66  // Adding './' or '/' makes no difference when everything is
67  // looked up relative to the specified paths.
68  auto const fullPath2 = maker("./" + filename);
69  auto const fullPath3 = maker("/" + filename);
70  bfs::path const p1{fullPath1};
71  bfs::path const p2{fullPath2};
72  bfs::path const p3{fullPath3};
73  BOOST_TEST(bfs::equivalent(p1, p2));
74  BOOST_TEST(bfs::equivalent(p1, p3));
75  // Check that operator() has created an absolute filepath.
76  BOOST_TEST(cet::is_absolute_filepath(fullPath1));
77  BOOST_TEST(cet::is_absolute_filepath(fullPath2));
78  BOOST_TEST(cet::is_absolute_filepath(fullPath3));
79  // Now check that the file *cannot* be accessed via its absolute filepath.
80  check_exception(maker, current_nested_dir() + '/' + filename);
81  }
82 }
83 
84 BOOST_AUTO_TEST_CASE(filepath_lookup_t2)
85 {
87  // Check that we cannot access a file in the current source
88  // directory.
89  check_exception(maker, file_in_current_dir);
90 }
91 
92 BOOST_AUTO_TEST_CASE(filepath_lookup_nonabsolute_t)
93 {
95  auto const files = {"a.txt"s, "b.txt"s, "c.txt"s};
96  for (auto const& filename : files) {
97  auto const fullPath1 = maker(filename);
98  // Test that it can be accessed via the absolute path
99  auto const fullPath2 = maker(current_nested_dir() + '/' + filename);
100  bfs::path const p1{fullPath1};
101  bfs::path const p2{fullPath2};
102  BOOST_TEST(bfs::equivalent(p1, p2));
103  // Check that operator() has created an absolute filepath.
104  BOOST_TEST(cet::is_absolute_filepath(fullPath1));
105  BOOST_TEST(cet::is_absolute_filepath(fullPath2));
106  }
107 
108  // Check that we cannot access a file in the current source
109  // directory without specifying the full path...
110  check_exception(maker, file_in_current_dir);
111  // ...but we can access it through its full path.
112  maker(current_dir() + '/' + file_in_current_dir);
113 }
114 
115 BOOST_AUTO_TEST_CASE(filepath_lookup_after1_t1)
116 {
118  // For the first file, the maker returns whatever you give it.
119  // There is no checking to see if the file actually exists.
120  auto const file = maker("a.txt");
121  BOOST_TEST(!cet::file_exists(file));
122 }
123 
124 BOOST_AUTO_TEST_CASE(filepath_lookup_after1_t2)
125 {
127  auto const absolute_path = current_dir() + '/' + file_in_current_dir;
128  // Absolute allowed for first file
129  maker(absolute_path);
130  // ... not allowed for second file
131  check_exception(maker, absolute_path);
132 
133  // File relative to path allowed for second file
134  maker("a.txt");
135  // Absolute path not allowed for second file
136  check_exception(maker, current_nested_dir() + "/a.txt");
137 }
138 
139 BOOST_AUTO_TEST_CASE(filepath_lookup_after1_t3)
140 {
142  auto const relative_path = "./" + file_in_current_dir;
143  // Dot-prefixed file allowed for first file
144  maker(relative_path);
145  // ... not allowed for second file
146  check_exception(maker, relative_path);
147 
148  // File relative to path allowed for second file
149  maker("a.txt");
150  // Absolute path not allowed for second file
151  check_exception(maker, current_nested_dir() + "/a.txt");
152 }
153 
154 BOOST_AUTO_TEST_CASE(filepath_lookup_after1_t4)
155 {
157  // Relative file allowed for first file
158  maker(file_in_current_dir);
159  // ... not allowed for second file
160  check_exception(maker, file_in_current_dir);
161 
162  // File relative to path allowed for second file
163  maker("a.txt");
164  // Absolute path not allowed for second file
165  check_exception(maker, current_nested_dir() + "/a.txt");
166 }
167 
168 BOOST_AUTO_TEST_CASE(filepath_first_absolute_or_lookup_with_dot_t1)
169 {
170  // Must provide value of environment variable for this policy, not
171  // its name.
173  // The first file is permitted to be relative the path.
174  auto const file = maker("a.txt");
175  BOOST_TEST(cet::file_exists(file));
176 }
177 
178 BOOST_AUTO_TEST_CASE(filepath_first_absolute_or_lookup_with_dot_t2)
179 {
181  auto const absolute_path = current_dir() + '/' + file_in_current_dir;
182  // Absolute allowed for first file
183  maker(absolute_path);
184  // ... not allowed for second file
185  check_exception(maker, absolute_path);
186 
187  // File relative to path allowed for second file
188  maker("a.txt");
189  // Absolute path not allowed for second file
190  check_exception(maker, current_nested_dir() + "/a.txt");
191 }
192 
193 BOOST_AUTO_TEST_CASE(filepath_first_absolute_or_lookup_with_dot_t3)
194 {
196  auto const relative_path = "./" + file_in_current_dir;
197  // Dot-prefixed file allowed for first file
198  maker(relative_path);
199  // ... not allowed for second file
200  check_exception(maker, relative_path);
201 
202  // File relative to path allowed for second file
203  maker("a.txt");
204  // Absolute path not allowed for second file
205  check_exception(maker, current_nested_dir() + "/a.txt");
206 }
207 
208 BOOST_AUTO_TEST_CASE(filepath_first_absolute_or_lookup_with_dot_t4)
209 {
211  // Relative file allowed for first file
212  maker(file_in_current_dir);
213  // ... not allowed for second file
214  check_exception(maker, file_in_current_dir);
215 
216  // File relative to path allowed for second file
217  maker("a.txt");
218  // Absolute path not allowed for second file
219  check_exception(maker, current_nested_dir() + "/a.txt");
220 }
221 
222 BOOST_AUTO_TEST_SUITE_END()
std::string string
Definition: nybbler.cc:12
string filename
Definition: train.py:213
list files
Definition: languages.py:9
const double e
std::string getenv(std::string const &name)
Definition: getenv.cc:15
BOOST_AUTO_TEST_CASE(filepath_maker_t)
bool is_absolute_filepath(std::string const &qualified_filename)
Definition: filesystem.cc:23
string filepath
Definition: train.py:371
bool file_exists(std::string const &qualified_filename)
Definition: filesystem.cc:14
def maker(G, ac, typename)
Definition: apa.py:280
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33