4 #include <boost/filesystem/operations.hpp> 5 #include <boost/test/unit_test.hpp> 13 struct trivial_getenv {
14 char const* operator()(
char const*) {
19 struct trivial_validator {
20 bool operator()(fs::path
const&) {
29 namespace filesystem {
31 void PrintTo(path
const& p, ::std::ostream* os) {
41 trivial_getenv getenv;
43 trivial t0(fs::path(
"/foo/var/baz/program"),
"TEST_ROOT", getenv);
44 BOOST_CHECK(not t0.search_path().empty());
46 trivial t1(fs::path(
"/foo/var/baz/program"),
"TEST_ROOT");
47 BOOST_CHECK(not t1.search_path().empty());
49 trivial t3(fs::path(
"/foo/var/baz/program"), getenv);
50 BOOST_CHECK(not t3.search_path().empty());
52 trivial t4(fs::path(
"/foo/var/baz/program"));
53 BOOST_CHECK(not t4.search_path().empty());
55 trivial t5(
"/foo/var/baz/program",
"TEST_ROOT", getenv);
56 BOOST_CHECK(not t5.search_path().empty());
58 trivial t6(
"/foo/var/baz/program",
"TEST_ROOT");
59 BOOST_CHECK(not t6.search_path().empty());
61 trivial t7(
"/foo/var/baz/program", getenv);
62 BOOST_CHECK(not t7.search_path().empty());
64 trivial t8(
"/foo/var/baz/program");
65 BOOST_CHECK(not t8.search_path().empty());
69 template <
typename Functor>
70 struct shared_functor {
75 template <
typename... T>
76 auto operator()(T&&... a) -> decltype(Functor().exec(std::forward<T>(a)...)) {
77 return mock->exec(std::forward<T>(a)...);
81 mock = std::shared_ptr<Functor>(
new Functor);
84 std::shared_ptr<Functor> mock;
88 struct mock_getenv_f {
89 MOCK_CONST_METHOD1(exec,
char const*(
char const*));
91 using mock_getenv = shared_functor<mock_getenv_f>;
94 struct mock_validator_f {
95 MOCK_CONST_METHOD1(exec,
bool(fs::path
const&));
97 using mock_validator = shared_functor<mock_validator_f>;
104 mock_getenv& getenv, mock_validator& validator,
char const* test_root,
105 char const* jaybeams_root,
bool valid) {
107 using namespace ::testing;
108 EXPECT_CALL(*getenv.mock, exec(Truly([](
auto arg) {
109 return std::string(
"TEST_ROOT") == arg;
110 }))).WillRepeatedly(Return(test_root));
111 EXPECT_CALL(*getenv.mock, exec(Truly([](
auto arg) {
112 return std::string(
"JAYBEAMS_ROOT") == arg;
113 }))).WillRepeatedly(Return(jaybeams_root));
116 EXPECT_CALL(*validator.mock, exec(_)).WillRepeatedly(Return(valid));
125 mock_validator validator;
126 set_mocks(getenv, validator,
"/test/path",
"/install/path",
true);
128 fs::path programdir = fs::path(
"/foo/var/baz");
129 mocked t(programdir /
"program",
"TEST_ROOT", getenv);
132 std::vector<fs::path> expected({
"/test/path" / etc,
"/install/path" / etc,
134 using namespace ::testing;
135 BOOST_CHECK_EQUAL_COLLECTIONS(
136 t.search_path().begin(), t.search_path().end(), expected.begin(),
146 mock_validator validator;
147 set_mocks(getenv, validator,
"/test/path",
"/install/path",
true);
149 fs::path programdir = fs::path(
"/foo/var/baz");
150 mocked t(programdir /
"program", getenv);
153 std::vector<fs::path> expected(
155 using namespace ::testing;
156 BOOST_CHECK_EQUAL_COLLECTIONS(
157 t.search_path().begin(), t.search_path().end(), expected.begin(),
167 mock_validator validator;
168 set_mocks(getenv, validator,
nullptr,
"/install/path",
true);
170 fs::path programdir = fs::path(
"/foo/var/baz");
171 mocked t(programdir /
"program",
"TEST_ROOT", getenv);
174 std::vector<fs::path> expected(
176 using namespace ::testing;
177 BOOST_CHECK_EQUAL_COLLECTIONS(
178 t.search_path().begin(), t.search_path().end(), expected.begin(),
188 mock_validator validator;
189 set_mocks(getenv, validator,
"/test/path",
nullptr,
true);
191 fs::path programdir = fs::path(
"/foo/var/baz");
192 mocked t(programdir /
"program",
"TEST_ROOT", getenv);
195 std::vector<fs::path> expected(
197 using namespace ::testing;
198 BOOST_CHECK_EQUAL_COLLECTIONS(
199 t.search_path().begin(), t.search_path().end(), expected.begin(),
208 mock_validator validator;
209 set_mocks(getenv, validator,
"/test/path",
"/install/path",
true);
213 fs::path install_path = fs::path(
"/install") /
jb::bindir();
214 fs::path program = install_path /
"program";
216 mocked t(program,
"TEST_ROOT", getenv);
218 std::vector<fs::path> expected({
"/test/path" / etc,
"/install/path" / etc,
220 program.parent_path().parent_path() / etc});
221 using namespace ::testing;
222 BOOST_CHECK_EQUAL_COLLECTIONS(
223 t.search_path().begin(), t.search_path().end(), expected.begin(),
233 mock_validator validator;
234 set_mocks(getenv, validator,
"/test/path",
"/install/path",
true);
236 mocked t(
"program", getenv);
239 std::vector<fs::path> expected({
"/install/path" / etc,
jb::sysconfdir()});
240 using namespace ::testing;
241 BOOST_CHECK_EQUAL_COLLECTIONS(
242 t.search_path().begin(), t.search_path().end(), expected.begin(),
251 mock_validator validator;
252 set_mocks(getenv, validator,
"/test/path",
"/install/path",
true);
256 fs::path install_path = fs::path(
"/install") /
jb::bindir();
257 fs::path program = install_path /
"program";
259 mocked t(program,
"TEST_ROOT", getenv);
262 std::string filename =
"test.yaml";
263 using namespace ::testing;
265 EXPECT_CALL(*validator.mock, exec(_)).WillRepeatedly(Return(
false));
268 t.find_configuration_file(filename, validator), std::runtime_error);
272 for (
auto path : t.search_path()) {
273 EXPECT_CALL(*validator.mock, exec(_))
274 .WillRepeatedly(Invoke(
275 [path, filename](
auto arg) {
return path / filename == arg; }));
276 auto full = path / filename;
277 BOOST_CHECK_EQUAL(full, t.find_configuration_file(filename, validator));
BOOST_AUTO_TEST_CASE(config_files_location_constructors)
char const * bindir()
Return the binary installation directory directory.
void PrintTo(path const &p, ::std::ostream *os)
Initialize GMock to work with Boost.Test.
char const * sysconfdir()
Return the system configuration directory.
Compute the directories where a configuration file can be found.