X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Fexperimental%2FTestUtil.cpp;h=afbf25a7a36321c68fa132cf76ea01cf9b1af28c;hb=4fe88e4c921168ffbc53ccfa5210d9b9721d9f29;hp=b9c68ad1bb5a458728c3c620bf9cbeed3d6f8da4;hpb=321542683a01c3f334047531e9b487f047129775;p=folly.git diff --git a/folly/experimental/TestUtil.cpp b/folly/experimental/TestUtil.cpp index b9c68ad1..afbf25a7 100644 --- a/folly/experimental/TestUtil.cpp +++ b/folly/experimental/TestUtil.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * 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. @@ -18,18 +18,17 @@ #include #include -#include -#include #include -#include #include #include #include +#include #include +#include -#ifndef _MSC_VER -extern char** environ; +#ifdef _WIN32 +#include #endif namespace folly { @@ -98,18 +97,20 @@ TemporaryFile::~TemporaryFile() { } } -TemporaryDirectory::TemporaryDirectory(StringPiece namePrefix, - fs::path dir, - Scope scope) - : scope_(scope), - path_(generateUniquePath(std::move(dir), namePrefix)) { - fs::create_directory(path_); +TemporaryDirectory::TemporaryDirectory( + StringPiece namePrefix, + fs::path dir, + Scope scope) + : scope_(scope), + path_(folly::make_unique( + generateUniquePath(std::move(dir), namePrefix))) { + fs::create_directory(path()); } TemporaryDirectory::~TemporaryDirectory() { - if (scope_ == Scope::DELETE_ON_DESTRUCTION) { + if (scope_ == Scope::DELETE_ON_DESTRUCTION && path_ != nullptr) { boost::system::error_code ec; - fs::remove_all(path_, ec); + fs::remove_all(path(), ec); if (ec) { LOG(WARNING) << "recursive delete on destruction failed: " << ec; } @@ -128,6 +129,32 @@ ChangeToTempDir::~ChangeToTempDir() { namespace detail { +SavedState disableInvalidParameters() { +#ifdef _WIN32 + SavedState ret; + ret.previousThreadLocalHandler = _set_thread_local_invalid_parameter_handler( + [](const wchar_t*, + const wchar_t*, + const wchar_t*, + unsigned int, + uintptr_t) {}); + ret.previousCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0); + return ret; +#else + return SavedState(); +#endif +} + +#ifdef _WIN32 +void enableInvalidParameters(SavedState state) { + _set_thread_local_invalid_parameter_handler( + (_invalid_parameter_handler)state.previousThreadLocalHandler); + _CrtSetReportMode(_CRT_ASSERT, state.previousCrtReportMode); +} +#else +void enableInvalidParameters(SavedState) {} +#endif + bool hasPCREPatternMatch(StringPiece pattern, StringPiece target) { return boost::regex_match( target.begin(), @@ -178,44 +205,14 @@ std::string CaptureFD::readIncremental() { std::string filename = file_.path().string(); // Yes, I know that I could just keep the file open instead. So sue me. folly::File f(openNoInt(filename.c_str(), O_RDONLY), true); - auto size = lseek(f.fd(), 0, SEEK_END) - readOffset_; + auto size = size_t(lseek(f.fd(), 0, SEEK_END) - readOffset_); std::unique_ptr buf(new char[size]); auto bytes_read = folly::preadFull(f.fd(), buf.get(), size, readOffset_); - PCHECK(size == bytes_read); - readOffset_ += size; + PCHECK(ssize_t(size) == bytes_read); + readOffset_ += off_t(size); chunkCob_(StringPiece(buf.get(), buf.get() + size)); 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