Repository: mesos
Updated Branches:
  refs/heads/master 12a01d925 -> ee3b04bc9


Refactored test filter handling in stout.

This refactoring will make it easier to create test filters in
libprocess and Mesos. Furthermore the code has been updated to follow
stout's coding style.

Review: https://reviews.apache.org/r/58661/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/bca95cab
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/bca95cab
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/bca95cab

Branch: refs/heads/master
Commit: bca95cabbfb07cc6293bf499f566244ea3bb0a94
Parents: 12a01d9
Author: Jan Schlicht <[email protected]>
Authored: Mon Apr 24 11:15:12 2017 -0400
Committer: Neil Conway <[email protected]>
Committed: Mon Apr 24 11:47:22 2017 -0400

----------------------------------------------------------------------
 .../stout/include/stout/tests/environment.hpp   | 119 +++++--------------
 3rdparty/stout/tests/main.cpp                   |  53 ++++++++-
 2 files changed, 80 insertions(+), 92 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/bca95cab/3rdparty/stout/include/stout/tests/environment.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/tests/environment.hpp 
b/3rdparty/stout/include/stout/tests/environment.hpp
index 61dfe0d..211f1fb 100644
--- a/3rdparty/stout/include/stout/tests/environment.hpp
+++ b/3rdparty/stout/include/stout/tests/environment.hpp
@@ -17,47 +17,27 @@
 #ifndef __STOUT_TESTS_ENVIRONMENT_HPP__
 #define __STOUT_TESTS_ENVIRONMENT_HPP__
 
-#ifndef __WINDOWS__
-#include <sys/wait.h>
-
-#include <unistd.h>
-#endif // __WINDOWS__
-
+#include <memory>
 #include <string>
 #include <vector>
 
 #include <gtest/gtest.h>
 
-#include <stout/check.hpp>
-#include <stout/error.hpp>
-#include <stout/fs.hpp>
-#include <stout/option.hpp>
-#include <stout/os.hpp>
-#include <stout/path.hpp>
 #include <stout/strings.hpp>
-#include <stout/try.hpp>
-
-#include <stout/os/temp.hpp>
-#include <stout/os/touch.hpp>
-
-using std::string;
-using std::vector;
-
-namespace stout {
-namespace internal {
-namespace tests {
 
 class TestFilter
 {
 public:
-  TestFilter() {}
-  virtual ~TestFilter() {}
+  TestFilter() = default;
+  virtual ~TestFilter() = default;
 
   // Returns true iff the test should be disabled.
   virtual bool disable(const ::testing::TestInfo* test) const = 0;
 
   // Returns whether the test name or parameterization matches the pattern.
-  static bool matches(const ::testing::TestInfo* test, const string& pattern)
+  static bool matches(
+      const ::testing::TestInfo* test,
+      const std::string& pattern)
   {
     if (strings::contains(test->test_case_name(), pattern) ||
         strings::contains(test->name(), pattern)) {
@@ -71,49 +51,13 @@ public:
   }
 };
 
-// Attempt to create a symlink. If not possible, disable all unit tests
-// that rely on the creation of symlinks.
-class SymlinkFilter : public TestFilter
-{
-public:
-  SymlinkFilter()
-  {
-    const Try<string> temp_path = os::mkdtemp();
-    CHECK_SOME(temp_path);
-
-    const string file = path::join(temp_path.get(), "file");
-    const string link = path::join(temp_path.get(), "link");
-
-    CHECK_SOME(os::touch(file));
-
-    Try<Nothing> symlinkCheck = fs::symlink(file, link);
-    canCreateSymlinks = !symlinkCheck.isError();
-
-    if (!canCreateSymlinks) {
-      std::cerr
-        << "-------------------------------------------------------------\n"
-        << "Not able to create Symlinks, so no symlink tests will be run\n"
-        << "-------------------------------------------------------------"
-        << std::endl;
-    }
-  }
-
-  bool disable(const ::testing::TestInfo* test) const
-  {
-    return matches(test, "SYMLINK_") && !canCreateSymlinks;
-  }
-
-private:
-  bool canCreateSymlinks;
-};
-
 
 // Return list of disabled tests based on test name based filters.
-static vector<string> disabled(
+static std::vector<std::string> disabled(
     const ::testing::UnitTest* unitTest,
-    const vector<std::shared_ptr<TestFilter>>& filters)
+    const std::vector<std::shared_ptr<TestFilter>>& filters)
 {
-  vector<string> disabled;
+  std::vector<std::string> disabled;
 
   for (int i = 0; i < unitTest->total_test_case_count(); i++) {
     const ::testing::TestCase* testCase = unitTest->GetTestCase(i);
@@ -123,7 +67,7 @@ static vector<string> disabled(
       foreach (const std::shared_ptr<TestFilter>& filter, filters) {
         if (filter->disable(test)) {
           disabled.push_back(
-              test->test_case_name() + string(".") + test->name());
+              test->test_case_name() + std::string(".") + test->name());
           break;
         }
       }
@@ -133,8 +77,10 @@ static vector<string> disabled(
   return disabled;
 }
 
+
 // Used to set up and manage the test environment.
-class Environment : public ::testing::Environment {
+class Environment : public ::testing::Environment
+{
 public:
   // We use the constructor to setup specific tests by updating the
   // gtest filter. We do this so that we can selectively run tests that
@@ -142,52 +88,47 @@ public:
   // should not effect any other filters that have been put in place
   // either on the command line or via an environment variable.
   // NOTE: This should be done before invoking `RUN_ALL_TESTS`.
-  Environment(vector<std::shared_ptr<TestFilter>> filters)
+  Environment(const std::vector<std::shared_ptr<TestFilter>>& filters)
   {
     // First we split the current filter into enabled and disabled tests
     // (which are separated by a '-').
-    const string& filter = ::testing::GTEST_FLAG(filter);
+    const std::string& filtered_tests = ::testing::GTEST_FLAG(filter);
 
     // An empty filter indicates no tests should be run.
-    if (filter.empty()) {
+    if (filtered_tests.empty()) {
       return;
     }
 
-    filters.push_back(std::shared_ptr<TestFilter>(new SymlinkFilter()));
-
-    string enabled;
-    string disabled;
+    std::string enabled_tests;
+    std::string disabled_tests;
 
-    size_t dash = filter.find('-');
-    if (dash != string::npos) {
-      enabled = filter.substr(0, dash);
-      disabled = filter.substr(dash + 1);
+    size_t dash = filtered_tests.find('-');
+    if (dash != std::string::npos) {
+      enabled_tests = filtered_tests.substr(0, dash);
+      disabled_tests = filtered_tests.substr(dash + 1);
     } else {
-      enabled = filter;
+      enabled_tests = filtered_tests;
     }
 
     // Use universal filter if not specified.
-    if (enabled.empty()) {
-      enabled = "*";
+    if (enabled_tests.empty()) {
+      enabled_tests = "*";
     }
 
     // Ensure disabled tests end with ":" separator before we add more.
-    if (!disabled.empty() && !strings::endsWith(disabled, ":")) {
-      disabled += ":";
+    if (!disabled_tests.empty() && !strings::endsWith(disabled_tests, ":")) {
+      disabled_tests += ":";
     }
 
     // Construct the filter string to handle system or platform specific tests.
     ::testing::UnitTest* unitTest = ::testing::UnitTest::GetInstance();
 
-    disabled += strings::join(":", tests::disabled(unitTest, filters));
+    disabled_tests += strings::join(":", disabled(unitTest, filters));
 
     // Now update the gtest flag.
-    ::testing::GTEST_FLAG(filter) = enabled + "-" + disabled;
+    ::testing::GTEST_FLAG(filter) =
+      enabled_tests + "-" + disabled_tests;
   }
 };
 
-} // namespace tests {
-} // namespace internal {
-} // namespace stout {
-
 #endif // __STOUT_TESTS_ENVIRONMENT_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/bca95cab/3rdparty/stout/tests/main.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/main.cpp b/3rdparty/stout/tests/main.cpp
index fbbe839..1a4acab 100644
--- a/3rdparty/stout/tests/main.cpp
+++ b/3rdparty/stout/tests/main.cpp
@@ -10,20 +10,67 @@
 // See the License for the specific language governing permissions and
 // limitations under the License
 
+#include <memory>
+#include <string>
+#include <vector>
+
 #include <glog/logging.h>
 
 #include <gmock/gmock.h>
 
 #include <gtest/gtest.h>
 
+#include <stout/check.hpp>
 #include <stout/exit.hpp>
+#include <stout/fs.hpp>
 
+#include <stout/os/mkdtemp.hpp>
 #include <stout/os/socket.hpp> // For `wsa_*` on Windows.
+#include <stout/os/touch.hpp>
 
 #include <stout/tests/environment.hpp>
 
-using stout::internal::tests::Environment;
-using stout::internal::tests::TestFilter;
+using std::make_shared;
+using std::shared_ptr;
+using std::string;
+using std::vector;
+
+
+// Attempt to create a symlink. If not possible, disable all unit tests
+// that rely on the creation of symlinks.
+class SymlinkFilter : public TestFilter
+{
+public:
+  SymlinkFilter()
+  {
+    const Try<string> temp_path = os::mkdtemp();
+    CHECK_SOME(temp_path);
+
+    const string file = path::join(temp_path.get(), "file");
+    const string link = path::join(temp_path.get(), "link");
+
+    CHECK_SOME(os::touch(file));
+
+    Try<Nothing> symlink_check = fs::symlink(file, link);
+    can_create_symlinks = !symlink_check.isError();
+
+    if (!can_create_symlinks) {
+      std::cerr
+        << "-------------------------------------------------------------\n"
+        << "Not able to create Symlinks, so no symlink tests will be run\n"
+        << "-------------------------------------------------------------"
+        << std::endl;
+    }
+  }
+
+  bool disable(const ::testing::TestInfo* test) const
+  {
+    return matches(test, "SYMLINK_") && !can_create_symlinks;
+  }
+
+private:
+  bool can_create_symlinks;
+};
 
 
 #ifdef __WINDOWS__
@@ -66,7 +113,7 @@ int main(int argc, char** argv)
   _set_invalid_parameter_handler(noop_invalid_parameter_handler);
 #endif // __WINDOWS__
 
-  vector<std::shared_ptr<TestFilter>> filters;
+  vector<shared_ptr<TestFilter>> filters = {make_shared<SymlinkFilter>()};
   Environment* environment = new Environment(filters);
   testing::AddGlobalTestEnvironment(environment);
 

Reply via email to