proxygen
TestUtilTest.cpp File Reference
#include <folly/experimental/TestUtil.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>

Go to the source code of this file.

Functions

 TEST (TemporaryFile, Simple)
 
 TEST (TemporaryFile, EarlyClose)
 
 TEST (TemporaryFile, Prefix)
 
 TEST (TemporaryFile, PathPrefix)
 
 TEST (TemporaryFile, NoSuchPath)
 
 TEST (TemporaryFile, moveAssignment)
 
 TEST (TemporaryFile, moveCtor)
 
void testTemporaryDirectory (TemporaryDirectory::Scope scope)
 
 TEST (TemporaryDirectory, Permanent)
 
 TEST (TemporaryDirectory, DeleteOnDestruction)
 
void expectTempdirExists (const TemporaryDirectory &d)
 
 TEST (TemporaryDirectory, SafelyMove)
 
 TEST (ChangeToTempDir, ChangeDir)
 
 TEST (PCREPatternMatch, Simple)
 
 TEST (CaptureFD, GlogPatterns)
 
 TEST (CaptureFD, ChunkCob)
 

Function Documentation

void expectTempdirExists ( const TemporaryDirectory d)

Definition at line 146 of file TestUtilTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, and folly::test::TemporaryDirectory::path().

Referenced by TEST().

146  {
147  EXPECT_FALSE(d.path().empty());
148  EXPECT_TRUE(fs::exists(d.path()));
149  EXPECT_TRUE(fs::is_directory(d.path()));
150 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
const fs::path & path() const
Definition: TestUtil.h:116
TEST ( TemporaryFile  ,
Simple   
)

Definition at line 32 of file TestUtilTest.cpp.

References c, EXPECT_EQ, EXPECT_FALSE, EXPECT_LE, EXPECT_TRUE, f, folly::test::TemporaryFile::fd(), folly::test::msvcSuppressAbortOnInvalidParams(), folly::test::TemporaryFile::path(), and fizz::detail::write().

32  {
33  int fd = -1;
34  char c = 'x';
35  {
37  EXPECT_FALSE(f.path().empty());
38  EXPECT_TRUE(f.path().is_absolute());
39  fd = f.fd();
40  EXPECT_LE(0, fd);
41  ssize_t r = write(fd, &c, 1);
42  EXPECT_EQ(1, r);
43  }
44 
46  // The file must have been closed. This assumes that no other thread
47  // has opened another file in the meanwhile, which is a sane assumption
48  // to make in this test.
49  ssize_t r = write(fd, &c, 1);
50  int savedErrno = errno;
51  EXPECT_EQ(-1, r);
52  EXPECT_EQ(EBADF, savedErrno);
53  });
54 }
#define EXPECT_LE(val1, val2)
Definition: gtest.h:1928
auto f
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
auto msvcSuppressAbortOnInvalidParams(Func func) -> decltype(func())
Definition: TestUtil.h:166
const fs::path & path() const
Definition: TestUtil.cpp:85
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
char c
TEST ( TemporaryFile  ,
EarlyClose   
)

Definition at line 56 of file TestUtilTest.cpp.

References folly::test::TemporaryFile::close(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, f, folly::test::TemporaryFile::fd(), and folly::test::TemporaryFile::path().

56  {
57  fs::path p;
58  {
60  p = f.path();
61  EXPECT_TRUE(fs::exists(p));
62  f.close();
63  EXPECT_EQ(-1, f.fd());
64  EXPECT_TRUE(fs::exists(p));
65  }
66  EXPECT_FALSE(fs::exists(p));
67 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const fs::path & path() const
Definition: TestUtil.cpp:85
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( TemporaryFile  ,
Prefix   
)

Definition at line 69 of file TestUtilTest.cpp.

References EXPECT_TRUE, f, folly::test::TemporaryFile::path(), and folly::fs::starts_with().

69  {
70  TemporaryFile f("Foo");
71  EXPECT_TRUE(f.path().is_absolute());
73  boost::algorithm::starts_with(f.path().filename().native(), "Foo"));
74 }
auto f
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool starts_with(const path &pth, const path &prefix)
Definition: FsUtil.cpp:45
TEST ( TemporaryFile  ,
PathPrefix   
)

Definition at line 76 of file TestUtilTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, f, folly::test::TemporaryFile::path(), and folly::fs::starts_with().

76  {
77  TemporaryFile f("Foo", ".");
78  EXPECT_EQ(fs::path("."), f.path().parent_path());
80  boost::algorithm::starts_with(f.path().filename().native(), "Foo"));
81 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool starts_with(const path &pth, const path &prefix)
Definition: FsUtil.cpp:45
TEST ( TemporaryFile  ,
NoSuchPath   
)

Definition at line 83 of file TestUtilTest.cpp.

References EXPECT_THROW, and f.

83  {
84  EXPECT_THROW({ TemporaryFile f("", "/no/such/path"); }, std::system_error);
85 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
TEST ( TemporaryFile  ,
moveAssignment   
)

Definition at line 87 of file TestUtilTest.cpp.

References EXPECT_EQ, EXPECT_NE, EXPECT_TRUE, f, folly::test::TemporaryFile::fd(), g(), h, folly::gen::move, and folly::test::TemporaryFile::path().

87  {
89  int fd;
90 
91  EXPECT_TRUE(f.path().is_absolute());
92  {
93  TemporaryFile g("Foo", ".");
94  EXPECT_NE(g.fd(), -1);
95  fd = g.fd();
96  f = std::move(g);
97  }
98  EXPECT_EQ(fs::path("."), f.path().parent_path());
99  EXPECT_EQ(f.fd(), fd);
100 
101  TemporaryFile h = TemporaryFile("FooBar", ".");
102  EXPECT_NE(h.fd(), -1);
103 }
*than *hazptr_holder h
Definition: Hazptr.h:116
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const fs::path & path() const
Definition: TestUtil.cpp:85
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
g_t g(f_t)
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( TemporaryFile  ,
moveCtor   
)

Definition at line 105 of file TestUtilTest.cpp.

References EXPECT_EQ, EXPECT_NE, f, folly::test::TemporaryFile::fd(), g(), and folly::gen::move.

105  {
106  struct FooBar {
107  TemporaryFile f_;
108  explicit FooBar(TemporaryFile&& f) : f_(std::move(f)) {}
109  };
110  TemporaryFile g("Foo");
111  FooBar fb(std::move(g));
112  EXPECT_EQ(g.fd(), -1);
113  EXPECT_NE(fb.f_.fd(), -1);
114 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
g_t g(f_t)
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( TemporaryDirectory  ,
Permanent   
)

Definition at line 138 of file TestUtilTest.cpp.

References folly::test::TemporaryDirectory::PERMANENT, and testTemporaryDirectory().

138  {
139  testTemporaryDirectory(TemporaryDirectory::Scope::PERMANENT);
140 }
void testTemporaryDirectory(TemporaryDirectory::Scope scope)
TEST ( TemporaryDirectory  ,
DeleteOnDestruction   
)

Definition at line 142 of file TestUtilTest.cpp.

References folly::test::TemporaryDirectory::DELETE_ON_DESTRUCTION, and testTemporaryDirectory().

142  {
143  testTemporaryDirectory(TemporaryDirectory::Scope::DELETE_ON_DESTRUCTION);
144 }
void testTemporaryDirectory(TemporaryDirectory::Scope scope)
TEST ( TemporaryDirectory  ,
SafelyMove   
)

Definition at line 152 of file TestUtilTest.cpp.

References folly::test::TemporaryDirectory::DELETE_ON_DESTRUCTION, expectTempdirExists(), and folly::gen::move.

152  {
153  std::unique_ptr<TemporaryDirectory> dir;
154  TemporaryDirectory dir2;
155  {
156  auto scope = TemporaryDirectory::Scope::DELETE_ON_DESTRUCTION;
157  TemporaryDirectory d("", "", scope);
158  TemporaryDirectory d2("", "", scope);
161 
162  dir = std::make_unique<TemporaryDirectory>(std::move(d));
163  dir2 = std::move(d2);
164  }
165 
166  expectTempdirExists(*dir);
167  expectTempdirExists(dir2);
168 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void expectTempdirExists(const TemporaryDirectory &d)
TEST ( ChangeToTempDir  ,
ChangeDir   
)

Definition at line 170 of file TestUtilTest.cpp.

References EXPECT_EQ, and EXPECT_NE.

170  {
171  auto pwd1 = fs::current_path();
172  {
173  ChangeToTempDir d;
174  EXPECT_NE(pwd1, fs::current_path());
175  }
176  EXPECT_EQ(pwd1, fs::current_path());
177 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( PCREPatternMatch  ,
Simple   
)

Definition at line 179 of file TestUtilTest.cpp.

References EXPECT_NO_PCRE_MATCH, and EXPECT_PCRE_MATCH.

179  {
180  EXPECT_PCRE_MATCH(".*a.c.*", "gabca");
181  EXPECT_NO_PCRE_MATCH("a.c", "gabca");
182  EXPECT_NO_PCRE_MATCH(".*ac.*", "gabca");
183 }
#define EXPECT_NO_PCRE_MATCH(pattern_stringpiece, target_stringpiece)
Definition: TestUtil.h:184
#define EXPECT_PCRE_MATCH(pattern_stringpiece, target_stringpiece)
Definition: TestUtil.h:179
TEST ( CaptureFD  ,
GlogPatterns   
)

Definition at line 185 of file TestUtilTest.cpp.

References EXPECT_NO_PCRE_MATCH, EXPECT_PCRE_MATCH, folly::test::glogErrorPattern(), folly::test::glogErrOrWarnPattern(), folly::test::glogWarningPattern(), folly::INFO, folly::test::CaptureFD::readIncremental(), s, and folly::WARNING.

185  {
186  CaptureFD err(fileno(stderr));
187  LOG(INFO) << "All is well";
188  EXPECT_NO_PCRE_MATCH(glogErrOrWarnPattern(), err.readIncremental());
189  {
190  LOG(ERROR) << "Uh-oh";
191  auto s = err.readIncremental();
195  }
196  {
197  LOG(WARNING) << "Oops";
198  auto s = err.readIncremental();
202  }
203 }
#define EXPECT_NO_PCRE_MATCH(pattern_stringpiece, target_stringpiece)
Definition: TestUtil.h:184
#define EXPECT_PCRE_MATCH(pattern_stringpiece, target_stringpiece)
Definition: TestUtil.h:179
std::string glogErrOrWarnPattern()
Definition: TestUtil.h:212
std::string glogWarningPattern()
Definition: TestUtil.h:208
std::string glogErrorPattern()
Definition: TestUtil.h:205
static set< string > s
TEST ( CaptureFD  ,
ChunkCob   
)

Definition at line 205 of file TestUtilTest.cpp.

References chunks, EXPECT_EQ, EXPECT_PCRE_MATCH, FAIL, folly::INFO, and folly::Range< Iter >::str().

205  {
206  std::vector<std::string> chunks;
207  {
208  CaptureFD err(fileno(stderr), [&](StringPiece p) {
209  chunks.emplace_back(p.str());
210  switch (chunks.size()) {
211  case 1:
212  EXPECT_PCRE_MATCH(".*foo.*bar.*", p);
213  break;
214  case 2:
215  EXPECT_PCRE_MATCH("[^\n]*baz.*", p);
216  break;
217  default:
218  FAIL() << "Got too many chunks: " << chunks.size();
219  }
220  });
221  LOG(INFO) << "foo";
222  LOG(INFO) << "bar";
223  EXPECT_PCRE_MATCH(".*foo.*bar.*", err.read());
224  auto chunk = err.readIncremental();
225  EXPECT_EQ(chunks.at(0), chunk);
226  LOG(INFO) << "baz";
227  EXPECT_PCRE_MATCH(".*foo.*bar.*baz.*", err.read());
228  }
229  EXPECT_EQ(2, chunks.size());
230 }
auto chunks
#define FAIL()
Definition: gtest.h:1822
std::string str() const
Definition: Range.h:591
#define EXPECT_PCRE_MATCH(pattern_stringpiece, target_stringpiece)
Definition: TestUtil.h:179
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void testTemporaryDirectory ( TemporaryDirectory::Scope  scope)

Definition at line 116 of file TestUtilTest.cpp.

References folly::netops::close(), EXPECT_EQ, EXPECT_FALSE, EXPECT_NE, EXPECT_TRUE, f, folly::test::TemporaryDirectory::path(), and folly::test::TemporaryDirectory::PERMANENT.

Referenced by TEST().

116  {
117  fs::path path;
118  {
119  TemporaryDirectory d("", "", scope);
120  path = d.path();
121  EXPECT_FALSE(path.empty());
122  EXPECT_TRUE(path.is_absolute());
123  EXPECT_TRUE(fs::exists(path));
124  EXPECT_TRUE(fs::is_directory(path));
125 
126  fs::path fp = path / "bar";
127  int fd = open(fp.string().c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
128  EXPECT_NE(fd, -1);
129  close(fd);
130 
131  TemporaryFile f("Foo", d.path());
132  EXPECT_EQ(d.path(), f.path().parent_path());
133  }
134  bool exists = (scope == TemporaryDirectory::Scope::PERMANENT);
135  EXPECT_EQ(exists, fs::exists(path));
136 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
int close(NetworkSocket s)
Definition: NetOps.cpp:90