Split up experimental/TestUtil
authorMichael Lee <mzlee@fb.com>
Mon, 30 Jan 2017 20:03:04 +0000 (12:03 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 30 Jan 2017 20:18:13 +0000 (12:18 -0800)
Summary: TestUtil is primarily a temporary file and temporary directory.  Split out the env variable manipulation into a different file.

Reviewed By: Orvid

Differential Revision: D4482071

fbshipit-source-id: 9d1a4a08010a8fad270aa56a7e1432829eb6484c

folly/Makefile.am
folly/experimental/EnvUtil.cpp [new file with mode: 0644]
folly/experimental/EnvUtil.h [new file with mode: 0644]
folly/experimental/TestUtil.cpp
folly/experimental/TestUtil.h
folly/experimental/test/EnvUtilTest.cpp [new file with mode: 0644]
folly/experimental/test/TestUtilTest.cpp

index b879b2fabf7952da1e926896462fd0219855b881..6bd6dca6c0ba72e870be5505ac4193643c92dbb1 100644 (file)
@@ -102,6 +102,7 @@ nobase_follyinclude_HEADERS = \
        experimental/DynamicParser-inl.h \
        experimental/ExecutionObserver.h \
        experimental/EliasFanoCoding.h \
+       experimental/EnvUtil.h \
        experimental/EventCount.h \
        experimental/Instructions.h \
        experimental/bser/Bser.h \
@@ -514,6 +515,7 @@ libfolly_la_SOURCES = \
        experimental/bser/Dump.cpp \
        experimental/bser/Load.cpp \
        experimental/DynamicParser.cpp \
+       experimental/EnvUtil.cpp
        experimental/FunctionScheduler.cpp \
        experimental/io/FsUtil.cpp \
        experimental/JemallocNodumpAllocator.cpp \
diff --git a/folly/experimental/EnvUtil.cpp b/folly/experimental/EnvUtil.cpp
new file mode 100644 (file)
index 0000000..f31bd37
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2017 Facebook, 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 <folly/experimental/EnvUtil.h>
+
+#include <folly/String.h>
+#include <folly/portability/Stdlib.h>
+#include <folly/portability/Unistd.h>
+
+namespace folly {
+namespace test {
+
+static std::map<std::string, std::string> getEnvVarMap() {
+  std::map<std::string, std::string> data;
+  for (auto it = environ; *it != nullptr; ++it) {
+    std::string key, value;
+    split("=", *it, key, value);
+    if (key.empty()) {
+      continue;
+    }
+    CHECK(!data.count(key)) << "already contains: " << key;
+    data.emplace(move(key), move(value));
+  }
+  return data;
+}
+
+EnvVarSaver::EnvVarSaver() {
+  saved_ = getEnvVarMap();
+}
+
+EnvVarSaver::~EnvVarSaver() {
+  for (const auto& kvp : getEnvVarMap()) {
+    if (saved_.count(kvp.first)) {
+      continue;
+    }
+    PCHECK(0 == unsetenv(kvp.first.c_str()));
+  }
+  for (const auto& kvp : saved_) {
+    PCHECK(0 == setenv(kvp.first.c_str(), kvp.second.c_str(), (int)true));
+  }
+}
+}
+}
diff --git a/folly/experimental/EnvUtil.h b/folly/experimental/EnvUtil.h
new file mode 100644 (file)
index 0000000..a44e07b
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2017 Facebook, 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.
+ */
+
+#pragma once
+
+#include <map>
+#include <string>
+
+namespace folly {
+namespace test {
+
+class EnvVarSaver {
+ public:
+  EnvVarSaver();
+  ~EnvVarSaver();
+
+ private:
+  std::map<std::string, std::string> saved_;
+};
+}
+}
index 100ed1ae28e102ec031b849bb38b2abcc081dfaf..afbf25a7a36321c68fa132cf76ea01cf9b1af28c 100644 (file)
 #include <sys/stat.h>
 
 #include <boost/regex.hpp>
-#include <folly/Conv.h>
 #include <folly/Exception.h>
 #include <folly/File.h>
 #include <folly/FileUtil.h>
 #include <folly/Memory.h>
 #include <folly/String.h>
 #include <folly/portability/Fcntl.h>
-#include <folly/portability/Stdlib.h>
-#include <folly/portability/Unistd.h>
 
 #ifdef _WIN32
 #include <crtdbg.h>
@@ -217,35 +214,5 @@ std::string CaptureFD::readIncremental() {
   return std::string(buf.get(), size);
 }
 
-static std::map<std::string, std::string> getEnvVarMap() {
-  std::map<std::string, std::string> data;
-  for (auto it = environ; *it != nullptr; ++it) {
-    std::string key, value;
-    split("=", *it, key, value);
-    if (key.empty()) {
-      continue;
-    }
-    CHECK(!data.count(key)) << "already contains: " << key;
-    data.emplace(move(key), move(value));
-  }
-  return data;
-}
-
-EnvVarSaver::EnvVarSaver() {
-  saved_ = getEnvVarMap();
-}
-
-EnvVarSaver::~EnvVarSaver() {
-  for (const auto& kvp : getEnvVarMap()) {
-    if (saved_.count(kvp.first)) {
-      continue;
-    }
-    PCHECK(0 == unsetenv(kvp.first.c_str()));
-  }
-  for (const auto& kvp : saved_) {
-    PCHECK(0 == setenv(kvp.first.c_str(), kvp.second.c_str(), (int)true));
-  }
-}
-
 }  // namespace test
 }  // namespace folly
index 42ac8894343672743bb6cdec1f7c2af957cee542..1cc9f190799b28f6c9ead27e59cd760cb42a27a8 100644 (file)
@@ -232,13 +232,5 @@ private:
   off_t readOffset_;  // for incremental reading
 };
 
-class EnvVarSaver {
-public:
-  EnvVarSaver();
-  ~EnvVarSaver();
-private:
-  std::map<std::string, std::string> saved_;
-};
-
 }  // namespace test
 }  // namespace folly
diff --git a/folly/experimental/test/EnvUtilTest.cpp b/folly/experimental/test/EnvUtilTest.cpp
new file mode 100644 (file)
index 0000000..2265e51
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2017 Facebook, 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 <folly/experimental/EnvUtil.h>
+
+#include <system_error>
+
+#include <boost/algorithm/string.hpp>
+#include <glog/logging.h>
+
+#include <folly/Memory.h>
+#include <folly/portability/Fcntl.h>
+#include <folly/portability/GTest.h>
+#include <folly/portability/Stdlib.h>
+
+using namespace folly;
+using namespace folly::test;
+
+class EnvVarSaverTest : public testing::Test {};
+
+TEST_F(EnvVarSaverTest, ExampleNew) {
+  auto key = "hahahahaha";
+  EXPECT_EQ(nullptr, getenv(key));
+
+  PCHECK(0 == setenv(key, "", true));
+  EXPECT_STREQ("", getenv(key));
+  PCHECK(0 == unsetenv(key));
+  EXPECT_EQ(nullptr, getenv(key));
+
+  auto saver = make_unique<EnvVarSaver>();
+  PCHECK(0 == setenv(key, "blah", true));
+  EXPECT_EQ("blah", std::string{getenv(key)});
+  saver = nullptr;
+  EXPECT_EQ(nullptr, getenv(key));
+}
+
+TEST_F(EnvVarSaverTest, ExampleExisting) {
+  auto key = "PATH";
+  EXPECT_NE(nullptr, getenv(key));
+  auto value = std::string{getenv(key)};
+
+  auto saver = make_unique<EnvVarSaver>();
+  PCHECK(0 == setenv(key, "blah", true));
+  EXPECT_EQ("blah", std::string{getenv(key)});
+  saver = nullptr;
+  EXPECT_TRUE(value == getenv(key));
+}
+
+TEST_F(EnvVarSaverTest, ExampleDeleting) {
+  auto key = "PATH";
+  EXPECT_NE(nullptr, getenv(key));
+  auto value = std::string{getenv(key)};
+
+  auto saver = make_unique<EnvVarSaver>();
+  PCHECK(0 == unsetenv(key));
+  EXPECT_EQ(nullptr, getenv(key));
+  saver = nullptr;
+  EXPECT_TRUE(value == getenv(key));
+}
index f351fc52d62611c4df6439b2b01fa213bd10bcb2..cbae58154febf618c304f4813dc7c29b2607ad94 100644 (file)
@@ -187,52 +187,3 @@ TEST(CaptureFD, ChunkCob) {
   }
   EXPECT_EQ(2, chunks.size());
 }
-
-
-class EnvVarSaverTest : public testing::Test {};
-
-TEST_F(EnvVarSaverTest, ExampleNew) {
-  auto key = "hahahahaha";
-  EXPECT_EQ(nullptr, getenv(key));
-
-  PCHECK(0 == setenv(key, "", true));
-  EXPECT_STREQ("", getenv(key));
-  PCHECK(0 == unsetenv(key));
-  EXPECT_EQ(nullptr, getenv(key));
-
-  auto saver = make_unique<EnvVarSaver>();
-  PCHECK(0 == setenv(key, "blah", true));
-  EXPECT_EQ("blah", std::string{getenv(key)});
-  saver = nullptr;
-  EXPECT_EQ(nullptr, getenv(key));
-}
-
-TEST_F(EnvVarSaverTest, ExampleExisting) {
-  auto key = "PATH";
-  EXPECT_NE(nullptr, getenv(key));
-  auto value = std::string{getenv(key)};
-
-  auto saver = make_unique<EnvVarSaver>();
-  PCHECK(0 == setenv(key, "blah", true));
-  EXPECT_EQ("blah", std::string{getenv(key)});
-  saver = nullptr;
-  EXPECT_TRUE(value == getenv(key));
-}
-
-TEST_F(EnvVarSaverTest, ExampleDeleting) {
-  auto key = "PATH";
-  EXPECT_NE(nullptr, getenv(key));
-  auto value = std::string{getenv(key)};
-
-  auto saver = make_unique<EnvVarSaver>();
-  PCHECK(0 == unsetenv(key));
-  EXPECT_EQ(nullptr, getenv(key));
-  saver = nullptr;
-  EXPECT_TRUE(value == getenv(key));
-}
-
-int main(int argc, char *argv[]) {
-  testing::InitGoogleTest(&argc, argv);
-  gflags::ParseCommandLineFlags(&argc, &argv, true);
-  return RUN_ALL_TESTS();
-}