diff options
author | Mathias Stearn <mathias@10gen.com> | 2013-09-25 14:33:33 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2013-09-27 17:11:07 -0400 |
commit | 152e2750461f8410010a30f95431e43314f131ab (patch) | |
tree | 7fa5d60a84263618bac288d81604a4d8baad68c0 /src/mongo/unittest/temp_dir.h | |
parent | 8a2181ba012c8d28faeb5f5dd56cc7e19ae4ec56 (diff) | |
download | mongo-152e2750461f8410010a30f95431e43314f131ab.tar.gz |
SERVER-10868 Step 1: Make an RAII TempDir class for unit tests
Prep for SERVER-10868: move external sort tests out of dbtests
Diffstat (limited to 'src/mongo/unittest/temp_dir.h')
-rw-r--r-- | src/mongo/unittest/temp_dir.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/mongo/unittest/temp_dir.h b/src/mongo/unittest/temp_dir.h new file mode 100644 index 00000000000..e32a659dff9 --- /dev/null +++ b/src/mongo/unittest/temp_dir.h @@ -0,0 +1,55 @@ +/** + * Copyright (C) 2013 10gen Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <string> + +#include "mongo/base/disallow_copying.h" + +namespace mongo { +namespace unittest { + /** + * An RAII temporary directory that deletes itself and all contents files on scope exit. + */ + class TempDir { + MONGO_DISALLOW_COPYING(TempDir); + public: + /** + * Creates a new unique temporary directory. + * + * Throws if this fails for any reason, such as bad permissions. + * + * The leaf of the directory path will start with namePrefix and have + * unspecified characters added to ensure uniqueness. + * + * namePrefix must not contain either / or \ + */ + explicit TempDir(const std::string& namePrefix); + + /** + * Delete the directory and all contents. + * + * This only does best-effort. In particular no new files should be created in the directory + * once the TempDir goes out of scope. Any errors are logged and ignored. + */ + ~TempDir(); + + const std::string& path() { return _path; } + + private: + std::string _path; + }; +} // namespace unittest +} // namespace mongo |