Belle II Software light-2406-ragdoll
TempDirCreator Class Reference

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

#include <TestHelpers.h>

Collaboration diagram for TempDirCreator:

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 45 of file TestHelpers.cc.

45 :
46 m_oldpwd(current_path().string())
47{
48 char* temporaryDirName = strdup((temp_directory_path() / "basf2_XXXXXX").c_str());
49 auto directory = mkdtemp(temporaryDirName);
50 if (!directory) {
51 B2ERROR("Cannot create temporary directory: " << strerror(errno));
52 free(temporaryDirName);
53 return;
54 }
55 path tmpdir = directory;
56 current_path(tmpdir);
57 m_tmpdir = tmpdir.string();
58 free(temporaryDirName);
59}
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 61 of file TestHelpers.cc.

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

Member Function Documentation

◆ getTempDir()

std::string getTempDir ( ) const

Returns path of temporary directory.

Definition at line 67 of file TestHelpers.cc.

68{
69 return m_tmpdir;
70}

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: