https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
Moose::UnitUtils::ScopedTestDirectory Class Reference

Run a unit test from a unique temporary working directory. More...

#include <MooseUnitUtils.h>

Public Member Functions

 ScopedTestDirectory (const std::vector< std::filesystem::path > &inputs)
 
 ~ScopedTestDirectory ()
 
 ScopedTestDirectory (const ScopedTestDirectory &)=delete
 
ScopedTestDirectory & operator= (const ScopedTestDirectory &)=delete
 
const std::filesystem::path & path () const
 

Static Private Member Functions

static std::filesystem::path createPath ()
 

Private Attributes

const std::filesystem::path _original_path
 Working directory to restore upon destruction.
 
const std::filesystem::path _path
 Unique temporary working directory.
 

Detailed Description

Run a unit test from a unique temporary working directory.

The provided inputs are copied into the temporary directory using their file names. The original working directory is restored and the temporary directory is removed upon destruction.

Definition at line 140 of file MooseUnitUtils.h.

Constructor & Destructor Documentation

◆ ScopedTestDirectory() [1/2]

Moose::UnitUtils::ScopedTestDirectory::ScopedTestDirectory ( const std::vector< std::filesystem::path > &  inputs)
explicit

Definition at line 42 of file MooseUnitUtils.C.

43 : _original_path(std::filesystem::current_path()), _path(createPath())
44{
45 try
46 {
47 for (const auto & input : inputs)
48 std::filesystem::copy(
49 input, _path / input.filename(), std::filesystem::copy_options::recursive);
50 std::filesystem::current_path(_path);
51 }
52 catch (...)
53 {
54 std::error_code ec;
55 std::filesystem::remove_all(_path, ec);
56 throw;
57 }
58}
const std::filesystem::path _original_path
Working directory to restore upon destruction.
static std::filesystem::path createPath()
const std::filesystem::path _path
Unique temporary working directory.

◆ ~ScopedTestDirectory()

Moose::UnitUtils::ScopedTestDirectory::~ScopedTestDirectory ( )

Definition at line 60 of file MooseUnitUtils.C.

61{
62 std::error_code ec;
63 std::filesystem::current_path(_original_path, ec);
64 if (!ec)
65 std::filesystem::remove_all(_path, ec);
66}

◆ ScopedTestDirectory() [2/2]

Moose::UnitUtils::ScopedTestDirectory::ScopedTestDirectory ( const ScopedTestDirectory &  )
delete

Member Function Documentation

◆ createPath()

std::filesystem::path Moose::UnitUtils::ScopedTestDirectory::createPath ( )
staticprivate

Definition at line 69 of file MooseUnitUtils.C.

70{
71 while (true)
72 {
73 const auto path = generatePath();
74 if (std::filesystem::create_directory(path))
75 return path;
76 }
77}
const std::filesystem::path & path() const

◆ operator=()

ScopedTestDirectory & Moose::UnitUtils::ScopedTestDirectory::operator= ( const ScopedTestDirectory &  )
delete

◆ path()

const std::filesystem::path & Moose::UnitUtils::ScopedTestDirectory::path ( ) const
inline
Returns
The path to the temporary working directory.

Definition at line 152 of file MooseUnitUtils.h.

152{ return _path; }

Referenced by createPath().

Member Data Documentation

◆ _original_path

const std::filesystem::path Moose::UnitUtils::ScopedTestDirectory::_original_path
private

Working directory to restore upon destruction.

Definition at line 158 of file MooseUnitUtils.h.

Referenced by ~ScopedTestDirectory().

◆ _path

const std::filesystem::path Moose::UnitUtils::ScopedTestDirectory::_path
private

Unique temporary working directory.

Definition at line 160 of file MooseUnitUtils.h.

Referenced by path(), ScopedTestDirectory(), and ~ScopedTestDirectory().


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