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 #ifndef FOLLY_TESTUTIL_H_
18 #define FOLLY_TESTUTIL_H_
21 #include <folly/Range.h>
22 #include <folly/experimental/io/FsUtil.h>
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.
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
45 explicit TemporaryFile(StringPiece namePrefix = StringPiece(),
46 fs::path dir = fs::path(),
47 Scope scope = Scope::UNLINK_ON_DESTRUCTION,
48 bool closeOnDestruction = true);
51 // Movable, but not copiable
52 TemporaryFile(TemporaryFile&&) = default;
53 TemporaryFile& operator=(TemporaryFile&&) = default;
55 int fd() const { return fd_; }
56 const fs::path& path() const;
60 bool closeOnDestruction_;
66 * Temporary directory.
68 * By default, the temporary directory is created in a system-specific
69 * location (the value of the TMPDIR environment variable, or /tmp), but you
70 * can override that with a non-empty directory passed to the constructor.
72 * By default, the directory is recursively deleted when the TemporaryDirectory
73 * object is destroyed, but that can be overridden with an argument
77 class TemporaryDirectory {
83 explicit TemporaryDirectory(StringPiece namePrefix = StringPiece(),
84 fs::path dir = fs::path(),
85 Scope scope = Scope::DELETE_ON_DESTRUCTION);
86 ~TemporaryDirectory();
88 // Movable, but not copiable
89 TemporaryDirectory(TemporaryDirectory&&) = default;
90 TemporaryDirectory& operator=(TemporaryDirectory&&) = default;
92 const fs::path& path() const { return path_; }
100 * Changes into a temporary directory, and deletes it with all its contents
101 * upon destruction, also changing back to the original working directory.
103 class ChangeToTempDir {
108 // Movable, but not copiable
109 ChangeToTempDir(ChangeToTempDir&&) = default;
110 ChangeToTempDir& operator=(ChangeToTempDir&&) = default;
112 const fs::path& path() const { return dir_.path(); }
115 fs::path initialPath_;
116 TemporaryDirectory dir_;
122 #endif /* FOLLY_TESTUTIL_H_ */