36 #include "OutputFileHandler.hpp" 42 #include "ArchiveLocationInfo.hpp" 45 #include "FileFinder.hpp" 46 #include "GetCurrentWorkingDirectory.hpp" 59 void CleanFolder(
const fs::path& rPath,
bool isTop=
true);
61 void CleanFolder(
const fs::path& rPath,
bool isTop)
63 assert(fs::is_directory(rPath));
64 fs::directory_iterator end_iter;
66 for (fs::directory_iterator dir_iter(rPath); dir_iter != end_iter; ++dir_iter)
68 if (fs::is_directory(dir_iter->status()))
70 CleanFolder(dir_iter->path(),
false);
74 const fs::path& r_item_path(dir_iter->path());
77 fs::remove(r_item_path);
90 bool cleanOutputDirectory)
97 bool cleanOutputDirectory)
100 std::string relative_path;
107 EXCEPTION(
"The location provided to OutputFileHandler must be inside CHASTE_TEST_OUTPUT; '" 111 if (*output_root.
GetAbsolutePath().rbegin() !=
'/' && !relative_path.empty())
113 assert(*relative_path.begin() ==
'/');
114 relative_path.erase(0, 1);
121 bool cleanOutputDirectory)
124 if (rDirectory.find(
"..") != std::string::npos)
126 EXCEPTION(
"Will not create directory: " + rDirectory +
127 " due to it potentially being above, and cleaning, CHASTE_TEST_OUTPUT.");
134 if (fs::path(rDirectory).has_root_path())
136 EXCEPTION(
"The constructor argument to OutputFileHandler must be a relative path; '" 137 << rDirectory <<
"' is absolute.");
143 if (rDirectory !=
"" && cleanOutputDirectory)
146 if (!signature_file.
Exists())
163 char *chaste_test_output = getenv(
"CHASTE_TEST_OUTPUT");
165 if (chaste_test_output ==
nullptr || *chaste_test_output == 0)
176 std::string chaste_test_output_directory = directory_root.
GetAbsolutePath();
178 return chaste_test_output_directory;
184 fs::path rel_path(rDirectory);
186 if (!rel_path.empty() && (*(--rel_path.end())) ==
".")
189 rel_path.remove_leaf();
200 fs::create_directories(output_root);
203 fs::path next_folder(output_root);
204 for (fs::path::iterator path_iter = rel_path.begin(); path_iter != rel_path.end(); ++path_iter)
206 next_folder /= *path_iter;
207 bool created_dir = fs::create_directory(next_folder);
217 catch (
const fs::filesystem_error& e)
219 TERMINATE(
"Error making test output folder: " << e.what());
227 std::string path_with_slash = (output_root / rel_path).
string();
229 return path_with_slash;
241 if (!relative_path.empty() && *relative_path.rbegin() ==
'/')
243 relative_path.erase(--relative_path.end());
245 return relative_path;
249 std::ios_base::openmode mode)
const 251 out_stream p_output_file(
new std::ofstream((
mDirectory+rFileName).c_str(), mode));
252 if (!p_output_file->is_open())
256 return p_output_file;
261 const std::string& rFileFormat,
262 std::ios_base::openmode mode)
const 264 std::stringstream string_stream;
265 string_stream << rFileName << number << rFileFormat;
278 if (rDirectory!=
"" && !( *(rDirectory.end()-1) ==
'/'))
280 rDirectory = rDirectory +
"/";
286 if (!rSourceFile.
IsFile())
292 to_path /= from_path.leaf();
297 fs::copy_file(from_path, to_path);
300 catch (
const fs::filesystem_error& e)
std::string MakeFoldersAndReturnFullPath(const std::string &rDirectory) const
static const std::string SIG_FILE_NAME
static void AddTrailingSlash(std::string &rDirectory)
FileFinder CopyFileTo(const FileFinder &rSourceFile) const
std::string GetAbsolutePath() const
#define EXCEPTION(message)
#define TERMINATE(message)
std::string GetOutputDirectoryFullPath() const
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
void SetArchiveDirectory() const
FileFinder FindFile(std::string leafName) const
std::string GetRelativePath() const
#define PATH_LEAF_NAME(path)
std::string mDirectory
The directory to store output files in (always ends in "/")
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)
static std::string GetChasteTestOutputDirectory()
static void SetArchiveDirectory(const FileFinder &rDirectory)
std::string GetRelativePath(const FileFinder &rBasePath) const
void CommonConstructor(const std::string &rDirectory, bool cleanOutputDirectory)
OutputFileHandler(const std::string &rDirectory, bool cleanOutputDirectory=true)