From 50ef8adbfe77a88ca5005a2cefe76e91ea694612 Mon Sep 17 00:00:00 2001 From: Ryan Timmons <119094+rtimmons@users.noreply.github.com> Date: Mon, 18 Mar 2019 16:00:18 -0400 Subject: [PATCH] TIG-1209: Add findRepoRoot to assist with loading test fixtures (#184) --- .genny-root | 2 + CMakeLists.txt | 1 + src/testlib/CMakeLists.txt | 1 + src/testlib/include/testlib/findRepoRoot.hpp | 30 +++++++++++ src/testlib/src/findRepoRoot.cpp | 53 ++++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 .genny-root create mode 100644 src/testlib/include/testlib/findRepoRoot.hpp create mode 100644 src/testlib/src/findRepoRoot.cpp diff --git a/.genny-root b/.genny-root new file mode 100644 index 0000000000..75db5d0e2a --- /dev/null +++ b/.genny-root @@ -0,0 +1,2 @@ +This is a "dummy" file used by `findRepoRoot` to find the "root" of the Genny +repository by successively looking up the directory tree from cwd. diff --git a/CMakeLists.txt b/CMakeLists.txt index 700596abd1..2792dc4153 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ find_package( 1.68 REQUIRED COMPONENTS + filesystem log_setup log program_options diff --git a/src/testlib/CMakeLists.txt b/src/testlib/CMakeLists.txt index 9a7d915e4f..e9e3961563 100644 --- a/src/testlib/CMakeLists.txt +++ b/src/testlib/CMakeLists.txt @@ -22,6 +22,7 @@ CreateGennyTargets( MongoCxx::mongocxx MongoCxx::bsoncxx Boost::boost + Boost::filesystem Boost::log catch2 yaml-cpp diff --git a/src/testlib/include/testlib/findRepoRoot.hpp b/src/testlib/include/testlib/findRepoRoot.hpp new file mode 100644 index 0000000000..b0639fd774 --- /dev/null +++ b/src/testlib/include/testlib/findRepoRoot.hpp @@ -0,0 +1,30 @@ +// Copyright 2019-present MongoDB 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. + +#ifndef HEADER_00E808D9_C909_4A88_99A5_C0B5E8596E84_INCLUDED +#define HEADER_00E808D9_C909_4A88_99A5_C0B5E8596E84_INCLUDED + +#include + +namespace genny { + +/** + * @return the path to the Genny repository's root directory by looking 'up' the directory tree. + * @throws invalid_argument if not inside the genny repository + */ +std::string findRepoRoot(); + +} // namespace genny + +#endif // HEADER_00E808D9_C909_4A88_99A5_C0B5E8596E84_INCLUDED diff --git a/src/testlib/src/findRepoRoot.cpp b/src/testlib/src/findRepoRoot.cpp new file mode 100644 index 0000000000..a0337aa6bd --- /dev/null +++ b/src/testlib/src/findRepoRoot.cpp @@ -0,0 +1,53 @@ +// Copyright 2019-present MongoDB 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 + +#include +#include + +namespace genny { + +namespace { + +constexpr auto ROOT_FILE = ".genny-root"; + +} // namespace + + +// +// Note this function doesn't have any automated testing. Be careful when changing. +// +std::string findRepoRoot() { + using namespace boost::filesystem; + + auto fileSystemRoot = path("/"); + + auto curr = current_path(); + const auto start = curr; + + while (!exists(curr / ROOT_FILE)) { + curr = curr / ".."; + if (curr == fileSystemRoot) { + std::stringstream msg; + msg << "Cannot find '" << ROOT_FILE << "' in '" << start << "'"; + throw std::invalid_argument(msg.str()); + } + } + + return curr.string(); +} + + +} // namespace genny