2 * Copyright 2017 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 #include <folly/experimental/TestUtil.h>
19 #include <system_error>
21 #include <boost/algorithm/string.hpp>
22 #include <glog/logging.h>
24 #include <folly/Memory.h>
25 #include <folly/portability/Fcntl.h>
26 #include <folly/portability/GTest.h>
27 #include <folly/portability/Stdlib.h>
29 using namespace folly;
30 using namespace folly::test;
32 TEST(TemporaryFile, Simple) {
37 EXPECT_FALSE(f.path().empty());
38 EXPECT_TRUE(f.path().is_absolute());
41 ssize_t r = write(fd, &c, 1);
45 msvcSuppressAbortOnInvalidParams([&] {
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;
52 EXPECT_EQ(EBADF, savedErrno);
56 TEST(TemporaryFile, EarlyClose) {
61 EXPECT_TRUE(fs::exists(p));
63 EXPECT_EQ(-1, f.fd());
64 EXPECT_TRUE(fs::exists(p));
66 EXPECT_FALSE(fs::exists(p));
69 TEST(TemporaryFile, Prefix) {
70 TemporaryFile f("Foo");
71 EXPECT_TRUE(f.path().is_absolute());
72 EXPECT_TRUE(boost::algorithm::starts_with(f.path().filename().native(),
76 TEST(TemporaryFile, PathPrefix) {
77 TemporaryFile f("Foo", ".");
78 EXPECT_EQ(fs::path("."), f.path().parent_path());
79 EXPECT_TRUE(boost::algorithm::starts_with(f.path().filename().native(),
83 TEST(TemporaryFile, NoSuchPath) {
84 EXPECT_THROW({TemporaryFile f("", "/no/such/path");},
88 TEST(TemporaryFile, moveAssignment) {
92 EXPECT_TRUE(f.path().is_absolute());
94 TemporaryFile g("Foo", ".");
95 EXPECT_NE(g.fd(), -1);
99 EXPECT_EQ(fs::path("."), f.path().parent_path());
100 EXPECT_EQ(f.fd(), fd);
102 TemporaryFile h = TemporaryFile("FooBar", ".");
103 EXPECT_NE(h.fd(), -1);
106 TEST(TemporaryFile, moveCtor) {
109 explicit FooBar(TemporaryFile&& f) : f_(std::move(f)) {}
111 TemporaryFile g("Foo");
112 FooBar fb(std::move(g));
113 EXPECT_EQ(g.fd(), -1);
114 EXPECT_NE(fb.f_.fd(), -1);
117 void testTemporaryDirectory(TemporaryDirectory::Scope scope) {
120 TemporaryDirectory d("", "", scope);
122 EXPECT_FALSE(path.empty());
123 EXPECT_TRUE(path.is_absolute());
124 EXPECT_TRUE(fs::exists(path));
125 EXPECT_TRUE(fs::is_directory(path));
127 fs::path fp = path / "bar";
128 int fd = open(fp.string().c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
132 TemporaryFile f("Foo", d.path());
133 EXPECT_EQ(d.path(), f.path().parent_path());
135 bool exists = (scope == TemporaryDirectory::Scope::PERMANENT);
136 EXPECT_EQ(exists, fs::exists(path));
139 TEST(TemporaryDirectory, Permanent) {
140 testTemporaryDirectory(TemporaryDirectory::Scope::PERMANENT);
143 TEST(TemporaryDirectory, DeleteOnDestruction) {
144 testTemporaryDirectory(TemporaryDirectory::Scope::DELETE_ON_DESTRUCTION);
147 void expectTempdirExists(const TemporaryDirectory& d) {
148 EXPECT_FALSE(d.path().empty());
149 EXPECT_TRUE(fs::exists(d.path()));
150 EXPECT_TRUE(fs::is_directory(d.path()));
153 TEST(TemporaryDirectory, SafelyMove) {
154 std::unique_ptr<TemporaryDirectory> dir;
155 TemporaryDirectory dir2;
157 auto scope = TemporaryDirectory::Scope::DELETE_ON_DESTRUCTION;
158 TemporaryDirectory d("", "", scope);
159 TemporaryDirectory d2("", "", scope);
160 expectTempdirExists(d);
161 expectTempdirExists(d2);
163 dir = std::make_unique<TemporaryDirectory>(std::move(d));
164 dir2 = std::move(d2);
167 expectTempdirExists(*dir);
168 expectTempdirExists(dir2);
171 TEST(ChangeToTempDir, ChangeDir) {
172 auto pwd1 = fs::current_path();
175 EXPECT_NE(pwd1, fs::current_path());
177 EXPECT_EQ(pwd1, fs::current_path());
180 TEST(PCREPatternMatch, Simple) {
181 EXPECT_PCRE_MATCH(".*a.c.*", "gabca");
182 EXPECT_NO_PCRE_MATCH("a.c", "gabca");
183 EXPECT_NO_PCRE_MATCH(".*ac.*", "gabca");
186 TEST(CaptureFD, GlogPatterns) {
187 CaptureFD err(fileno(stderr));
188 LOG(INFO) << "All is well";
189 EXPECT_NO_PCRE_MATCH(glogErrOrWarnPattern(), err.readIncremental());
191 LOG(ERROR) << "Uh-oh";
192 auto s = err.readIncremental();
193 EXPECT_PCRE_MATCH(glogErrorPattern(), s);
194 EXPECT_NO_PCRE_MATCH(glogWarningPattern(), s);
195 EXPECT_PCRE_MATCH(glogErrOrWarnPattern(), s);
198 LOG(WARNING) << "Oops";
199 auto s = err.readIncremental();
200 EXPECT_NO_PCRE_MATCH(glogErrorPattern(), s);
201 EXPECT_PCRE_MATCH(glogWarningPattern(), s);
202 EXPECT_PCRE_MATCH(glogErrOrWarnPattern(), s);
206 TEST(CaptureFD, ChunkCob) {
207 std::vector<std::string> chunks;
209 CaptureFD err(fileno(stderr), [&](StringPiece p) {
210 chunks.emplace_back(p.str());
211 switch (chunks.size()) {
213 EXPECT_PCRE_MATCH(".*foo.*bar.*", p);
216 EXPECT_PCRE_MATCH("[^\n]*baz.*", p);
219 FAIL() << "Got too many chunks: " << chunks.size();
224 EXPECT_PCRE_MATCH(".*foo.*bar.*", err.read());
225 auto chunk = err.readIncremental();
226 EXPECT_EQ(chunks.at(0), chunk);
228 EXPECT_PCRE_MATCH(".*foo.*bar.*baz.*", err.read());
230 EXPECT_EQ(2, chunks.size());