/*
* This file is part of the GROMACS molecular simulation package.
*
- * Copyright (c) 2012,2013, by the GROMACS development team, led by
+ * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
* Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
* and including many others, as listed in the AUTHORS file in the
* top-level source directory and at http://www.gromacs.org.
* \author Teemu Murtola <teemu.murtola@gmail.com>
* \ingroup module_testutils
*/
+#include "gmxpre.h"
+
#include "testfilemanager.h"
#include <cstdio>
#include <gtest/gtest.h>
+#include "gromacs/options/basicoptions.h"
+#include "gromacs/options/options.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/path.h"
-#include "gromacs/options/options.h"
-#include "gromacs/options/basicoptions.h"
+
#include "testutils/testoptions.h"
namespace gmx
//! Global test input data path set with setDataInputDirectory().
static const char *s_inputDirectory;
- //! Global temporary output directory for tests, set with setOutputTempDirectory().
- static const char *s_outputTempDirectory;
+ //! Global temporary output directory for tests, set with setGlobalOutputTempDirectory().
+ static const char *s_globalOutputTempDirectory;
//! Container type for names of temporary files.
typedef std::set<std::string> FileNameList;
+ /*! \brief Constructor
+ *
+ * \param path Value for the outputTempDirectory, typically
+ * set by default from s_globalOutputTempDirectory */
+ explicit Impl(const char *path)
+ : outputTempDirectory_(path)
+ {
+ GMX_RELEASE_ASSERT(Directory::exists(outputTempDirectory_),
+ "Directory for tests' temporary files does not exist");
+ }
+
/*! \brief
* Try to remove all temporary files.
*
//! List of unique paths returned by getTemporaryFilePath().
FileNameList files_;
+
+ /*! \brief Temporary output directory local to the current
+ * test, set by a test with setOutputTempDirectory() if the
+ * global default is inappropriate. */
+ std::string outputTempDirectory_;
};
-const char *TestFileManager::Impl::s_inputDirectory = NULL;
-const char *TestFileManager::Impl::s_outputTempDirectory = NULL;
+const char *TestFileManager::Impl::s_inputDirectory = NULL;
+const char *TestFileManager::Impl::s_globalOutputTempDirectory = NULL;
/** Controls whether TestFileManager should delete temporary files
after the test finishes. */
static bool g_bDeleteFilesAfterTest = true;
+//! \cond
GMX_TEST_OPTIONS(TestFileManagerOptions, options)
{
options->addOption(BooleanOption("delete-temporary-files")
.store(&g_bDeleteFilesAfterTest)
.description("At the end of each test case, delete temporary and output files"));
}
-
+//! \endcond
void TestFileManager::Impl::removeFiles()
{
*/
TestFileManager::TestFileManager()
- : impl_(new Impl())
+ : impl_(new Impl(Impl::s_globalOutputTempDirectory))
{
}
return filename;
}
-std::string TestFileManager::getTestSpecificFileName(const char *suffix)
+std::string TestFileManager::getTemporaryFilePath(const std::string &suffix)
+{
+ return getTemporaryFilePath(suffix.c_str());
+}
+
+std::string TestFileManager::getTestSpecificFileNameRoot()
{
const ::testing::TestInfo *test_info =
::testing::UnitTest::GetInstance()->current_test_info();
- std::string filename = std::string(test_info->test_case_name())
+ std::string filenameRoot = std::string(test_info->test_case_name())
+ "_" + test_info->name();
+ std::replace(filenameRoot.begin(), filenameRoot.end(), '/', '_');
+ return filenameRoot;
+}
+
+std::string TestFileManager::getTestSpecificFileName(const char *suffix)
+{
+ std::string filename = getTestSpecificFileNameRoot();
if (suffix[0] != '.')
{
filename.append("_");
}
filename.append(suffix);
- std::replace(filename.begin(), filename.end(), '/', '_');
return filename;
}
return Impl::s_inputDirectory;
}
-const char *TestFileManager::getOutputTempDirectory()
+const char *TestFileManager::getGlobalOutputTempDirectory()
{
- GMX_RELEASE_ASSERT(Impl::s_outputTempDirectory != NULL, "Path for temporary output files from tests is not set");
- return Impl::s_outputTempDirectory;
+ GMX_RELEASE_ASSERT(Impl::s_globalOutputTempDirectory != NULL, "Global path for temporary output files from tests is not set");
+ return Impl::s_globalOutputTempDirectory;
+}
+
+const char *TestFileManager::getOutputTempDirectory() const
+{
+ return impl_->outputTempDirectory_.c_str();
}
void TestFileManager::setInputDataDirectory(const char *path)
Impl::s_inputDirectory = path;
}
-void TestFileManager::setOutputTempDirectory(const char *path)
+void TestFileManager::setGlobalOutputTempDirectory(const char *path)
{
// There is no need to protect this by a mutex, as this is called in early
// initialization of the tests.
GMX_RELEASE_ASSERT(Directory::exists(path),
"Directory for tests' temporary files does not exist");
- Impl::s_outputTempDirectory = path;
+ Impl::s_globalOutputTempDirectory = path;
+}
+
+void TestFileManager::setOutputTempDirectory(const std::string &path)
+{
+ // There could be a need to protect this with a mutex, since it is
+ // intended to be used in test fixtures, not just during setup.
+ GMX_RELEASE_ASSERT(Directory::exists(path),
+ "Directory for tests' temporary files does not exist");
+ impl_->outputTempDirectory_ = path;
}
} // namespace test