proxygen
TestUtil.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <map>
20 #include <string>
21 
22 #include <folly/Range.h>
23 #include <folly/ScopeGuard.h>
25 
26 namespace folly {
27 namespace test {
28 
41  public:
42  enum class Scope {
43  PERMANENT,
46  };
47  explicit TemporaryFile(
48  StringPiece namePrefix = StringPiece(),
49  fs::path dir = fs::path(),
51  bool closeOnDestruction = true);
53 
54  // Movable, but not copyable
56  assign(other);
57  }
58 
60  if (this != &other) {
61  reset();
62  assign(other);
63  }
64  return *this;
65  }
66 
67  void close();
68  int fd() const {
69  return fd_;
70  }
71  const fs::path& path() const;
72  void reset();
73 
74  private:
77  int fd_;
78  fs::path path_;
79 
80  void assign(TemporaryFile& other) {
81  scope_ = other.scope_;
82  closeOnDestruction_ = other.closeOnDestruction_;
83  fd_ = std::exchange(other.fd_, -1);
84  path_ = other.path_;
85  }
86 };
87 
101  public:
102  enum class Scope {
103  PERMANENT,
104  DELETE_ON_DESTRUCTION,
105  };
106  explicit TemporaryDirectory(
107  StringPiece namePrefix = StringPiece(),
108  fs::path dir = fs::path(),
109  Scope scope = Scope::DELETE_ON_DESTRUCTION);
111 
112  // Movable, but not copiable
115 
116  const fs::path& path() const {
117  return *path_;
118  }
119 
120  private:
122  std::unique_ptr<fs::path> path_;
123 };
124 
130  public:
131  ChangeToTempDir();
132  ~ChangeToTempDir();
133 
134  // Movable, but not copiable
135  ChangeToTempDir(ChangeToTempDir&&) = default;
137 
138  const fs::path& path() const {
139  return dir_.path();
140  }
141 
142  private:
144  fs::path orig_;
145 };
146 
147 namespace detail {
148 struct SavedState {
151 };
154 } // namespace detail
155 
156 // Ok, so fun fact: The CRT on windows will actually abort
157 // on certain failed parameter validation checks in debug
158 // mode rather than simply returning -1 as it does in release
159 // mode. We can however, ensure consistent behavior by
160 // registering our own thread-local invalid parameter handler
161 // for the duration of the call, and just have that handler
162 // immediately return. We also have to disable CRT asertion
163 // alerts for the duration of the call, otherwise we get
164 // the abort-retry-ignore window.
165 template <typename Func>
166 auto msvcSuppressAbortOnInvalidParams(Func func) -> decltype(func()) {
167  auto savedState = detail::disableInvalidParameters();
168  SCOPE_EXIT {
170  };
171  return func();
172 }
173 
179 #define EXPECT_PCRE_MATCH(pattern_stringpiece, target_stringpiece) \
180  EXPECT_PRED2( \
181  ::folly::test::detail::hasPCREPatternMatch, \
182  pattern_stringpiece, \
183  target_stringpiece)
184 #define EXPECT_NO_PCRE_MATCH(pattern_stringpiece, target_stringpiece) \
185  EXPECT_PRED2( \
186  ::folly::test::detail::hasNoPCREPatternMatch, \
187  pattern_stringpiece, \
188  target_stringpiece)
189 
190 namespace detail {
191 bool hasPCREPatternMatch(StringPiece pattern, StringPiece target);
192 bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target);
193 } // namespace detail
194 
206  return ".*(^|\n)E[0-9].*";
207 }
209  return ".*(^|\n)W[0-9].*";
210 }
211 // Error OR warning
213  return ".*(^|\n)[EW][0-9].*";
214 }
215 
222 class CaptureFD {
223  private:
224  struct NoOpChunkCob {
226  };
227 
228  public:
229  using ChunkCob = std::function<void(folly::StringPiece)>;
230 
236  explicit CaptureFD(int fd, ChunkCob chunk_cob = NoOpChunkCob());
237  ~CaptureFD();
238 
244  void release();
245 
249  std::string read() const;
250 
255  std::string readIncremental();
256 
257  private:
260 
261  int fd_;
262  int oldFDCopy_; // equal to fd_ after restore()
263 
264  off_t readOffset_; // for incremental reading
265 };
266 
267 } // namespace test
268 } // namespace folly
TemporaryDirectory dir_
Definition: TestUtil.h:143
bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target)
Definition: TestUtil.cpp:179
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
auto msvcSuppressAbortOnInvalidParams(Func func) -> decltype(func())
Definition: TestUtil.h:166
requires E e noexcept(noexcept(s.error(std::move(e))))
TemporaryFile(TemporaryFile &&other) noexcept
Definition: TestUtil.h:55
std::string glogErrOrWarnPattern()
Definition: TestUtil.h:212
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
TemporaryFile & operator=(TemporaryFile &&other)
Definition: TestUtil.h:59
TemporaryFile(StringPiece namePrefix=StringPiece(), fs::path dir=fs::path(), Scope scope=Scope::UNLINK_ON_DESTRUCTION, bool closeOnDestruction=true)
Definition: TestUtil.cpp:55
std::string glogWarningPattern()
Definition: TestUtil.h:208
const fs::path & path() const
Definition: TestUtil.cpp:85
TemporaryFile file_
Definition: TestUtil.h:259
std::string glogErrorPattern()
Definition: TestUtil.h:205
SavedState disableInvalidParameters()
Definition: TestUtil.cpp:146
const fs::path & path() const
Definition: TestUtil.h:138
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
const char * string
Definition: Conv.cpp:212
std::unique_ptr< fs::path > path_
Definition: TestUtil.h:122
std::function< void(folly::StringPiece)> ChunkCob
Definition: TestUtil.h:229
void enableInvalidParameters(SavedState)
Definition: TestUtil.cpp:169
void assign(TemporaryFile &other)
Definition: TestUtil.h:80
Range< const char * > StringPiece
bool hasPCREPatternMatch(StringPiece pattern, StringPiece target)
Definition: TestUtil.cpp:172
const fs::path & path() const
Definition: TestUtil.h:116
state
Definition: http_parser.c:272