Pull from FB rev 63ce89e2f2301e6bba44a111cc7d4218022156f6
[folly.git] / folly / experimental / TestUtil.h
1 /*
2  * Copyright 2012 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 #ifndef FOLLY_TESTUTIL_H_
18 #define FOLLY_TESTUTIL_H_
19
20 #include <string>
21
22 namespace folly {
23 namespace test {
24
25 /**
26  * Temporary file.
27  *
28  * By default, the file is created in a system-specific location (the value
29  * of the TMPDIR environment variable, or /tmp), but you can override that
30  * by making "prefix" be a path (containing a '/'; use a prefix starting with
31  * './' to create a file in the current directory).
32  *
33  * By default, the file is closed and deleted when the TemporaryFile object
34  * is destroyed, but both these behaviors can be overridden with arguments
35  * to the constructor.
36  */
37 class TemporaryFile {
38  public:
39   enum class Scope {
40     PERMANENT,
41     UNLINK_IMMEDIATELY,
42     UNLINK_ON_DESTRUCTION
43   };
44   explicit TemporaryFile(const char* prefix=nullptr,
45                          Scope scope=Scope::UNLINK_ON_DESTRUCTION,
46                          bool closeOnDestruction=true);
47   ~TemporaryFile();
48
49   int fd() const { return fd_; }
50   const std::string& path() const;
51
52  private:
53   Scope scope_;
54   bool closeOnDestruction_;
55   int fd_;
56   std::string path_;
57 };
58
59 }  // namespace test
60 }  // namespace folly
61
62 #endif /* FOLLY_TESTUTIL_H_ */
63