2 * Copyright 2016 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>
20 #include <sys/types.h>
23 #include <system_error>
25 #include <boost/algorithm/string.hpp>
26 #include <folly/Memory.h>
27 #include <glog/logging.h>
28 #include <gtest/gtest.h>
30 using namespace folly;
31 using namespace folly::test;
33 TEST(TemporaryFile, Simple) {
38 EXPECT_FALSE(f.path().empty());
39 EXPECT_TRUE(f.path().is_absolute());
42 ssize_t r = write(fd, &c, 1);
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);
55 TEST(TemporaryFile, Prefix) {
56 TemporaryFile f("Foo");
57 EXPECT_TRUE(f.path().is_absolute());
58 EXPECT_TRUE(boost::algorithm::starts_with(f.path().filename().native(),
62 TEST(TemporaryFile, PathPrefix) {
63 TemporaryFile f("Foo", ".");
64 EXPECT_EQ(fs::path("."), f.path().parent_path());
65 EXPECT_TRUE(boost::algorithm::starts_with(f.path().filename().native(),
69 TEST(TemporaryFile, NoSuchPath) {
70 EXPECT_THROW({TemporaryFile f("", "/no/such/path");},
74 void testTemporaryDirectory(TemporaryDirectory::Scope scope) {
77 TemporaryDirectory d("", "", scope);
79 EXPECT_FALSE(path.empty());
80 EXPECT_TRUE(path.is_absolute());
81 EXPECT_TRUE(fs::exists(path));
82 EXPECT_TRUE(fs::is_directory(path));
84 fs::path fp = path / "bar";
85 int fd = open(fp.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
89 TemporaryFile f("Foo", d.path());
90 EXPECT_EQ(d.path(), f.path().parent_path());
92 bool exists = (scope == TemporaryDirectory::Scope::PERMANENT);
93 EXPECT_EQ(exists, fs::exists(path));
96 TEST(TemporaryDirectory, Permanent) {
97 testTemporaryDirectory(TemporaryDirectory::Scope::PERMANENT);
100 TEST(TemporaryDirectory, DeleteOnDestruction) {
101 testTemporaryDirectory(TemporaryDirectory::Scope::DELETE_ON_DESTRUCTION);
104 TEST(ChangeToTempDir, ChangeDir) {
105 auto pwd1 = fs::current_path();
108 EXPECT_NE(pwd1, fs::current_path());
110 EXPECT_EQ(pwd1, fs::current_path());
113 TEST(PCREPatternMatch, Simple) {
114 EXPECT_PCRE_MATCH(".*a.c.*", "gabca");
115 EXPECT_NO_PCRE_MATCH("a.c", "gabca");
116 EXPECT_NO_PCRE_MATCH(".*ac.*", "gabca");
119 TEST(CaptureFD, GlogPatterns) {
121 LOG(INFO) << "All is well";
122 EXPECT_NO_PCRE_MATCH(glogErrOrWarnPattern(), stderr.readIncremental());
124 LOG(ERROR) << "Uh-oh";
125 auto s = stderr.readIncremental();
126 EXPECT_PCRE_MATCH(glogErrorPattern(), s);
127 EXPECT_NO_PCRE_MATCH(glogWarningPattern(), s);
128 EXPECT_PCRE_MATCH(glogErrOrWarnPattern(), s);
131 LOG(WARNING) << "Oops";
132 auto s = stderr.readIncremental();
133 EXPECT_NO_PCRE_MATCH(glogErrorPattern(), s);
134 EXPECT_PCRE_MATCH(glogWarningPattern(), s);
135 EXPECT_PCRE_MATCH(glogErrOrWarnPattern(), s);
139 TEST(CaptureFD, ChunkCob) {
140 std::vector<std::string> chunks;
142 CaptureFD stderr(2, [&](StringPiece p) {
143 chunks.emplace_back(p.str());
144 switch (chunks.size()) {
146 EXPECT_PCRE_MATCH(".*foo.*bar.*", p);
149 EXPECT_PCRE_MATCH("[^\n]*baz.*", p);
152 FAIL() << "Got too many chunks: " << chunks.size();
157 EXPECT_PCRE_MATCH(".*foo.*bar.*", stderr.read());
158 auto chunk = stderr.readIncremental();
159 EXPECT_EQ(chunks.at(0), chunk);
161 EXPECT_PCRE_MATCH(".*foo.*bar.*baz.*", stderr.read());
163 EXPECT_EQ(2, chunks.size());
167 class EnvVarSaverTest : public testing::Test {};
169 TEST_F(EnvVarSaverTest, ExampleNew) {
170 auto key = "hahahahaha";
171 EXPECT_EQ(nullptr, getenv(key));
173 auto saver = make_unique<EnvVarSaver>();
174 PCHECK(0 == setenv(key, "blah", true));
175 EXPECT_EQ("blah", std::string{getenv(key)});
177 EXPECT_EQ(nullptr, getenv(key));
180 TEST_F(EnvVarSaverTest, ExampleExisting) {
182 EXPECT_NE(nullptr, getenv(key));
183 auto value = std::string{getenv(key)};
185 auto saver = make_unique<EnvVarSaver>();
186 PCHECK(0 == setenv(key, "blah", true));
187 EXPECT_EQ("blah", std::string{getenv(key)});
189 EXPECT_TRUE(value == getenv(key));
192 TEST_F(EnvVarSaverTest, ExampleDeleting) {
194 EXPECT_NE(nullptr, getenv(key));
195 auto value = std::string{getenv(key)};
197 auto saver = make_unique<EnvVarSaver>();
198 PCHECK(0 == unsetenv(key));
199 EXPECT_EQ(nullptr, getenv(key));
201 EXPECT_TRUE(value == getenv(key));
204 int main(int argc, char *argv[]) {
205 testing::InitGoogleTest(&argc, argv);
206 gflags::ParseCommandLineFlags(&argc, &argv, true);
207 return RUN_ALL_TESTS();