2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_TESTUTIL_H_
18 #define FOLLY_TESTUTIL_H_
22 #include <folly/Range.h>
23 #include <folly/experimental/io/FsUtil.h>
31 * By default, the file is created in a system-specific location (the value
32 * of the TMPDIR environment variable, or /tmp), but you can override that
33 * with a different (non-empty) directory passed to the constructor.
35 * By default, the file is closed and deleted when the TemporaryFile object
36 * is destroyed, but both these behaviors can be overridden with arguments
46 explicit TemporaryFile(StringPiece namePrefix = StringPiece(),
47 fs::path dir = fs::path(),
48 Scope scope = Scope::UNLINK_ON_DESTRUCTION,
49 bool closeOnDestruction = true);
52 // Movable, but not copiable
53 TemporaryFile(TemporaryFile&&) = default;
54 TemporaryFile& operator=(TemporaryFile&&) = default;
56 int fd() const { return fd_; }
57 const fs::path& path() const;
61 bool closeOnDestruction_;
67 * Temporary directory.
69 * By default, the temporary directory is created in a system-specific
70 * location (the value of the TMPDIR environment variable, or /tmp), but you
71 * can override that with a non-empty directory passed to the constructor.
73 * By default, the directory is recursively deleted when the TemporaryDirectory
74 * object is destroyed, but that can be overridden with an argument
78 class TemporaryDirectory {
84 explicit TemporaryDirectory(StringPiece namePrefix = StringPiece(),
85 fs::path dir = fs::path(),
86 Scope scope = Scope::DELETE_ON_DESTRUCTION);
87 ~TemporaryDirectory();
89 // Movable, but not copiable
90 TemporaryDirectory(TemporaryDirectory&&) = default;
91 TemporaryDirectory& operator=(TemporaryDirectory&&) = default;
93 const fs::path& path() const { return path_; }
101 * Changes into a temporary directory, and deletes it with all its contents
102 * upon destruction, also changing back to the original working directory.
104 class ChangeToTempDir {
109 // Movable, but not copiable
110 ChangeToTempDir(ChangeToTempDir&&) = default;
111 ChangeToTempDir& operator=(ChangeToTempDir&&) = default;
113 const fs::path& path() const { return dir_.path(); }
116 fs::path initialPath_;
117 TemporaryDirectory dir_;
121 * Easy PCRE regex matching. Note that pattern must match the ENTIRE target,
122 * so use .* at the start and end of the pattern, as appropriate. See
123 * http://regex101.com/ for a PCRE simulator.
125 #define EXPECT_PCRE_MATCH(pattern_stringpiece, target_stringpiece) \
127 ::folly::test::detail::hasPCREPatternMatch, \
128 pattern_stringpiece, \
131 #define EXPECT_NO_PCRE_MATCH(pattern_stringpiece, target_stringpiece) \
133 ::folly::test::detail::hasNoPCREPatternMatch, \
134 pattern_stringpiece, \
139 bool hasPCREPatternMatch(StringPiece pattern, StringPiece target);
140 bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target);
141 } // namespace detail
144 * Use these patterns together with CaptureFD and EXPECT_PCRE_MATCH() to
145 * test for the presence (or absence) of log lines at a particular level:
147 * CaptureFD stderr(2);
148 * LOG(INFO) << "All is well";
149 * EXPECT_NO_PCRE_MATCH(glogErrOrWarnPattern(), stderr.readIncremental());
150 * LOG(ERROR) << "Uh-oh";
151 * EXPECT_PCRE_MATCH(glogErrorPattern(), stderr.readIncremental());
153 inline std::string glogErrorPattern() { return ".*(^|\n)E[0-9].*"; }
154 inline std::string glogWarningPattern() { return ".*(^|\n)W[0-9].*"; }
156 inline std::string glogErrOrWarnPattern() { return ".*(^|\n)[EW][0-9].*"; }
159 * Temporarily capture a file descriptor by redirecting it into a file.
160 * You can consume its entire output thus far via read(), incrementally
161 * via readIncremental(), or via callback using chunk_cob.
162 * Great for testing logging (see also glog*Pattern()).
166 struct NoOpChunkCob { void operator()(StringPiece) {} };
168 using ChunkCob = std::function<void(folly::StringPiece)>;
171 * chunk_cob is is guaranteed to consume all the captured output. It is
172 * invoked on each readIncremental(), and also on FD release to capture
173 * as-yet unread lines. Chunks can be empty.
175 explicit CaptureFD(int fd, ChunkCob chunk_cob = NoOpChunkCob());
179 * Restore the captured FD to its original state. It can be useful to do
180 * this before the destructor so that you can read() the captured data and
181 * log about it to the formerly captured stderr or stdout.
186 * Reads the whole file into a string, but does not remove the redirect.
188 std::string read() const;
191 * Read any bytes that were appended to the file since the last
192 * readIncremental. Great for testing line-by-line output.
194 std::string readIncremental();
201 int oldFDCopy_; // equal to fd_ after restore()
203 off_t readOffset_; // for incremental reading
211 std::map<std::string, std::string> saved_;
217 #endif /* FOLLY_TESTUTIL_H_ */