Gromacs  2024.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Classes | Public Member Functions | Static Public Member Functions
gmx::test::TestFileManager Class Reference

#include <testutils/include/testutils/testfilemanager.h>

Description

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...
 

Constructor & Destructor Documentation

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.

Member Function Documentation

std::filesystem::path gmx::test::TestFileManager::getGlobalOutputTempDirectory ( )
static

Returns the path to the global test output temporary directory for future TestFileManager objects.

Returns
Path to default output temporary directory for the test executable.
std::filesystem::path gmx::test::TestFileManager::getInputDataDirectory ( )
static

Returns the path to the test input directory.

Returns
Path to input data directory for the test executable.
std::filesystem::path gmx::test::TestFileManager::getInputFilePath ( const std::filesystem::path &  filename)
static

Returns the path to a test input file.

Parameters
[in]filenameRelative path/filename to a test input file.
Returns
Path to 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.

Returns
Path to output temporary directory
std::filesystem::path gmx::test::TestFileManager::getTemporaryFilePath ( const std::filesystem::path &  suffix)

Creates a name for a temporary file within a single unit test.

Parameters
[in]suffixSuffix to add to the file name (should contain an extension if one is desired).
Returns
Temporary file name that includes the test name and 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::filesystem::path gmx::test::TestFileManager::getTestSimulationDatabaseDirectory ( )
static

Returns the path to the simulation input database directory.

Returns
Path to simulation input database directory.
std::filesystem::path gmx::test::TestFileManager::getTestSpecificFileName ( const std::filesystem::path &  suffix)
static

Creates a file name for use within a single unit test.

Parameters
[in]suffixSuffix to add to the file name (should contain an extension if one is desired).
Returns
File name that includes the test name and 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.

std::filesystem::path gmx::test::TestFileManager::getTestSpecificFileNameRoot ( )
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.

Parameters
[in]filenameFilename 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::setGlobalOutputTempDirectory ( const std::filesystem::path &  path)
static

Sets the default global test output temporary directory for future TestFileManager objects.

Parameters
[in]pathPath 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.

void gmx::test::TestFileManager::setInputDataDirectory ( const std::filesystem::path &  path)
static

Sets the test input directory.

Parameters
[in]pathPath 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.

Parameters
[in]pathPath 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.

void gmx::test::TestFileManager::setTestSimulationDatabaseDirectory ( const std::filesystem::path &  path)
static

Sets the input directory for simulation input files.

Parameters
[in]pathPath 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().


The documentation for this class was generated from the following files: