From: Michael Lee Date: Mon, 30 Jan 2017 20:03:04 +0000 (-0800) Subject: Split up experimental/TestUtil X-Git-Tag: v2017.03.06.00~70 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=ceac4e9ef26fdad9bb3259c2d3702051a331d193;p=folly.git Split up experimental/TestUtil 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 --- diff --git a/folly/Makefile.am b/folly/Makefile.am index b879b2fa..6bd6dca6 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -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 index 00000000..f31bd37d --- /dev/null +++ b/folly/experimental/EnvUtil.cpp @@ -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 + +#include +#include +#include + +namespace folly { +namespace test { + +static std::map getEnvVarMap() { + std::map 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 index 00000000..a44e07bc --- /dev/null +++ b/folly/experimental/EnvUtil.h @@ -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 +#include + +namespace folly { +namespace test { + +class EnvVarSaver { + public: + EnvVarSaver(); + ~EnvVarSaver(); + + private: + std::map saved_; +}; +} +} diff --git a/folly/experimental/TestUtil.cpp b/folly/experimental/TestUtil.cpp index 100ed1ae..afbf25a7 100644 --- a/folly/experimental/TestUtil.cpp +++ b/folly/experimental/TestUtil.cpp @@ -20,15 +20,12 @@ #include #include -#include #include #include #include #include #include #include -#include -#include #ifdef _WIN32 #include @@ -217,35 +214,5 @@ std::string CaptureFD::readIncremental() { return std::string(buf.get(), size); } -static std::map getEnvVarMap() { - std::map 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 diff --git a/folly/experimental/TestUtil.h b/folly/experimental/TestUtil.h index 42ac8894..1cc9f190 100644 --- a/folly/experimental/TestUtil.h +++ b/folly/experimental/TestUtil.h @@ -232,13 +232,5 @@ private: off_t readOffset_; // for incremental reading }; -class EnvVarSaver { -public: - EnvVarSaver(); - ~EnvVarSaver(); -private: - std::map saved_; -}; - } // namespace test } // namespace folly diff --git a/folly/experimental/test/EnvUtilTest.cpp b/folly/experimental/test/EnvUtilTest.cpp new file mode 100644 index 00000000..2265e514 --- /dev/null +++ b/folly/experimental/test/EnvUtilTest.cpp @@ -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 + +#include + +#include +#include + +#include +#include +#include +#include + +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(); + 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(); + 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(); + PCHECK(0 == unsetenv(key)); + EXPECT_EQ(nullptr, getenv(key)); + saver = nullptr; + EXPECT_TRUE(value == getenv(key)); +} diff --git a/folly/experimental/test/TestUtilTest.cpp b/folly/experimental/test/TestUtilTest.cpp index f351fc52..cbae5815 100644 --- a/folly/experimental/test/TestUtilTest.cpp +++ b/folly/experimental/test/TestUtilTest.cpp @@ -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(); - 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(); - 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(); - 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(); -}