Gromacs
2024.3
|
#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.
Classes | |
class | Impl |
Private implementation class for TestFileManager. More... | |
Public Member Functions | |
TestFileManager () | |
Constructor. | |
~TestFileManager () | |
Frees internal storage and deletes any accessed file paths. More... | |
std::filesystem::path | getTemporaryFilePath (const std::filesystem::path &suffix) |
Creates a name for a temporary file within a single unit test. More... | |
void | manageGeneratedOutputFile (const std::filesystem::path &filename) |
Manage cleaning up this output file whose name was generated. More... | |
std::filesystem::path | getOutputTempDirectory () const |
Returns the path to the output temporary directory for tests which use this TestFileManager object. More... | |
void | setOutputTempDirectory (const std::filesystem::path &path) |
Sets the output temporary directory for tests which use this TestFileManager object. More... | |
Static Public Member Functions | |
static std::filesystem::path | getTestSpecificFileNameRoot () |
Creates a file name root for use within a single unit test. More... | |
static std::filesystem::path | getTestSpecificFileName (const std::filesystem::path &suffix) |
Creates a file name for use within a single unit test. More... | |
static std::filesystem::path | getInputFilePath (const std::filesystem::path &filename) |
Returns the path to a test input file. More... | |
static std::filesystem::path | getTestSimulationDatabaseDirectory () |
Returns the path to the simulation input database directory. More... | |
static std::filesystem::path | getInputDataDirectory () |
Returns the path to the test input directory. More... | |
static void | setInputDataDirectory (const std::filesystem::path &path) |
Sets the test input directory. More... | |
static void | setTestSimulationDatabaseDirectory (const std::filesystem::path &path) |
Sets the input directory for simulation input files. More... | |
static std::filesystem::path | getGlobalOutputTempDirectory () |
Returns the path to the global test output temporary directory for future TestFileManager objects. More... | |
static void | setGlobalOutputTempDirectory (const std::filesystem::path &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. std::filesystem::path gmx::test::TestFileManager::getOutputTempDirectory | ( | ) | const |
Returns the path to the output temporary directory for tests which use this TestFileManager object.
std::filesystem::path gmx::test::TestFileManager::getTemporaryFilePath | ( | const std::filesystem::path & | 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 std::filesystem::path & | 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::filesystem::path & | 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().