36#include "OutputFileHandler.hpp"
43#include "ArchiveLocationInfo.hpp"
46#include "FileFinder.hpp"
47#include "GetCurrentWorkingDirectory.hpp"
60void CleanFolder(
const fs::path& rPath,
bool isTop=
true);
62void CleanFolder(
const fs::path& rPath,
bool isTop)
64 assert(fs::is_directory(rPath));
65 fs::directory_iterator end_iter;
67 for (fs::directory_iterator dir_iter(rPath); dir_iter != end_iter; ++dir_iter)
69 if (fs::is_directory(dir_iter->status()))
71 CleanFolder(dir_iter->path(),
false);
75 const fs::path& r_item_path(dir_iter->path());
76 if (!isTop || (r_item_path.filename().string())[0] !=
'.')
78 fs::remove(r_item_path);
91 bool cleanOutputDirectory)
98 bool cleanOutputDirectory)
101 std::string relative_path;
108 EXCEPTION(
"The location provided to OutputFileHandler must be inside CHASTE_TEST_OUTPUT; '"
112 if (*output_root.
GetAbsolutePath().rbegin() !=
'/' && !relative_path.empty())
114 assert(*relative_path.begin() ==
'/');
115 relative_path.erase(0, 1);
122 bool cleanOutputDirectory)
125 if (rDirectory.find(
"..") != std::string::npos)
127 EXCEPTION(
"Will not create directory: " + rDirectory +
128 " due to it potentially being above, and cleaning, CHASTE_TEST_OUTPUT.");
135 if (fs::path(rDirectory).has_root_path())
137 EXCEPTION(
"The constructor argument to OutputFileHandler must be a relative path; '"
138 << rDirectory <<
"' is absolute.");
144 if (rDirectory !=
"" && cleanOutputDirectory)
147 if (!signature_file.
Exists())
164 char* chaste_test_output = std::getenv(
"CHASTE_TEST_OUTPUT");
168 if (chaste_test_output ==
nullptr || *chaste_test_output == 0)
178 std::string chaste_test_output_directory = directory_root.
GetAbsolutePath();
180 return chaste_test_output_directory;
186 fs::path rel_path(rDirectory);
194 if (!rel_path.empty())
196 assert( (*(--rel_path.end())) !=
".");
207 fs::create_directories(output_root);
210 fs::path next_folder(output_root);
211 for (fs::path::iterator path_iter = rel_path.begin(); path_iter != rel_path.end(); ++path_iter)
213 next_folder /= *path_iter;
214 bool created_dir = fs::create_directory(next_folder);
224 catch (
const fs::filesystem_error& e)
226 TERMINATE(
"Error making test output folder: " << e.what());
234 std::string path_with_slash = (output_root / rel_path).
string();
236 return path_with_slash;
248 if (!relative_path.empty() && *relative_path.rbegin() ==
'/')
250 relative_path.erase(--relative_path.end());
252 return relative_path;
256 std::ios_base::openmode mode)
const
258 out_stream p_output_file(
new std::ofstream((
mDirectory+rFileName).c_str(), mode));
259 if (!p_output_file->is_open())
263 return p_output_file;
268 const std::string& rFileFormat,
269 std::ios_base::openmode mode)
const
271 std::stringstream string_stream;
272 string_stream << rFileName << number << rFileFormat;
285 if (rDirectory!=
"" && !( *(rDirectory.end()-1) ==
'/'))
287 rDirectory = rDirectory +
"/";
293 if (!rSourceFile.
IsFile())
299 to_path /= from_path.filename();
304 fs::copy_file(from_path, to_path);
307 catch (
const fs::filesystem_error& e)
std::string DefaultChasteTestOutput()
#define TERMINATE(message)
#define EXCEPTION(message)
static void SetArchiveDirectory(const FileFinder &rDirectory)
std::string GetRelativePath(const FileFinder &rBasePath) const
std::string GetAbsolutePath() const
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)
FileFinder CopyFileTo(const FileFinder &rSourceFile) const
static void AddTrailingSlash(std::string &rDirectory)
static std::string GetChasteTestOutputDirectory()
static const std::string SIG_FILE_NAME
std::string GetOutputDirectoryFullPath() const
void SetArchiveDirectory() const
FileFinder FindFile(std::string leafName) const
std::string MakeFoldersAndReturnFullPath(const std::string &rDirectory) const
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
void CommonConstructor(const std::string &rDirectory, bool cleanOutputDirectory)
std::string mDirectory
The directory to store output files in (always ends in "/")
std::string GetRelativePath() const
OutputFileHandler(const std::string &rDirectory, bool cleanOutputDirectory=true)