EnvVarSaver.
[folly.git] / folly / experimental / TestUtil.cpp
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/experimental/TestUtil.h>
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #include <boost/regex.hpp>
25 #include <folly/Conv.h>
26 #include <folly/Exception.h>
27 #include <folly/File.h>
28 #include <folly/FileUtil.h>
29 #include <folly/String.h>
30
31 namespace folly {
32 namespace test {
33
34 namespace {
35
36 fs::path generateUniquePath(fs::path path, StringPiece namePrefix) {
37   if (path.empty()) {
38     path = fs::temp_directory_path();
39   }
40   if (namePrefix.empty()) {
41     path /= fs::unique_path();
42   } else {
43     path /= fs::unique_path(
44         to<std::string>(namePrefix, ".%%%%-%%%%-%%%%-%%%%"));
45   }
46   return path;
47 }
48
49 }  // namespace
50
51 TemporaryFile::TemporaryFile(StringPiece namePrefix,
52                              fs::path dir,
53                              Scope scope,
54                              bool closeOnDestruction)
55   : scope_(scope),
56     closeOnDestruction_(closeOnDestruction),
57     fd_(-1),
58     path_(generateUniquePath(std::move(dir), namePrefix)) {
59   fd_ = open(path_.string().c_str(), O_RDWR | O_CREAT | O_EXCL, 0666);
60   checkUnixError(fd_, "open failed");
61
62   if (scope_ == Scope::UNLINK_IMMEDIATELY) {
63     boost::system::error_code ec;
64     fs::remove(path_, ec);
65     if (ec) {
66       LOG(WARNING) << "unlink on construction failed: " << ec;
67     } else {
68       path_.clear();
69     }
70   }
71 }
72
73 const fs::path& TemporaryFile::path() const {
74   CHECK(scope_ != Scope::UNLINK_IMMEDIATELY);
75   DCHECK(!path_.empty());
76   return path_;
77 }
78
79 TemporaryFile::~TemporaryFile() {
80   if (fd_ != -1 && closeOnDestruction_) {
81     if (close(fd_) == -1) {
82       PLOG(ERROR) << "close failed";
83     }
84   }
85
86   // If we previously failed to unlink() (UNLINK_IMMEDIATELY), we'll
87   // try again here.
88   if (scope_ != Scope::PERMANENT && !path_.empty()) {
89     boost::system::error_code ec;
90     fs::remove(path_, ec);
91     if (ec) {
92       LOG(WARNING) << "unlink on destruction failed: " << ec;
93     }
94   }
95 }
96
97 TemporaryDirectory::TemporaryDirectory(StringPiece namePrefix,
98                                        fs::path dir,
99                                        Scope scope)
100   : scope_(scope),
101     path_(generateUniquePath(std::move(dir), namePrefix)) {
102   fs::create_directory(path_);
103 }
104
105 TemporaryDirectory::~TemporaryDirectory() {
106   if (scope_ == Scope::DELETE_ON_DESTRUCTION) {
107     boost::system::error_code ec;
108     fs::remove_all(path_, ec);
109     if (ec) {
110       LOG(WARNING) << "recursive delete on destruction failed: " << ec;
111     }
112   }
113 }
114
115 ChangeToTempDir::ChangeToTempDir() : initialPath_(fs::current_path()) {
116   std::string p = dir_.path().string();
117   ::chdir(p.c_str());
118 }
119
120 ChangeToTempDir::~ChangeToTempDir() {
121   std::string p = initialPath_.string();
122   ::chdir(p.c_str());
123 }
124
125 namespace detail {
126
127 bool hasPCREPatternMatch(StringPiece pattern, StringPiece target) {
128   return boost::regex_match(
129     target.begin(),
130     target.end(),
131     boost::regex(pattern.begin(), pattern.end())
132   );
133 }
134
135 bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target) {
136   return !hasPCREPatternMatch(pattern, target);
137 }
138
139 }  // namespace detail
140
141 CaptureFD::CaptureFD(int fd) : fd_(fd), readOffset_(0) {
142   oldFDCopy_ = dup(fd_);
143   PCHECK(oldFDCopy_ != -1) << "Could not copy FD " << fd_;
144
145   int file_fd = open(file_.path().string().c_str(), O_WRONLY|O_CREAT, 0600);
146   PCHECK(dup2(file_fd, fd_) != -1) << "Could not replace FD " << fd_
147     << " with " << file_fd;
148   PCHECK(close(file_fd) != -1) << "Could not close " << file_fd;
149 }
150
151 void CaptureFD::release() {
152   if (oldFDCopy_ != fd_) {
153     PCHECK(dup2(oldFDCopy_, fd_) != -1) << "Could not restore old FD "
154       << oldFDCopy_ << " into " << fd_;
155     PCHECK(close(oldFDCopy_) != -1) << "Could not close " << oldFDCopy_;
156     oldFDCopy_ = fd_;  // Make this call idempotent
157   }
158 }
159
160 CaptureFD::~CaptureFD() {
161   release();
162 }
163
164 std::string CaptureFD::read() {
165   std::string contents;
166   std::string filename = file_.path().string();
167   PCHECK(folly::readFile(filename.c_str(), contents));
168   return contents;
169 }
170
171 std::string CaptureFD::readIncremental() {
172   std::string filename = file_.path().string();
173   // Yes, I know that I could just keep the file open instead. So sue me.
174   folly::File f(openNoInt(filename.c_str(), O_RDONLY), true);
175   auto size = lseek(f.fd(), 0, SEEK_END) - readOffset_;
176   std::unique_ptr<char[]> buf(new char[size]);
177   auto bytes_read = folly::preadFull(f.fd(), buf.get(), size, readOffset_);
178   PCHECK(size == bytes_read);
179   readOffset_ += size;
180   return std::string(buf.get(), size);
181 }
182
183 static std::map<std::string, std::string> getEnvVarMap() {
184   std::map<std::string, std::string> data;
185   for (auto it = environ; *it != nullptr; ++it) {
186     std::string key, value;
187     split("=", *it, key, value);
188     if (key.empty()) {
189       continue;
190     }
191     CHECK(!data.count(key)) << "already contains: " << key;
192     data.emplace(move(key), move(value));
193   }
194   return data;
195 }
196
197 EnvVarSaver::EnvVarSaver() {
198   saved_ = getEnvVarMap();
199 }
200
201 EnvVarSaver::~EnvVarSaver() {
202   for (const auto& kvp : getEnvVarMap()) {
203     if (saved_.count(kvp.first)) {
204       continue;
205     }
206     PCHECK(0 == unsetenv(kvp.first.c_str()));
207   }
208   for (const auto& kvp : saved_) {
209     PCHECK(0 == setenv(kvp.first.c_str(), kvp.second.c_str(), (int)true));
210   }
211 }
212
213 }  // namespace test
214 }  // namespace folly