Commit ceac4e9e authored by Michael Lee's avatar Michael Lee Committed by Facebook Github Bot

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
parent 0637780a
...@@ -102,6 +102,7 @@ nobase_follyinclude_HEADERS = \ ...@@ -102,6 +102,7 @@ nobase_follyinclude_HEADERS = \
experimental/DynamicParser-inl.h \ experimental/DynamicParser-inl.h \
experimental/ExecutionObserver.h \ experimental/ExecutionObserver.h \
experimental/EliasFanoCoding.h \ experimental/EliasFanoCoding.h \
experimental/EnvUtil.h \
experimental/EventCount.h \ experimental/EventCount.h \
experimental/Instructions.h \ experimental/Instructions.h \
experimental/bser/Bser.h \ experimental/bser/Bser.h \
...@@ -514,6 +515,7 @@ libfolly_la_SOURCES = \ ...@@ -514,6 +515,7 @@ libfolly_la_SOURCES = \
experimental/bser/Dump.cpp \ experimental/bser/Dump.cpp \
experimental/bser/Load.cpp \ experimental/bser/Load.cpp \
experimental/DynamicParser.cpp \ experimental/DynamicParser.cpp \
experimental/EnvUtil.cpp
experimental/FunctionScheduler.cpp \ experimental/FunctionScheduler.cpp \
experimental/io/FsUtil.cpp \ experimental/io/FsUtil.cpp \
experimental/JemallocNodumpAllocator.cpp \ experimental/JemallocNodumpAllocator.cpp \
......
/*
* 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));
}
}
}
}
/*
* 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_;
};
}
}
...@@ -20,15 +20,12 @@ ...@@ -20,15 +20,12 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <folly/Conv.h>
#include <folly/Exception.h> #include <folly/Exception.h>
#include <folly/File.h> #include <folly/File.h>
#include <folly/FileUtil.h> #include <folly/FileUtil.h>
#include <folly/Memory.h> #include <folly/Memory.h>
#include <folly/String.h> #include <folly/String.h>
#include <folly/portability/Fcntl.h> #include <folly/portability/Fcntl.h>
#include <folly/portability/Stdlib.h>
#include <folly/portability/Unistd.h>
#ifdef _WIN32 #ifdef _WIN32
#include <crtdbg.h> #include <crtdbg.h>
...@@ -217,35 +214,5 @@ std::string CaptureFD::readIncremental() { ...@@ -217,35 +214,5 @@ std::string CaptureFD::readIncremental() {
return std::string(buf.get(), size); 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 test
} // namespace folly } // namespace folly
...@@ -232,13 +232,5 @@ private: ...@@ -232,13 +232,5 @@ private:
off_t readOffset_; // for incremental reading off_t readOffset_; // for incremental reading
}; };
class EnvVarSaver {
public:
EnvVarSaver();
~EnvVarSaver();
private:
std::map<std::string, std::string> saved_;
};
} // namespace test } // namespace test
} // namespace folly } // namespace folly
/*
* 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));
}
...@@ -187,52 +187,3 @@ TEST(CaptureFD, ChunkCob) { ...@@ -187,52 +187,3 @@ TEST(CaptureFD, ChunkCob) {
} }
EXPECT_EQ(2, chunks.size()); 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();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment