diff options
Diffstat (limited to 'zencore')
| -rw-r--r-- | zencore/include/zencore/testutils.h | 31 | ||||
| -rw-r--r-- | zencore/testutils.cpp | 33 | ||||
| -rw-r--r-- | zencore/zencore.vcxproj | 2 | ||||
| -rw-r--r-- | zencore/zencore.vcxproj.filters | 2 |
4 files changed, 68 insertions, 0 deletions
diff --git a/zencore/include/zencore/testutils.h b/zencore/include/zencore/testutils.h new file mode 100644 index 000000000..72d985d5c --- /dev/null +++ b/zencore/include/zencore/testutils.h @@ -0,0 +1,31 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include <filesystem> + +namespace zen { + +std::filesystem::path CreateTemporaryDirectory(); + +class ScopedTemporaryDirectory +{ +public: + ScopedTemporaryDirectory(); + ~ScopedTemporaryDirectory(); + + std::filesystem::path& Path() { return m_RootPath; } + +private: + std::filesystem::path m_RootPath; +}; + +struct ScopedCurrentDirectoryChange +{ + std::filesystem::path OldPath{std::filesystem::current_path()}; + + ScopedCurrentDirectoryChange() { std::filesystem::current_path(CreateTemporaryDirectory()); } + ~ScopedCurrentDirectoryChange() { std::filesystem::current_path(OldPath); } +}; + +} // namespace zen diff --git a/zencore/testutils.cpp b/zencore/testutils.cpp new file mode 100644 index 000000000..116491950 --- /dev/null +++ b/zencore/testutils.cpp @@ -0,0 +1,33 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include "zencore/testutils.h" +#include <zencore/session.h> +#include "zencore/string.h" + +namespace zen { + +static std::atomic<int> Sequence{0}; + +std::filesystem::path +CreateTemporaryDirectory() +{ + std::error_code Ec; + + std::filesystem::path DirPath = std::filesystem::temp_directory_path() / GetSessionIdString() / IntNum(++Sequence).c_str(); + std::filesystem::remove_all(DirPath, Ec); + std::filesystem::create_directories(DirPath); + + return DirPath; +} + +ScopedTemporaryDirectory::ScopedTemporaryDirectory() : m_RootPath(CreateTemporaryDirectory()) +{ +} + +ScopedTemporaryDirectory::~ScopedTemporaryDirectory() +{ + std::error_code Ec; + std::filesystem::remove_all(m_RootPath, Ec); +} + +} // namespace zen
\ No newline at end of file diff --git a/zencore/zencore.vcxproj b/zencore/zencore.vcxproj index 150c42cd6..59b0bafcc 100644 --- a/zencore/zencore.vcxproj +++ b/zencore/zencore.vcxproj @@ -142,6 +142,7 @@ <ClInclude Include="include\zencore\streamutil.h" /> <ClInclude Include="include\zencore\string.h" /> <ClInclude Include="include\zencore\targetver.h" /> + <ClInclude Include="include\zencore\testutils.h" /> <ClInclude Include="include\zencore\thread.h" /> <ClInclude Include="include\zencore\timer.h" /> <ClInclude Include="include\zencore\uid.h" /> @@ -181,6 +182,7 @@ <ClCompile Include="stream.cpp" /> <ClCompile Include="streamutil.cpp" /> <ClCompile Include="string.cpp" /> + <ClCompile Include="testutils.cpp" /> <ClCompile Include="thread.cpp" /> <ClCompile Include="timer.cpp" /> <ClCompile Include="uid.cpp" /> diff --git a/zencore/zencore.vcxproj.filters b/zencore/zencore.vcxproj.filters index ea0f8a912..6c6b308f8 100644 --- a/zencore/zencore.vcxproj.filters +++ b/zencore/zencore.vcxproj.filters @@ -42,6 +42,7 @@ <ClInclude Include="include\zencore\postwindows.h" /> <ClInclude Include="include\zencore\logging.h" /> <ClInclude Include="include\zencore\session.h" /> + <ClInclude Include="include\zencore\testutils.h" /> </ItemGroup> <ItemGroup> <ClCompile Include="snapshot_manifest.cpp" /> @@ -74,6 +75,7 @@ <ClCompile Include="logging.cpp" /> <ClCompile Include="intmath.cpp" /> <ClCompile Include="session.cpp" /> + <ClCompile Include="testutils.cpp" /> </ItemGroup> <ItemGroup> <Filter Include="CAS"> |