sapling/eden/fs/testharness/TempFile.cpp
Adam Simpkins 2adb4a36b1 add new helper functions for creating temporary files/dirs in tests
Summary:
Add new helper files for creating temporary files and directories.

The main advantage of these functions is that they prefer creating files in
`/dev/shm` by default instead of `/tmp`.  Some of the eden unit tests are
fairly I/O intensive (particularly the checkout tests, which create and
destroy many test mounts).  This can be quite slow on hosts where `/tmp` is a
a spinning hard disk.  Putting the temporary files in a ramdisk greatly speeds
up the tests in this case.

These test functions also default to using the prefix `eden_test.` for the
temporary file names.

This does not yet change any of the test code to use these functions.  I will
do that in a subsequent diff.

Reviewed By: chadaustin, strager

Differential Revision: D12971161

fbshipit-source-id: 3f74be7a467e8080185d4d97d114288b4528755a
2018-11-09 14:29:17 -08:00

60 lines
1.6 KiB
C++

/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include "eden/fs/testharness/TempFile.h"
#include <unistd.h>
#include <cstdlib>
#include "eden/fs/utils/SystemError.h"
using folly::StringPiece;
using folly::test::TemporaryDirectory;
using folly::test::TemporaryFile;
namespace {
boost::filesystem::path computeTempDir() {
const char* envVar = nullptr;
if ((envVar = std::getenv("TMPDIR")) || (envVar = std::getenv("TMP")) ||
(envVar = std::getenv("TEMP")) || (envVar = std::getenv("TEMPDIR"))) {
// If we found an explicit directory through the environment, use that.
return boost::filesystem::path(envVar);
}
// Try the following locations in order:
for (const auto& path : {"/dev/shm", "/tmp"}) {
if (access(path, W_OK) == 0) {
return boost::filesystem::path(path);
}
}
throw std::runtime_error("unable to find a suitable temporary directory");
}
const boost::filesystem::path& getTempDir() {
static const auto tempDir = computeTempDir();
return tempDir;
}
} // namespace
namespace facebook {
namespace eden {
TemporaryFile makeTempFile(StringPiece prefix, TemporaryFile::Scope scope) {
return TemporaryFile(prefix, getTempDir(), scope);
}
TemporaryDirectory makeTempDir(
StringPiece prefix,
TemporaryDirectory::Scope scope) {
return TemporaryDirectory(prefix, getTempDir(), scope);
}
} // namespace eden
} // namespace facebook