Style fixes for folly/experimental/gdb/README.md
[folly.git] / folly / experimental / TestUtil.h
index f5fb1638dc2b994c2ef32e5cc0a67e81d55b6515..b6d2ad745947eb666dee12fa3ad00ff52f4fae1f 100644 (file)
@@ -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,7 +18,9 @@
 
 #include <map>
 #include <string>
+
 #include <folly/Range.h>
+#include <folly/ScopeGuard.h>
 #include <folly/experimental/io/FsUtil.h>
 
 namespace folly {
@@ -52,6 +54,7 @@ class TemporaryFile {
   TemporaryFile(TemporaryFile&&) = default;
   TemporaryFile& operator=(TemporaryFile&&) = default;
 
+  void close();
   int fd() const { return fd_; }
   const fs::path& path() const;
 
@@ -89,11 +92,13 @@ class TemporaryDirectory {
   TemporaryDirectory(TemporaryDirectory&&) = default;
   TemporaryDirectory& operator=(TemporaryDirectory&&) = default;
 
-  const fs::path& path() const { return path_; }
+  const fs::path& path() const {
+    return *path_;
+  }
 
  private:
   Scope scope_;
-  fs::path path_;
+  std::unique_ptr<fs::path> path_;
 };
 
 /**
@@ -101,7 +106,7 @@ class TemporaryDirectory {
  * upon destruction, also changing back to the original working directory.
  */
 class ChangeToTempDir {
-public:
+ public:
   ChangeToTempDir();
   ~ChangeToTempDir();
 
@@ -111,11 +116,38 @@ public:
 
   const fs::path& path() const { return dir_.path(); }
 
-private:
+ private:
   fs::path initialPath_;
   TemporaryDirectory dir_;
 };
 
+namespace detail {
+struct SavedState {
+  void* previousThreadLocalHandler;
+  int previousCrtReportMode;
+};
+SavedState disableInvalidParameters();
+void enableInvalidParameters(SavedState state);
+}
+
+// Ok, so fun fact: The CRT on windows will actually abort
+// on certain failed parameter validation checks in debug
+// mode rather than simply returning -1 as it does in release
+// mode. We can however, ensure consistent behavior by
+// registering our own thread-local invalid parameter handler
+// for the duration of the call, and just have that handler
+// immediately return. We also have to disable CRT asertion
+// alerts for the duration of the call, otherwise we get
+// the abort-retry-ignore window.
+template <typename Func>
+auto msvcSuppressAbortOnInvalidParams(Func func) -> decltype(func()) {
+  auto savedState = detail::disableInvalidParameters();
+  SCOPE_EXIT {
+    detail::enableInvalidParameters(savedState);
+  };
+  return func();
+}
+
 /**
  * Easy PCRE regex matching. Note that pattern must match the ENTIRE target,
  * so use .* at the start and end of the pattern, as appropriate.  See
@@ -135,9 +167,9 @@ private:
   )
 
 namespace detail {
-  bool hasPCREPatternMatch(StringPiece pattern, StringPiece target);
-  bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target);
-}  // namespace detail
+bool hasPCREPatternMatch(StringPiece pattern, StringPiece target);
+bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target);
+} // namespace detail
 
 /**
  * Use these patterns together with CaptureFD and EXPECT_PCRE_MATCH() to
@@ -161,9 +193,10 @@ inline std::string glogErrOrWarnPattern() { return ".*(^|\n)[EW][0-9].*"; }
  * Great for testing logging (see also glog*Pattern()).
  */
 class CaptureFD {
-private:
+ private:
   struct NoOpChunkCob { void operator()(StringPiece) {} };
-public:
+
+ public:
   using ChunkCob = std::function<void(folly::StringPiece)>;
 
   /**
@@ -192,7 +225,7 @@ public:
    */
   std::string readIncremental();
 
-private:
+ private:
   ChunkCob chunkCob_;
   TemporaryFile file_;
 
@@ -202,13 +235,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
+} // namespace test
+} // namespace folly