1b518fdeac318396e0b28a550676cfe7940c05bb
[folly.git] / folly / experimental / TestUtil.h
1 /*
2  * Copyright 2014 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 #include <folly/Range.h>
22 #include <folly/experimental/io/FsUtil.h>
23
24 namespace folly {
25 namespace test {
26
27 /**
28  * Temporary file.
29  *
30  * By default, the file is created in a system-specific location (the value
31  * of the TMPDIR environment variable, or /tmp), but you can override that
32  * with a different (non-empty) directory passed to the constructor.
33  *
34  * By default, the file is closed and deleted when the TemporaryFile object
35  * is destroyed, but both these behaviors can be overridden with arguments
36  * to the constructor.
37  */
38 class TemporaryFile {
39  public:
40   enum class Scope {
41     PERMANENT,
42     UNLINK_IMMEDIATELY,
43     UNLINK_ON_DESTRUCTION
44   };
45   explicit TemporaryFile(StringPiece namePrefix = StringPiece(),
46                          fs::path dir = fs::path(),
47                          Scope scope = Scope::UNLINK_ON_DESTRUCTION,
48                          bool closeOnDestruction = true);
49   ~TemporaryFile();
50
51   int fd() const { return fd_; }
52   const fs::path& path() const;
53
54  private:
55   Scope scope_;
56   bool closeOnDestruction_;
57   int fd_;
58   fs::path path_;
59 };
60
61 /**
62  * Temporary directory.
63  *
64  * By default, the temporary directory is created in a system-specific
65  * location (the value of the TMPDIR environment variable, or /tmp), but you
66  * can override that with a non-empty directory passed to the constructor.
67  *
68  * By default, the directory is recursively deleted when the TemporaryDirectory
69  * object is destroyed, but that can be overridden with an argument
70  * to the constructor.
71  */
72
73 class TemporaryDirectory {
74  public:
75   enum class Scope {
76     PERMANENT,
77     DELETE_ON_DESTRUCTION
78   };
79   explicit TemporaryDirectory(StringPiece namePrefix = StringPiece(),
80                               fs::path dir = fs::path(),
81                               Scope scope = Scope::DELETE_ON_DESTRUCTION);
82   ~TemporaryDirectory();
83
84   const fs::path& path() const { return path_; }
85
86  private:
87   Scope scope_;
88   fs::path path_;
89 };
90
91 }  // namespace test
92 }  // namespace folly
93
94 #endif /* FOLLY_TESTUTIL_H_ */