Gromacs
2022.2
|
#include <testutils/include/testutils/testfilemanager.h>
Helper for tests that need input and output files.
To be used as a member in a test fixture class, this class provides getTemporaryFilePath() method that returns a path for creating file names for temporary files. The returned path contains the name of the running test, making it unique across tests. Additionally, this class takes care of removing any temporary files (i.e., all paths returned by getTemporaryFilePath()) at test teardown (i.e., when the TestFileManager is destructed).
In addition, class-level static accessors provide means to access data files that are located in the test source directory. This is used to provide input files for the tests, and also to store test reference data persistently (see TestReferenceData).
Note that setInputDataDirectory(), setTestSimulationDataBaseDirectory() and setGlobalOutputTempDirectory() must be called in setup code, before creating any objects of this class that are used for accessing the paths for these respective directories. Code in tests should avoid calling setGlobalOutputTempDirectory(), and instead instantiate an object and use setOutputTempDirectory(), so that the global state is not changed.
Public Member Functions | |
TestFileManager () | |
Constructor. | |
~TestFileManager () | |
Frees internal storage and deletes any accessed file paths. More... | |
std::string | getTemporaryFilePath (const char *suffix) |
Creates a name for a temporary file within a single unit test. More... | |
std::string | getTemporaryFilePath (const std::string &suffix) |
Creates a name for a temporary file within a single unit test. More... | |
void | manageGeneratedOutputFile (const char *filename) |
Manage cleaning up this output file whose name was generated. More... | |
void | manageGeneratedOutputFile (std::string &&filename) |
Manage cleaning up this output file whose name was generated. More... | |
const char * | getOutputTempDirectory () const |
Returns the path to the output temporary directory for tests which use this TestFileManager object. More... | |
void | setOutputTempDirectory (const std::string &path) |
Sets the output temporary directory for tests which use this TestFileManager object. More... | |
Static Public Member Functions | |
static std::string | getTestSpecificFileNameRoot () |
Creates a file name root for use within a single unit test. More... | |
static std::string | getTestSpecificFileName (const char *suffix) |
Creates a file name for use within a single unit test. More... | |
static std::string | getInputFilePath (const char *filename) |
Returns the path to a test input file. More... | |
static std::string | getInputFilePath (const std::string &filename) |
Returns the path to a test input file. More... | |
static const char * | getTestSimulationDatabaseDirectory () |
Returns the path to the simulation input database directory. More... | |
static const char * | getInputDataDirectory () |
Returns the path to the test input directory. More... | |
static void | setInputDataDirectory (const std::string &path) |
Sets the test input directory. More... | |
static void | setTestSimulationDatabaseDirectory (const std::string &path) |
Sets the input directory for simulation input files. More... | |
static const char * | getGlobalOutputTempDirectory () |
Returns the path to the global test output temporary directory for future TestFileManager objects. More... | |
static void | setGlobalOutputTempDirectory (const char *path) |
Sets the default global test output temporary directory for future TestFileManager objects. More... | |
gmx::test::TestFileManager::~TestFileManager | ( | ) |
Frees internal storage and deletes any accessed file paths.
Any errors (e.g., missing files) encountered while deleting the files are ignored.
|
static |
Returns the path to the global test output temporary directory for future TestFileManager objects.
|
static |
Returns the path to the test input directory.
|
static |
Returns the path to a test input file.
[in] | filename | Relative path/filename to a test input file. |
filename
under the test input data directory.
|
static |
Returns the path to a test input file.
[in] | filename | Relative path/filename to a test input file. |
filename
under the test input data directory. const char * gmx::test::TestFileManager::getOutputTempDirectory | ( | ) | const |
Returns the path to the output temporary directory for tests which use this TestFileManager object.
std::string gmx::test::TestFileManager::getTemporaryFilePath | ( | const char * | suffix | ) |
Creates a name for a temporary file within a single unit test.
[in] | suffix | Suffix to add to the file name (should contain an extension if one is desired). |
suffix
.This method should only be called from within a Google Test test. Two calls with the same suffix
return the same string within the same test.
std::string gmx::test::TestFileManager::getTemporaryFilePath | ( | const std::string & | suffix | ) |
Creates a name for a temporary file within a single unit test.
[in] | suffix | Suffix to add to the file name (should contain an extension if one is desired). |
suffix
.This method should only be called from within a Google Test test. Two calls with the same suffix
return the same string within the same test.
|
static |
Returns the path to the simulation input database directory.
|
static |
Creates a file name for use within a single unit test.
[in] | suffix | Suffix to add to the file name (should contain an extension if one is desired). |
suffix
.This method should only be called from within a Google Test test. Two calls with the same suffix
return the same string within the same test. Intended to produce distinct names for files that may be stored in the same directory for multiple tests.
|
static |
Creates a file name root for use within a single unit test.
This method should only be called from within a Google Test test. Uses the Google Test test fixture and test case name to construct a string that is unique over all tests. Intended to produce distinct names for files that may be stored in the same directory for multiple tests.
void gmx::test::TestFileManager::manageGeneratedOutputFile | ( | const char * | filename | ) |
Manage cleaning up this output file whose name was generated.
[in] | filename | Filename relative to the working directory (which is generally not the output directory) |
This method should only be called from within a Google Test test. It lets the TestFileManager know to attempt to clean up the named file. That file should normally be one whose name is generated by the tool under test. For an output file whose name is specified by e.g. an input command-line option, pass the return value of getTemporaryFilePath() to that option.
void gmx::test::TestFileManager::manageGeneratedOutputFile | ( | std::string && | filename | ) |
Manage cleaning up this output file whose name was generated.
[in] | filename | Filename relative to the working directory (which is generally not the output directory) |
This method should only be called from within a Google Test test. It lets the TestFileManager know to attempt to clean up the named file. That file should normally be one whose name is generated by the tool under test. For an output file whose name is specified by e.g. an input command-line option, pass the return value of getTemporaryFilePath() to that option.
|
static |
Sets the default global test output temporary directory for future TestFileManager objects.
[in] | path | Path at which tests should write temporary files |
path
must name an existing directory.
This function is automatically called by unittest_main.cpp through initTestUtils(). Test fixtures should call setOutputTempDirectory(), rather than change the global state.
|
static |
Sets the test input directory.
[in] | path | Path from which test input data is looked up from. |
path
must name an existing directory.
This function is automatically called by unittest_main.cpp through initTestUtils().
void gmx::test::TestFileManager::setOutputTempDirectory | ( | const std::string & | path | ) |
Sets the output temporary directory for tests which use this TestFileManager object.
[in] | path | Path at which test should write temporary files |
path
must name an existing directory. An internal copy of path is made. The caller is responsible for holding a valid mutex on the object before calling this member function.
|
static |
Sets the input directory for simulation input files.
[in] | path | Path to look up the directory for simulation input files. |
path
must name an exisitng directory.
This function is automatically called by unittest_main.cpp through initTestUtils().