diff options
author | Colm Donelan <colm.donelan@arm.com> | 2023-02-10 15:19:46 +0000 |
---|---|---|
committer | Colm Donelan <colm.donelan@arm.com> | 2023-02-10 15:19:46 +0000 |
commit | b5ea589e568d3f56bd868d57c850680f999f83fc (patch) | |
tree | 0e9eac9867fc3804c08d5f1b3afacee86e22af3d | |
parent | aa4f5833bea14bd92b4429c4659d205a81167486 (diff) | |
download | armnn-b5ea589e568d3f56bd868d57c850680f999f83fc.tar.gz |
IVGCVSW-7510 Delete temporary files created by DebugTestImpl.
* The test cases that use DebugTestImpl were creating temporary files
but not cleaning them up after running.
* Refactored FileSystem to extract a common RemoveDirectoryAndContents
function.
Signed-off-by: Colm Donelan <colm.donelan@arm.com>
Change-Id: I35b8d2eeed286742358a9abccbc078493d033902
-rw-r--r-- | include/armnnUtils/Filesystem.hpp | 4 | ||||
-rw-r--r-- | src/armnnUtils/Filesystem.cpp | 44 | ||||
-rw-r--r-- | src/backends/backendsCommon/test/layerTests/DebugTestImpl.cpp | 3 |
3 files changed, 35 insertions, 16 deletions
diff --git a/include/armnnUtils/Filesystem.hpp b/include/armnnUtils/Filesystem.hpp index 0b97af4323..5d547016be 100644 --- a/include/armnnUtils/Filesystem.hpp +++ b/include/armnnUtils/Filesystem.hpp @@ -29,7 +29,9 @@ fs::path NamedTempFile(const char* fileName); /// Returns full path to temporary folder std::string CreateDirectory(std::string sPath); -FileContents ReadFileContentsIntoString(const std::string path); +FileContents ReadFileContentsIntoString(const std::string& path); + +void RemoveDirectoryAndContents(const std::string& path); } // namespace armnnUtils } // namespace Filesystem diff --git a/src/armnnUtils/Filesystem.cpp b/src/armnnUtils/Filesystem.cpp index 78f928a874..d09f87ba7f 100644 --- a/src/armnnUtils/Filesystem.cpp +++ b/src/armnnUtils/Filesystem.cpp @@ -49,20 +49,8 @@ std::string CreateDirectory(std::string path) // This line is very unlikely to throw an exception. fs::path tmpDir = fs::temp_directory_path(); std::string full_path = tmpDir.generic_string() + path; - if (fs::exists(full_path)) - { - try - { - // This could throw an exception on a multi-user system. - fs::remove_all(full_path); - } - catch (const std::system_error& e) - { - std::string error = "Directory exists and cannot be removed. Reason: "; - error.append(e.what()); - throw armnn::RuntimeException(error); - } - } + // This could throw a file permission exception. + RemoveDirectoryAndContents(full_path); #if defined(_WIN32) result = _mkdir(full_path.c_str()); // can be used on Windows armnn::ConditionalThrow<armnn::RuntimeException>((result == 0), "Was unable to create temporary directory"); @@ -85,7 +73,33 @@ std::string CreateDirectory(std::string path) return full_path + "/"; } -FileContents ReadFileContentsIntoString(const std::string path) { +/** + * @brief Remove a directory and its contents. + * + * Given a directory path delete it's contents and the directory. If the specified directory doesn't exist this + * does nothing. If any item cannot be removed this will throw a RuntimeException. + * + * @param full_path + */ +void RemoveDirectoryAndContents(const std::string& path) +{ + if (fs::exists(path)) + { + try + { + // This could throw an exception on a multi-user system. + fs::remove_all(path); + } + catch (const std::system_error& e) + { + std::string error = "Directory exists and cannot be removed. Reason: "; + error.append(e.what()); + throw armnn::RuntimeException(error); + } + } +} + +FileContents ReadFileContentsIntoString(const std::string& path) { if (!fs::exists(path)) { throw armnn::RuntimeException("Path does not exist: " + path); diff --git a/src/backends/backendsCommon/test/layerTests/DebugTestImpl.cpp b/src/backends/backendsCommon/test/layerTests/DebugTestImpl.cpp index 2ec94bca58..1768f5c503 100644 --- a/src/backends/backendsCommon/test/layerTests/DebugTestImpl.cpp +++ b/src/backends/backendsCommon/test/layerTests/DebugTestImpl.cpp @@ -79,6 +79,9 @@ LayerTestResult<T, Dim> DebugTestImpl( armnnUtils::Filesystem::FileContents output = armnnUtils::Filesystem::ReadFileContentsIntoString(full_path); CHECK((output == expectedStringOutput)); + + // Clean up afterwards. + armnnUtils::Filesystem::RemoveDirectoryAndContents(tmpDir); } else { |