Belle II Software development
TempDirCreator Class Reference

changes working directory into a newly created directory, and removes it (and contents) on destruction. More...

#include <TestHelpers.h>

Public Member Functions

 TempDirCreator ()
 ctor.
 
 ~TempDirCreator ()
 dtor.
 
std::string getTempDir () const
 Returns path of temporary directory.
 

Private Attributes

std::string m_oldpwd
 previous working directory.
 
std::string m_tmpdir
 path of temporary director.
 

Detailed Description

changes working directory into a newly created directory, and removes it (and contents) on destruction.

Example:

TEST(MyTest, TestThatCreatesFiles)
{
//create files here ... (automatically cleaned up at end of scope)
}
changes working directory into a newly created directory, and removes it (and contents) on destructio...
Definition: TestHelpers.h:66

Definition at line 66 of file TestHelpers.h.

Constructor & Destructor Documentation

◆ TempDirCreator()

ctor.

Definition at line 44 of file TestHelpers.cc.

44 :
45 m_oldpwd(current_path().string())
46{
47 char* temporaryDirName = strdup((temp_directory_path() / "basf2_XXXXXX").c_str());
48 auto directory = mkdtemp(temporaryDirName);
49 if (!directory) {
50 B2ERROR("Cannot create temporary directory: " << strerror(errno));
51 free(temporaryDirName);
52 return;
53 }
54 path tmpdir = directory;
55 current_path(tmpdir);
56 m_tmpdir = tmpdir.string();
57 free(temporaryDirName);
58}
std::string m_tmpdir
path of temporary director.
Definition: TestHelpers.h:77
std::string m_oldpwd
previous working directory.
Definition: TestHelpers.h:76

◆ ~TempDirCreator()

dtor.

Definition at line 60 of file TestHelpers.cc.

61{
62 current_path(m_oldpwd);
63 remove_all(m_tmpdir);
64}

Member Function Documentation

◆ getTempDir()

std::string getTempDir ( ) const

Returns path of temporary directory.

Definition at line 66 of file TestHelpers.cc.

67{
68 return m_tmpdir;
69}

Member Data Documentation

◆ m_oldpwd

std::string m_oldpwd
private

previous working directory.

Definition at line 76 of file TestHelpers.h.

◆ m_tmpdir

std::string m_tmpdir
private

path of temporary director.

Definition at line 77 of file TestHelpers.h.


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