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 #include <folly/experimental/TestUtil.h>
20 #include <sys/types.h>
23 #include <system_error>
25 #include <boost/algorithm/string.hpp>
26 #include <glog/logging.h>
27 #include <gtest/gtest.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 // The file must have been closed. This assumes that no other thread
46 // has opened another file in the meanwhile, which is a sane assumption
47 // to make in this test.
48 ssize_t r = write(fd, &c, 1);
49 int savedErrno = errno;
51 EXPECT_EQ(EBADF, savedErrno);
54 TEST(TemporaryFile, Prefix) {
55 TemporaryFile f("Foo");
56 EXPECT_TRUE(f.path().is_absolute());
57 EXPECT_TRUE(boost::algorithm::starts_with(f.path().filename().native(),
61 TEST(TemporaryFile, PathPrefix) {
62 TemporaryFile f("Foo", ".");
63 EXPECT_EQ(fs::path("."), f.path().parent_path());
64 EXPECT_TRUE(boost::algorithm::starts_with(f.path().filename().native(),
68 TEST(TemporaryFile, NoSuchPath) {
69 EXPECT_THROW({TemporaryFile f("", "/no/such/path");},
73 void testTemporaryDirectory(TemporaryDirectory::Scope scope) {
76 TemporaryDirectory d("", "", scope);
78 EXPECT_FALSE(path.empty());
79 EXPECT_TRUE(path.is_absolute());
80 EXPECT_TRUE(fs::exists(path));
81 EXPECT_TRUE(fs::is_directory(path));
83 fs::path fp = path / "bar";
84 int fd = open(fp.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
88 TemporaryFile f("Foo", d.path());
89 EXPECT_EQ(d.path(), f.path().parent_path());
91 bool exists = (scope == TemporaryDirectory::Scope::PERMANENT);
92 EXPECT_EQ(exists, fs::exists(path));
95 TEST(TemporaryDirectory, Permanent) {
96 testTemporaryDirectory(TemporaryDirectory::Scope::PERMANENT);
99 TEST(TemporaryDirectory, DeleteOnDestruction) {
100 testTemporaryDirectory(TemporaryDirectory::Scope::DELETE_ON_DESTRUCTION);
103 TEST(ChangeToTempDir, ChangeDir) {
104 auto pwd1 = fs::current_path();
107 EXPECT_NE(pwd1, fs::current_path());
109 EXPECT_EQ(pwd1, fs::current_path());
112 TEST(PCREPatternMatch, Simple) {
113 EXPECT_PCRE_MATCH(".*a.c.*", "gabca");
114 EXPECT_NO_PCRE_MATCH("a.c", "gabca");
115 EXPECT_NO_PCRE_MATCH(".*ac.*", "gabca");
118 TEST(CaptureFD, GlogPatterns) {
120 LOG(INFO) << "All is well";
121 EXPECT_NO_PCRE_MATCH(glogErrOrWarnPattern(), stderr.readIncremental());
123 LOG(ERROR) << "Uh-oh";
124 auto s = stderr.readIncremental();
125 EXPECT_PCRE_MATCH(glogErrorPattern(), s);
126 EXPECT_NO_PCRE_MATCH(glogWarningPattern(), s);
127 EXPECT_PCRE_MATCH(glogErrOrWarnPattern(), s);
130 LOG(WARNING) << "Oops";
131 auto s = stderr.readIncremental();
132 EXPECT_NO_PCRE_MATCH(glogErrorPattern(), s);
133 EXPECT_PCRE_MATCH(glogWarningPattern(), s);
134 EXPECT_PCRE_MATCH(glogErrOrWarnPattern(), s);
138 int main(int argc, char *argv[]) {
139 testing::InitGoogleTest(&argc, argv);
140 gflags::ParseCommandLineFlags(&argc, &argv, true);
141 return RUN_ALL_TESTS();